gpt4 book ai didi

java - Netty 聊天服务器出现问题

转载 作者:行者123 更新时间:2023-12-01 13:10:31 25 4
gpt4 key购买 nike

我无法弄清楚我正在使用的简单 Netty 聊天程序出了什么问题。

我尝试过通过程序进行调试,但它无法告诉我问题所在。

通过调试,客户端似乎正在连接服务器,但写入功能似乎不起作用。

我按照教程here进行操作。我有我的源代码here在 GitHub 上。

这是客户端类。

public void run() throws Exception {
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");
}

} finally {
group.shutdownGracefully();
}
}

public class ChatClientInitializer extends ChannelInitializer<SocketChannel> {

@Override
protected void initChannel(SocketChannel ch) throws Exception {

ChannelPipeline pipeline = ch.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 class ChatClientHandler extends SimpleChannelInboundHandler<String>{


@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(msg);
}
}

这是服务器类

public void run() throws Exception{
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();

try{
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChatServerInitializer());

bootstrap.bind(port).sync().channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}

public class ChatServerInitializer extends ChannelInitializer<SocketChannel> {

@Override
protected void initChannel(SocketChannel ch) throws Exception {

ChannelPipeline pipeline = ch.pipeline();

pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());

pipeline.addLast("handler", new ChatServerHandler());
}

}

public class ChatServerHandler extends SimpleChannelInboundHandler<String>{

private static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception{
Channel incoming = ctx.channel();

channels.add(ctx.channel());

for(Channel channel : channels){
channel.write("[SERVER] - " + incoming.remoteAddress() + "has joined\n");
}
}

@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
Channel leaving = ctx.channel();


for(Channel channel : channels){
channel.write("[SERVER] - " + leaving.remoteAddress() + "has left\n");
}
channels.remove(ctx.channel());
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg)
throws Exception {
Channel incoming = ctx.channel();
System.out.println(msg);

for(Channel channel : channels){
channel.write("[" + incoming.remoteAddress() + "]" + msg +"\n");
}
}

}

最佳答案

您需要将channel.write(...)替换为channel.writeAndFlush(...)

关于java - Netty 聊天服务器出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22902639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com