Socket programming send() return value

2020-07-20 05:50发布

问题:

In linux, or windows socket programming

I know that read returns a value which indicates the number of successfully received number of bytes.

this return value might be less than requested length. (i.e, read(sd, buf, 100) might return 50 if the receive buffer only has 50 bytes)

is it possible that

send(sd, buf, 100);

returns a value between 1~99?? if it is, what is the occasion? I want to know specific example situation.

thank you in advance

回答1:

See the man pages, or the MSDN documentation if you are talking about Winsock, for the offical specification.

In practice, send() in blocking mode sends all the data, regardless of what the documentation says, unless there was an error, in which case nothing is sent.

In non-blocking mode, it sends whatever will fit into the socket send buffer and returns that length if > 0. If the socket send buffer is full, it returns -1 with errno = EWOULDBLOCK/EAGAIN.