Differences between blocking and non-blocking socket in Linux Network Programming-slj_win's column-blog channel-csdn. net
Differences between blocking and non-blocking socket in Linux Network Programming Category: c Read by 358 Comment (0) Favorites Report Socketlinux Programming Network Server Buffer
Blocking socket and non-blocking Socket
Read operations
For a blocked socket, when no data exists in the socket receiving buffer, the read call will be blocked until data arrives.
Back. When the data volume in the socket buffer is smaller than the expected data volume, the actual number of bytes read is returned. When the receiving buffer of sockt
When the data in the partition is greater than the expected number of bytes to read, the actual length of the read is returned.
For a non-blocking socket, if there is no data in the socket's receiving buffer, the read call will return immediately. The receiving buffer has
Data is the same as blocking Socket data. If no data exists in the receiving buffer, the error code is returned.
Ewouldblock,
Indicates that the Operation should have been blocked, but because the socket is not blocked, it is returned immediately. In this case, you can
In order to try to read it next time. If the returned value is another negative value, it indicates a read error.
Therefore, non-blocking Rea calls are generally written as follows:
If (nread = read (sock_fd, buffer, Len) <0)
{
If (errno = ewouldblock)
{
Return 0; // indicates that no data is read.
} Else return-1; // indicates that the read fails.
} Else return nread; read Data Length
Write operation
For write operations, the principle is similar. When the non-blocking socket has no space for sending the buffer, it will directly return the error code ewouldblock,
Indicates that there is no space for writing data. If the error number is another value, it indicates that the sending failed. If the sending buffer has enough space or
If it is insufficient to copy all the data to be sent, the first n data records can be copied and the actual number of bytes are returned.
For blocking socket, if the sending buffer has no space or insufficient space, the write operation will be blocked directly.
If there is enough space, copy all the data to the sending buffer and return.
The general method of non-blocking write operations is:
Int write_pos = 0;
Int nleft = nlen;
While (nleft> 0)
{
Int nwrite = 0;
If (nwrite = write (sock_fd, Data + write_pos, nleft) <= 0)
{
If (errno = ewouldblock)
{
Nwrite = 0;
} Else return-1; // indicates a write failure.
}
Nleft-= nwrite;
Write_pos + = nwrite;
}
Return nlen;
Establish a connection
In blocking mode, connect first sends the SYN request to the server. When the client receives the confirmation of the SYN returned by the server
Connect
Returned. Otherwise, it will be blocked.
In non-blocking mode, connect will enable TCP three-way handshake, but the connect function does not return until the connection is established,
Return immediately. The error code returned is einprogress, indicating that a process is in progress.
Receive connection
For the listener socket in blocking mode, accept will be blocked when there is no established connection in the connection queue until there is an available connection.
Back.
The non-blocking listening socket returns immediately when there is no connection. If there is no connection, the returned error code is ewouldblock, indicating that
The block.
Non-blocking setting method
Method 1: fcntl
Int flag;
If (flag = fcntl (FD, f_getfl, 0) <0) perror ("Get flag ");
Flag | = o_nonblock;
If (fcntl (FD, f_setfl, flag) <0)
Perror ("set flag ");
Method 2: IOCTL
Int B _on = 1;
IOCTL (FD, fionbio, & B _on );