简体   繁体   English

插座。 数据包在我需要时排在队列中

[英]Socket. Packets stay in queue when I need them

I have strange problem with receiving data from socket. 我从套接字接收数据有一个奇怪的问题。 On client im using air socket. 在客户端即时通讯使用空气插座。 On server java netty. 在服务器Java netty上。 Im writing to socket simple packets: int numPacket, int textLength, utf8String text. 我正在写套接字简单的数据包:int numPacket,int textLength,utf8String文本。 And read on client. 并在客户端上阅读。

//server
buffer.writeInt( packetId );
ChannelBuffer ch = ChannelBuffers.copiedBuffer( text, CharsetUtil.UTF_8);
buffer.writeInt( text.length() );
buffer.writeBytes(ch);

//client
packetId = socket.readInt()
packetLen = socket.readInt()
text = socket.readUtfBytes(packetLen)

Sometimes one packets() doesnt receives by client, but server was send there, and tcpdump show that packet was send. 有时,客户端未收到一个packets(),但是服务器已发送到那里,而tcpdump显示该数据包已发送。 If server send new packet, client read previous packet, and doesn't receivs new packet - and it's works like queue that im don't need. 如果服务器发送新数据包,则客户端读取先前的数据包,并且不接收新数据包-就像我不需要的队列一样工作。 ps sorry for bad english -_- ps对不起,英语不好-_-

Looks like client maybe waiting for some byte \\n , \\u\u003c/code> etc to know the end of frame. 看起来客户端可能正在等待某个字节\\n\\u\u003c/code>等来知道帧的结尾。 I had similar problem with flash because the client was expecting a null byte at the end of the the transmission. 我在Flash中也遇到了类似的问题,因为客户端期望传输结束时为null byte

You could try to add the following sort of encoder as the last encoder in your pipeline and give it a try. 您可以尝试将以下类型的编码器添加为管道中的最后一个编码器,然后尝试一下。 The relevant code for handling nul byte is shown below. 处理nul字节的相关代码如下所示。

    ChannelBuffer nulBuffer = ChannelBuffers.wrappedBuffer(new byte[] { 0 });
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer((ChannelBuffer)msg,nulBuffer);

尝试在每三个或全部三个之后在缓冲区上使用flush()

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

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