moh*_*aps 4 java windows netty
我正在使用 jdk 1.7.0 (u51) 64 位在 Windows 7 Ultimate 上运行 echo 服务器。
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
Run Code Online (Sandbox Code Playgroud)
在 Linux / Mac 上,netstat 显示该进程仅获取指定端口(例如用于监听的端口为 9809)。然而在 Windows 上,它还会在环回上获取一堆其他 TCP 端口 (127.0.0.1)。
编辑:netty 版本 4.0.17.Final 和刚刚发布的 4.0.18.Final 的行为是相同的
一次运行的 Netstat 列表(PID 为 4956):
PS C:\Users\xxxx> netstat -ano | select-string 4956
TCP 0.0.0.0:9809 0.0.0.0:0 LISTENING 4956
TCP 127.0.0.1:50682 127.0.0.1:50683 ESTABLISHED 4956
TCP 127.0.0.1:50683 127.0.0.1:50682 ESTABLISHED 4956
TCP 127.0.0.1:50684 127.0.0.1:50685 ESTABLISHED 4956
TCP 127.0.0.1:50685 127.0.0.1:50684 ESTABLISHED 4956
TCP 127.0.0.1:50686 127.0.0.1:50687 ESTABLISHED 4956
TCP 127.0.0.1:50687 127.0.0.1:50686 ESTABLISHED 4956
TCP 127.0.0.1:50688 127.0.0.1:50689 ESTABLISHED 4956
TCP 127.0.0.1:50689 127.0.0.1:50688 ESTABLISHED 4956
TCP 127.0.0.1:50690 127.0.0.1:50691 ESTABLISHED 4956
TCP 127.0.0.1:50691 127.0.0.1:50690 ESTABLISHED 4956
TCP 127.0.0.1:50692 127.0.0.1:50693 ESTABLISHED 4956
TCP 127.0.0.1:50693 127.0.0.1:50692 ESTABLISHED 4956
TCP 127.0.0.1:50694 127.0.0.1:50695 ESTABLISHED 4956
TCP 127.0.0.1:50695 127.0.0.1:50694 ESTABLISHED 4956
TCP 127.0.0.1:50696 127.0.0.1:50697 ESTABLISHED 4956
TCP 127.0.0.1:50697 127.0.0.1:50696 ESTABLISHED 4956
TCP 127.0.0.1:50698 127.0.0.1:50699 ESTABLISHED 4956
TCP 127.0.0.1:50699 127.0.0.1:50698 ESTABLISHED 4956
TCP 127.0.0.1:50700 127.0.0.1:50701 ESTABLISHED 4956
TCP 127.0.0.1:50701 127.0.0.1:50700 ESTABLISHED 4956
TCP 127.0.0.1:50702 127.0.0.1:50703 ESTABLISHED 4956
TCP 127.0.0.1:50703 127.0.0.1:50702 ESTABLISHED 4956
TCP 127.0.0.1:50704 127.0.0.1:50705 ESTABLISHED 4956
TCP 127.0.0.1:50705 127.0.0.1:50704 ESTABLISHED 4956
TCP [::]:9809 [::]:0 LISTENING 4956
Run Code Online (Sandbox Code Playgroud)
这些不会显示在 Linux/Mac 上,仅显示在 Windows 上。我假设这是 Windows 上的某种 IPC 机制(可能是每个工作线程),但想问是否有人可以权威地为我澄清这一点。问题是因为 netty / java 获取这些本地端口,运行任何其他尝试绑定到这些端口的应用程序(主要是开发服务器、分配随机高端口的调试器)都会失败,并显示权限被拒绝类型错误消息。我主要在 linux/mac 上工作,所以想知道我是否错过了一些明显的 redmondism :)
echo 服务器代码如下:(我将其简化为基本服务器来测试)
package test;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class TestServer extends ChannelInitializer<SocketChannel>{
private int port = 9809;
public TestServer(int port) {
this.port = port;
}
public void run() throws Exception {
NioEventLoopGroup pool = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
Channel c = bootstrap.group(pool).channel(NioServerSocketChannel.class).childHandler(this).bind(port).sync().channel();
c.closeFuture().sync();
} finally {
pool.shutdownGracefully();
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
int port = 9809;
TestServer server = new TestServer(port);
server.run();
}
@Override
protected void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast("handler", new Handler());
}
private class Handler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object obj)
throws Exception {
ByteBuf buf = (ByteBuf)obj;
ctx.writeAndFlush(buf.retain());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想我记得这就是 java NIO 在 Windows 上的工作方式,所以我们在 Netty 中对此无能为力。
在 Windows 上,epoll系统调用不存在。因此,为了从网络获取通知,这是已实施的解决方法。
为了重现,只需打开一个Selector:
Selector selector = Selector.open();
Thread.sleep(Integer.MAX_VALUE);
Run Code Online (Sandbox Code Playgroud)
您将观察 Windows 上交叉引用的 TCP 连接:
TCP 127.0.0.1:51431 127.0.0.1:51432 ESTABLISHED
[javaw.exe]
TCP 127.0.0.1:51432 127.0.0.1:51431 ESTABLISHED
[javaw.exe]
Run Code Online (Sandbox Code Playgroud)
通常,这些连接应在您关闭后立即关闭Selector:
selector.close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2177 次 |
| 最近记录: |