send() not deliver all bytes?

后端 未结 3 558
故里飘歌
故里飘歌 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:52

    This behaviour is "by design".

    You can use an outer loop as shown in this example:

    int sendBuffer (SOCKET ClientSocket, const char *buf, int len, int flags) 
      {
        int num_left = len;
        int num_sent;
        int err = 0;
        const char *cp = buf;
    
        while (num_left > 0) 
          {
            num_sent = send(ClientSocket, cp, num_left, flags);
    
            if (num_sent < 0) 
              {
                err = SOCKET_ERROR;
                break;
              }
    
            assert(num_sent <= num_left);
    
            num_left -= num_sent;
            cp += num_sent;
          }
    
        return (err == SOCKET_ERROR ?  SOCKET_ERROR : len);
      }
    

提交回复
热议问题