简体   繁体   English

使用Netty发送和接收不同类型的数据包

[英]Sending and receiving different type of packets using Netty

I want to implement sending and receiving packets (I use this in OIO) 我想实现发送和接收数据包(我在OIO中使用它)

Step 1: Server -> Client, Packet A
Step 2: Client -> Server, Packet B
Step 3: Server -> Client, Packet C

When server send Packet A to client, Client should send boolean to server. 服务器将数据包A发送给客户端时,客户端应将布尔值发送给服务器。 Then if it's true server should send PACKET B to client. 然后,如果是真的,服务器应将PACKET B发送给客户端。 I don't know how to implement this with Netty. 我不知道如何用Netty来实现这一点。 Please help me. 请帮我。

Note that the methods channelRead of the ChannelInboundHandler and the write and writeAndFlush methods of the ChannelHandlerContext takes objects as parameters. 需要注意的是方法channelRead中的ChannelInboundHandlerwritewriteAndFlush的方法ChannelHandlerContext需要对象作为参数。

You need to implement your own client and server along with encoders and decoders that receives and sends data based on the packet specifications you have. 您需要实现自己的客户端和服务器,以及根据您所拥有的数据包规范接收和发送数据的编码器和解码器。 That is you need to encode Packet A, Packet B and Packet C and send to the client. 也就是说,您需要对数据包A,数据包B和数据包C进行编码,然后发送给客户端。 Client should decode the packets and respond with a boolean which needs to be encoded and sent to the server. 客户端应解码数据包并以布尔值进行响应,该布尔值需要进行编码并发送到服务器。 Server should then decode and process the boolean. 然后,服务器应解码并处理布尔值。

The above is a lot complicated. 上面很复杂。 You can easily do this by sending JSON or any other form of data over the HTTP protocol because decoders and encoders for this protocol is already supported by netty. 您可以通过HTTP协议发送JSON或任何其他形式的数据来轻松完成此操作,因为netty已经支持该协议的解码器和编码器。 All you have to do then is to process the JSON data. 然后,您要做的就是处理JSON数据。

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

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