Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCP and POSIX sockets accept() semantics

Situation: The server calls accept(). The client sends a SYN to the server. The server gets the SYN, and then sends a SYN/ACK back to the client. However, the client now hangs up / dies, so it never sends an ACK back to the server.

What happens? Does accept() return as soon as it receives the SYN, or does block until the client's ACK is returned? If it blocks, does it eventually time-out?

like image 457
Claudiu Avatar asked Apr 01 '09 18:04

Claudiu


People also ask

Is accept () blocking?

The default mode for Accept is blocking. Accept behavior changes when the socket is nonblocking. The FCNTL() or IOCTL() calls can be used to disable blocking for a given socket. When this is done, calls that would normally block continue regardless of whether the I/O call has completed.

Does TCP listen block?

By default, TCP sockets are in "blocking" mode. For example, when you call recv() to read from a stream, control isn't returned to your program until at least one byte of data is read from the remote site.

Does listener accept block?

You call listen() only 1 time, to open the listening port, that is all it does. Then you have to call accept() for each client that you want to communicate with. That is why accept() blocks and listen() does not.


3 Answers

The call to accept() blocks until it has a connection. Unless and until the 3-way handshake completes there is no connection, so accept() should not return. For non-blocking sockets it won't block, but neither will it give you info about partially completed handshakes.

like image 177
dwc Avatar answered Oct 17 '22 08:10

dwc


If the client never sends an ACK, accept() will either block or return EAGAIN if the socket is marked non-blocking.

like image 40
sigjuice Avatar answered Oct 17 '22 07:10

sigjuice


It will eventually time out, because that scenario is in actual face a DoS (Denial of Service) and the resource for the accept returned to for use by the operating system. if might cause the master socket to block, since client is connected to the server once the accept returns with a valid file discriptor

In the event that a error occurs during the connection from the client, the value errno will be set and a good idea would be log or display an error message. , however read the man pages it is the best source of info in most cases.

like image 1
biosFF Avatar answered Oct 17 '22 09:10

biosFF