异步聊天服务器/客户端问题... [英] Problem with Asynchronous chat server/client...
问题描述
您好。目前我在客户端和服务器上都使用异步套接字。服务器使用BeginRecieve接受数据,并将其传递给OnDataRecieved回调。
主要问题是这样的:
如果服务器重复发送数据包(即发送之间没有thread.sleep),比如说100个,那么只有10个左右的数据包被收到由客户。客户端得到了所有这些,只是数据包被捆绑在一起,只聚集成10个接收器。这当然是一个问题,因为我不希望在特定时间立即收到聊天时,客户端上的聊天坐在一起。
是否有一些技巧可以同时处理多个数据包拿起作为一个包?谢谢。
您可以定义数据包传输协议。在该协议中,您必须引入数据包的元数据(有关信息)。
For例如,您可以将数据包大小放在数据包数据前面,因此数据看起来像[size] packetData [size] packetData。
< p align = left>当你收到一堆数据包时,使用数据包大小信息解析它们是没有问题的。
大小前缀数据传输的样本可以是在这里找到 - http://vadmyst.blogspot.com/2008/01/how-to-transfer-fixed-sized-data-with。 html
HTH
-
最诚挚的问候,Vadym Stetsiak。
博客: http:/ / vadmyst .blogspot.com
Hello. Currently I am using asynchronous sockets on both client and server. The server uses BeginRecieve to accept data, and passes it to an OnDataRecieved callback.
The main problem is this:
If the server sends packets repeatedly (i.e. with no thread.sleep in between sends), say 100 of them, only 10 or so of the packets are recieved by the client. The client gets them all, it's just that the packets are bunched and clumped together into only 10 recieves. This of course is a problem, because I don't want chat sitting on top of each other on the client when it recieves it at once at specific times.
Is there some trick to handling multiple packets at once being picked up as a single packet? Thanks.
You can define a protocol of packet transfer. In that protocol you have to introduce meta data (information about) your data packet.
For example you can put packet size in front of the packet data, so data would look like [size]packetData[size]packetData.
When you'll receive a bunch of packets there would be no problem to parse them using packet size information.
Sample of size-prefixed data transfer can be found here - http://vadmyst.blogspot.com/2008/01/how-to-transfer-fixed-sized-data-with.html
HTH
--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com
这篇关于异步聊天服务器/客户端问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!