UNIX send() message size

TCP is a stream of bytes. You can send anything, but they all end up like a
stream of bytes at the other end. If you want to send and receive fixed size
messages, just make sure both sides know the length and always handle the
received chunks of that size like a message. If the size is variable, prefix a
small header giving info over the message, like size, type and so on.

Which datagram based protocols like UDP everything are packets and there's
a one to one relation between what you send and how the other will receive it.
The only difficulty is knowing how big the receive buffer should be, because if
it's too small you will lose data that didn't fit.

With UDP, if you send 100 2 byte packets, the receiver will need to call recv ()
100 times and get 2 bytes every time. With TCP, if the buffer is big enough, all
those 200 bytes may be received with one recv () call. Or more: You just don't
know and shouldn't care.

So you can't force the message size on either TCP or UDP. With TCP it's
all squashed together into a stream of bytes, and with UDP the size is as
big as the message you send, though the practical message size is limited
by the receiver's buffer size.

http://www.developerweb.net/forum/showthread.php?t=5935&highlight=recv+length
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值