gpt4 book ai didi

java - 简单的 Netty Echo 服务器/客户端不接收消息

转载 作者:可可西里 更新时间:2023-11-01 02:31:40 27 4
gpt4 key购买 nike

我正在尝试用 Netty 编写一个简单的回显服务器。我正在阅读 Netty in Action MEAP v8了解一些理论并学习 Netty 的核心基础知识。客户端连接成功,但客户端没有消息通过。我可以通过 telnet 向服务器发送消息并接收响应,所以我猜问题出在客户端上,我只是不知道出了什么问题,因为我是 Netty 的新手。

这是客户端:

public class Client {

private final String host;
private final int port;

public Client(String host, int port) {
this.host = host;
this.port = port;
}

public void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {

@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});

ChannelFuture f = b.connect().sync();

f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}


public static void main (String [] args) throws Exception {
new Client("127.0.0.1", 11235).start();
}
}

和客户端处理程序:(我确实尝试将“\r\n”附加到已发送的消息,但这并没有什么不同,我在这里找到了:Netty Client to Server message)

@Sharable 
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

public void channelActive(ChannelHandlerContext ctx) {
System.out.println("Connected");
ctx.write(Unpooled.copiedBuffer("Netty MAY rock!", CharsetUtil.UTF_8));
}

protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
System.out.println(
"Client received: " + in.toString(CharsetUtil.UTF_8));

}

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}

服务器:

public class EchoServer {

private final int port;

public EchoServer(int port) {
this.port = port;
}

public void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
System.out.println("New client connected: " + ch.localAddress());

ch.pipeline().addLast(new EchoServerHandler());
}
});
ChannelFuture f = b.bind().sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}

public static void main (String [] args) throws Exception {
new EchoServer(11235).start();
}
}

服务器处理程序:

@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
System.out.println(
"Server received: " + in.toString(CharsetUtil.UTF_8));
ctx.write(in);
}

public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
.addListener(ChannelFutureListener.CLOSE);
}

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}

这一定是我遗漏的一些小东西,所以任何帮助都会让我保持短暂的理智,我们将不胜感激!

最佳答案

在您的 ClientHandler 中使用 writeAndFlush 而不是 write:

public void channelActive(ChannelHandlerContext ctx) {
System.out.println("Connected");
ctx.writeAndFlush(Unpooled.copiedBuffer("Netty MAY rock!", CharsetUtil.UTF_8));
}

关于java - 简单的 Netty Echo 服务器/客户端不接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25720788/

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