Friday, September 30, 2011

Blocking Socket


Blocking Socket Calls

A blocking socket will not return control until it has sent (or received) some or all data specified for the operation. It is normal for a blocking socket not to send all data. The application must check the return value to determine how many bytes have been sent or received and it must resend any data not already processed. It also may cause problems if a socket continues to listen: a program may hang as the socket waits for data that may never arrive. When using blocking sockets, special consideration should be given to accept() as it may still block after indicating readability if a client disconnects during the connection phase.

Essentially, all socket calls are blocking in nature. In other words, they do not return control to the calling code until they complete. Ordinarily, this wouldn't matter, except that in network programming in general, and socket programming in particular, most operations fall into one of two categories:

1.  they take time to complete
2.  they rely on a third party to complete

For example, the accept function call will block until there is a connection available to be accepted. While blocked, the call will not return, and subsequently, no more processing can take place, which is especially dangerous in a single threaded application.

See Also

No comments:

Post a Comment