当前位置: 首页 > 知识库问答 >
问题:

我用netty编写了一个应用程序,将消息从客户端发送到服务器端,但服务器端无法接收到消息

叶阳
2023-03-14

我用netty编写了一个应用程序,客户端用channelActive方法向服务器端发送字符串消息,而服务器端没有接收到字符串消息。我不知道原因。

服务器代码:

public static void main(String[] args) throws InterruptedException {
    NioEventLoopGroup boss = new NioEventLoopGroup();
    NioEventLoopGroup worker = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();

        bootstrap.group(boss,worker);
        bootstrap.channel(NioServerSocketChannel.class);
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new NettyServerHandler());
            }
        });
        bootstrap.option(ChannelOption.SO_BACKLOG,1024);
        bootstrap.childOption(ChannelOption.SO_KEEPALIVE,true);

        ChannelFuture sync = bootstrap.bind(8080).sync();

        System.out.println("server start");

        sync.channel().closeFuture().sync();

        System.out.println("server end");
    } finally {
        worker.shutdownGracefully();
        boss.shutdownGracefully();
    }
}

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("channelRead");

    super.channelRead(ctx, msg);
    System.out.println(((ByteBuf) msg).toString(Charset.defaultCharset()));

}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channelActive");
    super.channelActive(ctx);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    System.out.println("exceptionCaught");
    super.exceptionCaught(ctx, cause);

    cause.printStackTrace();
    ctx.close();
}
}

我用服务器处理程序的方法ChannelRead打印来自客户端的消息。

以下是客户端代码:

public static void main(String[] args) throws InterruptedException {
    NioEventLoopGroup worker = new NioEventLoopGroup();
    try {
        Bootstrap bootstrap = new Bootstrap();

        bootstrap.group(worker);
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.handler(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new NettyClientHandler());
            }
        });

        ChannelFuture future = bootstrap.connect("localhost", 8080).sync();

        future.channel().closeFuture().sync();
    }finally {
        worker.shutdownGracefully();
    }
}

public class NettyClientHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channelActive");
    super.channelActive(ctx);
    ctx.writeAndFlush("message from client");
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("channelRead");
    super.channelRead(ctx, msg);

}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    System.out.println("exceptionCaught");
    super.exceptionCaught(ctx, cause);

    cause.printStackTrace();
    ctx.close();

}
}

我从通道活动的客户端发送消息,但服务器没有接收消息,字符串“message from client”

共有1个答案

刁钧
2023-03-14

这是因为在调用writeandflush(...)时使用了string。返回的ChannelFuture将告诉您消息类型不受支持。如果要使用String,则需要将StringEncoder放在ChannelPipeline上,否则只需要编写ByteBuf实例。

 类似资料:
  • 我正在试用netty,但当我响应时,客户端似乎没有收到我的消息。我使用Java NIO和socketChannel编写了相同的代码,看起来很好。我这样说是因为客户机会记录我发送的任何内容。下面是我的服务器 下面是我的服务器处理程序 } 示例响应: 我正在我的盒子上做一个tcpdump,我看到了我在电线上发送的内容,所以我不确定我做错了什么。而且,几分钟后,这会被记录下来,我不确定我在服务器或服务器

  • 问题内容: 这实际上是我在这里的第一篇文章,一段时间以来我一直在试图弄清楚这一点,但是我终于打电话给该旗帜,并将尝试寻求有关此主题的一些帮助。 因此,我有一个客户端和一个服务器,它们是根据回显客户端/服务器和安全聊天客户端/服务器建模的。我对聊天的SSL部分和使用回显仅对确保我在客户端/服务器之间收到响应不感兴趣。我将在这篇文章的底部添加所有相关代码。我现在遇到的问题是,在客户端连接后,我可以从服

  • 问题内容: 我正在尝试用两个客户端实现一个系统,其中一个客户端发送一条消息,而另一个客户端将接收该消息。下图将以更直观的方式对其进行解释: 因此,客户端1将消息发送到服务器(此工作正常),服务器接收到“推送”消息并发出应由客户端2接收的“弹出”消息。这里的问题是客户端2从未收到“流行”消息。:( 这是所有代码。 SERVER.JS 客户1(aka mobile.html) 客户2(aka web.

  • 我有一个Netty客户端和一个Netty服务器,并按照主要教程后,为了有一个EchoClient/服务器,我想让它,使我的客户端发送消息到我的服务器,当他第一次连接到它。 下面是我的的方法,这些方法应该解决这个问题: 但是正如你所看到的,教程使用了一个ByteBuf和一个String似乎不起作用! 下面是我如何在我的方法中显示收到的消息: 但是当为使用并在构造函数中初始化它并发送它时,我的服务器不

  • 当我提交表单时,我在浏览器控制台中看到“emit”消息,所以我知道表单提交事件正在触发,但我没有在服务器上收到测试消息。客户端或服务器端似乎什么也没发生,“socket.emit”函数似乎什么也没做。 我做错了什么?

  • 问题内容: 我要进行最简单的解释。我的Java TCP项目有一个服务器和三个客户端。 服务器具有一个ClientThread。每个客户端都有一个ServerThread和一个UserThread。 工作流程为: 1.客户端(例如,client_0)的UserThread获取用户输入,然后将消息发送到服务器。 2.服务器的ClientThread捕获来自client_0的消息,并将另一条消息发送到另