Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About listen(), accept() in network socket programming(3-way handshaking)

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.

like image 977
A.Cho Avatar asked Jan 08 '16 12:01

A.Cho


People also ask

What is socket () bind () listen () accept () and connect ()?

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().

What is the purpose of listen () method in socket programming?

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.

What is 3-way handshake in networking?

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.

Which method enable a server to accept connections bind () connect () listen () close ()?

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.


1 Answers

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.

like image 51
Barmar Avatar answered Sep 28 '22 01:09

Barmar