Understanding blocking and non-blocking sockets
Network connections can be established on blocking sockets or non-blocking sockets. The default mode depends on the OS, but for most OSes, it is blocking mode.
In blocking mode, if a program requests an Input/Output (I/O) operation on a socket, the operation must be performed, at least partially, or an error must occur, before the control returns to the program. How can an operation be performed partially? For example, if a program tries to read 100 bytes from a blocking socket, the reading function (for instance, recv()
) will only return when it is possible to read at least one byte from the socket – otherwise, an error occurs. If no data is coming from the network, the execution of the current thread of the program will be blocked, meaning that the current thread will wait until some data comes. In some cases, a thread may wait on a blocking socket indefinitely. When attempting to send data, the current thread may block if...