简体   繁体   English

Netty客户端引导连接失败

[英]Netty client bootstrap connection fails

I am building a client / server with Netty 4.0. 我正在使用Netty 4.0构建客户端/服务器。 The server is listening properly on localhost:8083 and I can telnet on it, it triggers server breakpoints properly. 服务器正在localhost:8083上正确侦听,并且可以在telnet上远程登录,它可以正确触发服务器断点。 But when I try to connect with that piece of code : 但是当我尝试连接这段代码时:

_bootstrap = new Bootstrap();
_bootstrap.group(locGroup)
    .channel(NioServerSocketChannel.class)
    .handler(new ClientInit(this, _sslContext, _logger));
_bootstrap.remoteAddress("127.0.0.1", 8083);
ChannelFuture locChannelFuture = _bootstrap.connect();
_channel = locChannelFuture.sync().channel();

It throws an exception at sync() : java.nio.channels.ClosedChannelException. 它在sync()处引发异常:java.nio.channels.ClosedChannelException。 As said before, when I telnet 127.0.0.1 8083 (or connect in code with a Socket), it does work. 如前所述,当我telnet 127.0.0.1 8083(或使用Socket在代码中连接)时,它确实起作用。 Any idea ? 任何想法 ? Thank you. 谢谢。

I fixed it. 我修好了它。 The problem was simply that I gave NioServerSocketChannel as the default channel class to the bootstrap constructor. 问题很简单,我将NioServerSocketChannel作为引导程序构造函数的默认通道类。 Building a client, I put NioSocketChannel instead, and it's working fine. 建立一个客户端,我改用NioSocketChannel,它工作正常。

So, here is the correct one : 所以,这是正确的一个:

_bootstrap.group(locGroup)
    .channel(NioSocketChannel.class)
    .handler(new ClientInit(this, _sslContext, _logger));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM