Why does connect() succeed even before my server did an accept()?
Answer / chaitanya
Once you have done a listen() call on your socket, the kernel is primed to accept connections on it. The usual UNIX implementation of this works by immediately completing the SYN handshake for any incoming valid SYN segments (connection attempts), creating the socket for the new connection, and keeping this new socket on an internal queue ready for the accept() call. So the socket is fully open before the accept is done.
The other factor in this is the 'backlog' parameter for listen(); that defines how many of these completed connections can be queued at one time. If the specified number is exceeded, then new incoming connects are simply ignored (which causes them to be retried).
| Is This Answer Correct ? | 0 Yes | 0 No |
What are socket exceptions? What is out-of-band data?
How does a socket work?
Are sockets files?
What's better 6pt or 12pt sockets?
How come select says there is data, but read returns zero?
How can I be sure that UDP messages are received in order?
Why do we need socket programming?
What is active unix domain sockets?
How can I bind() to a port number < 1024?
What does af mean in sockets?
Are unix sockets faster than tcp?
How can I tell when a socket is closed on the other end?