Why does connect() succeed even before my server did an accept()?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a sae socket?

516


Can a socket have multiple ports?

506


Why do we need socket programming?

500


How many sockets can a cpu have?

501


What is the difference between read() and recv()?

1598






What is the difference between SO_REUSEADDR and SO_REUSEPORT?

1356


How do I convert a string into an internet address?

910


Why do I get EPROTO from read()?

1013


What is af_inet in socket?

477


Is tcp or unix socket faster?

496


How are unix domain sockets implemented?

496


Is a socket a file?

480


What is a socket connection?

508


How many sockets can a port have?

483


What's better 6pt or 12pt sockets?

483