异步TCP套接字字节合并 [英] Asynchronous TCP socket bytes merging

查看:103
本文介绍了异步TCP套接字字节合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太清楚如何来解释我的问题了冠军,但我会尽力阐述我的问题。

I wasn't quite sure how to explain my problem in the title, but I'll try to elaborate on my problem.

基本上我编码聊天不是P2P,但其中所有用户都连接到中央服务器,类似的IRC。连接是异步的,它几乎完美的作品。的主要问题是,当大量数据被发送给一个用户(或向服务器从一个用户)一次,该字节可以合并,从而产生误差。我已经通过添加4字节包含数据中的数据的其余部分的前面的长度的头部接近此。仍然,该字节的似乎合并。我也试着设置无延迟真的 DontFragment 错误;不过,这是行不通的。

Basically I'm coding a chat that is not P2P, but where all users connect to a central server, similar to IRC. The connections are asynchronous and it almost works flawlessly. The main issue is that, when a lot of data is sent to one user (or to the server from one user) at once, the bytes may merge, resulting in errors. I've approached this by adding a header of 4 bytes containing the length of the data in front of the rest of the data. Still, the bytes seem to merge. I've also tried setting NoDelay to true and DontFragment to false; still, it doesn't work.

我猜问题是,当字节合并,我只处理第一个字节,然后什么也不做,其余。什么是处理这个问题的最佳方法是什么?

I'm guessing the problem is that when the bytes merge, I only handle the first bytes and then do nothing with the remaining. What would be the best way to approach this issue?

接收回调code: http://pastebin.com/f0MvjHag

推荐答案

这就是为什么他们称之为流。你把字节的一端和TCP保证他们出来以同样的顺序,没有遗漏或重复,在远端。任何大于一个字节是你的问题。

That's why they call it a stream. You put bytes in at one end and TCP guarantees they come out in the same order, none missing or duplicated, at the far end. Anything bigger than a byte is your problem.

您必须积累足够的字节在缓冲区中有你的头。然后跨$ P $磅,并开始处理额外的字节。你可能有一些遗留启动下一个头。

You have to accumulate enough bytes in a buffer to have your header. Then interpret it and start processing additional bytes. You may have a few left over that start the next header.

这是正常的行为。当你的应用程序不接收数据的系统将缓冲它。它会尝试下一次你犯了一个请求,手头宽裕的可用数据。在另一侧,一个大的写操作有可能越过该不支持适当的帧大小的连接。他们将被分割为必要的,在点点滴滴最终到达

This is normal behavior. When your application is not receiving data the system will be buffering it for you. It will try to hand off the available data the next time you make a request. On the other side, a large write may travel over connections that do not support an adequate frame size. They will be split as needed and arrive eventually in dribs and drabs.

这篇关于异步TCP套接字字节合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆