我使用jdk 1.7.0(u51)64bit在Windows 7终端上运行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)
在Linux / Mac上,netstat显示此进程仅抓取指定的端口(9809为侦听示例)。但是在Windows上它还会在环回(127.0.0.1)上抓取一堆其他TCP端口。
编辑: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
这些不会出现在Linux / Mac上,只能出现在Windows上。我假设这是Windows上的某种IPC机制(也许是每个工作者的线程),但是想问一下是否有人可以为我权威地澄清这一点。问题是,因为netty / java抓住这些本地端口运行任何其他尝试绑定到这些端口的应用程序(主要是开发服务器,调试器分配随机高端口)失败并显示权限被拒绝类型错误消息。我主要在linux / mac上工作,所以想知道我是否错过了一些明显的redmondism:)
回显服务器代码如下:(我把它归结为基本服务器进行测试)
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());
}
}
}
答案 0 :(得分:2)
我想我记得这就是java NIO在Windows上的工作原理,所以我们无法在Netty中做到这一点。