Java 为什么在服务器和客户端网络中总是调用channelActive方法
我正在使用下面的工作示例。这可能是一堵“代码墙”,但问题很简单:为什么总是调用方法Java 为什么在服务器和客户端网络中总是调用channelActive方法,java,sockets,netty,Java,Sockets,Netty,我正在使用下面的工作示例。这可能是一堵“代码墙”,但问题很简单:为什么总是调用方法channelActive?如果您不想阅读代码,也可以在这里找到示例 净周期客户: public class NettyClient { private String host; private int port = 5000; public NettyClient(String host) { this.host = host; } public vo
channelActive
?如果您不想阅读代码,也可以在这里找到示例
净周期客户:
public class NettyClient {
private String host;
private int port = 5000;
public NettyClient(String host) {
this.host = host;
}
public void send() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new ObjectEncoder());
p.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
p.addLast(new NettyClientHandler());
}
});
ChannelFuture future = b.connect(host, port).sync();
future.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
}
NettyServer
public class NettyServer extends Thread{
private Logger logger = LoggerFactory.getLogger(getClass());
private int port = 5000;
public NettyServer() {
}
@Override
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new ObjectEncoder());
p.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
p.addLast(new NettyServerHandler());
}
});
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync(); // (7)
logger.info("server bind port:{}", port);
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
logger.info(e.getMessage());
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
当我尝试从客户机->服务器写入时,我总是将其作为putput
客户端:
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
和服务器端:
2019-09-25 20:43:34.688 INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:34.984 INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:35.268 INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:35.571 INFO 8639 --- [ntLoopGroup-3-1] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:35.855 INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:36.137 INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:36.468 INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:36.777 INFO 8639 --- [ntLoopGroup-3-1] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:37.072 INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:37.364 INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:37.648 INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:37.930 INFO 8639 --- [ntLoopGroup-3-1] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:38.214 INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:38.503 INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:38.797 INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler
我们至少可以说客户端已连接到服务器。:)
每次接受新连接时都会调用channelActive
。我怀疑您在从客户端写入服务器时创建了新连接 对。但是,当连接出现时,我应该怎么做?我试着写,但什么也没发生。你需要调用writeAndFlush(…)
然后是。当我调用send()
时,客户端的一个通道将激活,服务器的一个通道将激活。然后客户端通道完全关闭。返回的通道的状态如何?您检查了它是否失败了吗?现在我已经停止使用Netty,而是使用RESTAPI HTTP。我发现Netty很难使用。很抱歉但为了回答你们的问题,我并没有得到任何返回状态。似乎当我从客户机打开一个通道并直接发送时,服务器就打开了一个到的通道。但是服务器没有收到该消息。如果你想看看我的解决方案。请查收:
// Start the Netty server
try {
new NettyServer().start();
logger.info("Starting netty server");
} catch (Exception e) {
logger.info(e.getMessage());
}
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
2019-09-25 20:43:34.688 INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:34.984 INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:35.268 INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:35.571 INFO 8639 --- [ntLoopGroup-3-1] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:35.855 INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:36.137 INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:36.468 INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:36.777 INFO 8639 --- [ntLoopGroup-3-1] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:37.072 INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:37.364 INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:37.648 INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:37.930 INFO 8639 --- [ntLoopGroup-3-1] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:38.214 INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:38.503 INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler : Channel active - server
2019-09-25 20:43:38.797 INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler