【发布时间】:2021-02-04 04:53:42
【问题描述】:
我有这个不工作的服务器-客户端应用程序。这是代码: 聊天服务器处理程序
public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
@Override
public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf buf) {
Channel incoming = channelHandlerContext.channel();
for (Channel channel : channels) {
if (channel != incoming){
System.out.println(buf);
channel.writeAndFlush("[" + incoming.remoteAddress() + "]" + buf + "\r\n");
}
}
buf.release();
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
Channel incoming = ctx.channel();
for (Channel channel : channels) {
channel.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " has joined" + "\r\n");
}
channels.add(incoming);
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
Channel incoming = ctx.channel();
for (Channel channel : channels) {
channel.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " has left" + "\r\n");
}
channels.remove(incoming);
}
ChatServerInitializer
@Override
protected void initChannel(SocketChannel channel) {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringDecoder());
pipeline.addLast("handler", new ChatServerHandler());
}
聊天服务器
public ChatServer(int port){
this.port = port;
}
public void run() throws Exception{
EventLoopGroup boosGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(boosGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChatServerInitializer());
bootstrap.bind(port).sync().channel().closeFuture().sync();
}finally {
boosGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
聊天客户端处理程序
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) {
System.out.println(s);
}
ChatClientInitializer
@Override
protected void initChannel(SocketChannel channel) {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new ChatClientHandler());
}
聊天客户端
public void run() {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new ChatClientInitializer());
Channel channel = bootstrap.connect(host, port).sync().channel();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(true){
channel.write(in.readLine() + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
顺便提一下,其他一切正常,只是没有收到消息。尝试使用 println 在 channelRead 打印一些东西是行不通的。这就是我知道它没有被调用的方式。 另外,对不起,如果我不应该在这里发布整个代码,但我只是没有想法。
【问题讨论】: