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 does af mean in sockets?

672


How do unix sockets work?

617


How can I set the timeout for the connect() system call?

1250


How many socket connections can a server handle?

623


What is a socket file?

648






What is the purpose of socket?

665


What is a socket set used for?

687


How many sockets can a port have?

643


Why does it take so long to detect that the peer died?

1294


Why do I get EPROTO from read()?

1141


What is a deep well socket?

685


What's better 6pt or 12pt sockets?

610


What are the pros/cons of select(), non-blocking I/O and SIGIO?

1103


How many sockets can a cpu have?

626


How does unix socket work?

658