send() not deliver all bytes?

后端 未结 3 555
故里飘歌
故里飘歌 2021-01-14 07:19

Why doesn\'t send() in winsock guarantee delivery of the all bytes you request?

This is TCP and it\'s blocking sockets.

Similarly,

3条回答
  •  自闭症患者
    2021-01-14 07:50

    If it didn't send everything, just call send again on the rest. If blocking, you can do it immediately. If non-blocking, you can either wait or use a socket discovery method (like select or I/O completion ports). The same goes for recv. If you didn't get all you wanted, call recv again. This is one of the reasons both recv and send return the number of bytes sent or received.

    The number of bytes you pass to send or recv is just a limit. It can send less than that (though, unless non-blocking, usually won't). But it can definitely receive less than that. (The OS has no control over how much data it receives or when it receives it.)

    TCP is implemented for you. But if you have an application protocol that involves application-level messages, then the application has to implement them. It won't happen by magic. TCP doesn't "glue the bytes together" into a message for you. TCP is a byte-stream protocol, not a message protocol. If you want messages, you have to implement them.

提交回复
热议问题