In network socket programming, I know what listen() and accept() do.
But, what I want to know is, in tcp, 3-way, where does the three-way handshaking occur.
Does listen() perform 3-way hand shaking, or is is it accept()?
I mean doing syn(client) // syn/ack(server) // ack(clinet) packet.
Bind the socket to an address using the bind() function; Listen for connections with the listen() function; Accept a connection with the accept() function system call. This call typically blocks until a client connects with the server. Send and receive data by means of send() and receive().
The listen() call indicates a readiness to accept client connection requests. It transforms an active socket into a passive socket. Once called, socket can never be used as an active socket to initiate connection requests. Calling listen() is the third of four steps that a server performs to accept a connection.
A three-way handshake is also known as a TCP handshake or SYN-SYN-ACK, and requires both the client and server to exchange SYN (synchronization) and ACK (acknowledgment) packets before actual data communication begins.
A server has a bind() method which binds it to a specific IP and port so that it can listen to incoming requests on that IP and port. A server has a listen() method which puts the server into listen mode. This allows the server to listen to incoming connections. And last a server has an accept() and close() method.
Once the application has called listen()
, the TCP stack will perform the 3-way handshake for any incoming connections. These connections are queued in the kernel, and accept()
then retrieves the next connection from the queue and returns it.
There's a backlog
argument to listen
, and it specifies how large this queue should be (although I think some implementations ignore this, and use a limit built into the stack). When the queue is full, the stack will no longer perform the handshake for incoming connections; the clients should retry, and their connections will succeed when the queue has room for them.
It's done this way so that the client receives the SYN/ACK
as quickly as possible in the normal case (when the backlog queue has room), so it doesn't have to retransmit the SYN
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With