java - Netty 4.0.17 basic server 在 windows loopback 上抓取一堆 TCP 端口

标签 java windows netty

我正在使用 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 捕获了这些本地端口,运行任何其他试图绑定(bind)到这些端口的应用程序(主要是开发服务器,调试器分配随机高端口)失败并出现权限被拒绝类型的错误消息。我主要在 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());
        }

    }

}

最佳答案

我想我记得这就是 java NIO 在 Windows 上的工作方式,所以我们在 Netty 中对此无能为力。

在 Windows 上,epoll 系统调用不存在。因此,为了从网络获取通知,这是已实现的解决方法。

为了重现,只需打开一个Selector:

Selector selector = Selector.open();
Thread.sleep(Integer.MAX_VALUE);

您将在 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]

通常,这些连接应该在您关闭 Selector 时立即关闭:

selector.close();

关于java - Netty 4.0.17 basic server 在 windows loopback 上抓取一堆 TCP 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22774816/

相关文章:

java - Netty4 可读字节太小

java - 在netty中使用TextWebsocketFrame在不同的TextAreas中写入不同的消息

java - 使用 Apache POI 扩展 Excel 中的现有表

windows - 在 Windows 上使用卷积神经网络进行物体检测

c++ - 如何从 32 位进程获取 64 位进程的可靠内存使用信息?

java - 在 Play 上强制执行 SSL!框架

java - 创建具有透明背景的图标

java - 更改字体中的 classcastException

java - 从 ArrayList 中删除重复的自定义对象

windows - 让 Linux shell 脚本将字符串填充到 Windows 用户的复制/粘贴缓冲区中