繁体   English   中英

如何使用netty客户端编写消息?

[英]How to write messages using netty client?

我正在尝试使用 Netty 构建一个简单的 TCP 客户端-服务器应用程序。 一开始,我通过 SocketChannel 以这种方式从客户端发送消息:

    SocketChannel client = SocketChannel.open(hostAddress);
    client.write(buffer);

服务器收到所有消息,但是当我想将响应写回客户端时,我看到为了获得客户端的响应,它需要通过引导程序发送消息并定义将读取的 Inboundhandler响应(也许有人知道另一种方式?)当我尝试通过引导程序发送消息时,我使用以下代码:

    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(group).channel(NioSocketChannel.class);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            // TODO Auto-generated method stub
            ch.pipeline().addLast(new TestClientHandler());
        }

      });
    ChannelFuture future = bootstrap.connect(hostAddress);
    future.channel().writeAndFlush(buffer);

但是这样服务器根本就没有收到消息! 这是我用于服务器的代码(当我不通过客户端的引导程序发送消息时工作正常):

    bootstrap = new ServerBootstrap();
    bootstrap.group(boosGroup, workerGroup);
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
              ChannelPipeline pipeline = ch.pipeline();
              pipeline.addLast(dnsMessageDecodingHandler); 
              pipeline.addLast(group,"DnsTcpHandler",dnsTcpHandler); 
            }
          });
     future = bootstrap.bind(hostAddress).await();

在 Netty 中编写简单的客户端 - 服务器的正确方法是什么? 我没有找到任何可行的例子...

谢谢,奥斯纳特。

你应该知道 TCP 连接是如何工作的

本文介绍了典型的 TCP 流。 https://www.ibm.com/support/knowledgecenter/en/SSB23S_1.1.0.14/gtps7/s5tcpcf.html

看了文章后需要正确编写服务端代码

// create your server bootstrap
ServerBootstrap b = new ServerBootstrap();

// add your ChannelInitializer which handles when client has been connected to your server
// in ChannelInitializer you need to register your ChannelHandler, such as ChannelDuplexHandler to client channel pipeline
b.childHandler(YOUR_CHANNEL_INITIALIZER); 

b.bind(bindAddress); // your server will now listen() for client connect()

运行引导代码后,客户端可以连接到服务器

netty 存储库中有多个示例,我不确定您为什么说没有。

https://github.com/netty/netty/tree/4.1/example

也就是说,我怀疑问题在于您没有“等待”直到connect(...)操作完成。 如果您检查writeAndFlush(...)返回的 ChannelFuture 结果,它会告诉您这一点。

S所以快速解决方法是:

ChannelFuture future = bootstrap.connect(hostAddress).await();
future.channel().writeAndFlush(buffer);

在检查了从 writeAndFlush 返回的 ChannelFuture 的原因(如 Norman Maurer 建议我这样做)后,我看到原因是:不支持的消息类型:HeapByteBuffer(预期:ByteBuf,FileRegion)这是因为我没有添加ObjectEncoder 到客户端管道!! 这个添加解决了这个问题: b.handler(new ChannelInitializer() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            // TODO Auto-generated method stub
            ch.pipeline().addLast(new ObjectEncoder());
            ch.pipeline().addLast(new TestClientHandler());
        }

      });

感谢所有帮助过我的人! 奥斯纳特。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM