非阻塞 I/O 笔记
By default, sockets are blocking. We can divide the socket calls that may block into four categories.1. Input operations-- read(), readv(), recv(), recvfrom(), recvmsg().
If we want to wait until some fixed amount of data is available, we can call our own function readn() or specify the MSG_WAITALL flag
With a nonblocking socket, if the input operation can not be satisfied, we see an immediate return with an error of EWOULDBLOCK
2. Output operations-- write(), writev(), send(), sendto(), sendmsg().
If there is no room in the socket send buffer for a blocking socket, the process is put to sleep until there is room. With a nonblocking TCP socket, if there is no room at all in the socket send buffer, we return immediately with an error of EWOULDBLOCK.
If there is some room in the socket send buffer, the return value will be the number of bytes the kernel was able to copy into the buffer(short count)
There is no actual UDP socket send buffer. The kernel just copies the application data and moves it down the stack, prepending the UDP and IP headers.
3. Accepting incoming connections-- accept().
If accept() is called for a blocking socket and a new connection is not available, the process is put to sleep.
4. Initiating outgoing connections -- connect() for TCP
connect() can be used with UDP, but it does not cause a "real" connection to be established; it just causes the kernel to store the peer's IP address and port number.
The connect() function does not return until the client receives the ACK of its SYN. This measn that a TCP connect always blocks the calling process for at least the RTT to the server.
If connect() is called for a nonblocking TCP socket and the connection cannot be established immediately, the erro EINPROGRESS is returned
[ 本帖最后由 madfrogme 于 2012-9-1 21:31 编辑 ]