我正在使用jdk 1.7.0(u51)64位在Windows 7最终运行一个回声服务器。
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抓住这些本地端口运行任何其他应用程序尝试绑定到这些端口(主要是开发服务器,debugging器分配随机高端口)失败,拒绝一个权限types的错误消息。 我主要工作在Linux / MAC,所以想知道如果我错过了一些明显的redmondism 🙂
回声服务器代码如下:(我把它煮到一个基本的服务器来testing)
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()); } } }
我想我记得这只是Java NIO如何在Windows上工作,所以在Netty中我们无能为力。