8

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.

1
  • Neither. listen() and accept() are not a direct interface to TCP (or any of the other supported protocols). Certainly Danny_ds is right that listen() returns (or must be able to do) before any handshakes are performed, but that does not mean that the system waits for an accept() call before establishing connections. Jan 8, 2016 at 12:59

2 Answers 2

20

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.

6
  • If 3-way handshaking is not constructed(I mean client sends RST such like that), then connection is not queued in backlog queue? Accept() only returns new socket only if there are perfect 3-way handshaking in queue?
    – A.Cho
    Jan 8, 2016 at 12:58
  • That's correct. As shown in the diagram in the other answer, accept() doesn't return until the 3-way handshake has been completed. He's only wrong about accept() triggering the 2nd step of the handshake.
    – Barmar
    Jan 8, 2016 at 13:18
  • @Barmar Are you sure that accept () system call doesn't trigger SYN + ACK at the server side ? This site says that accept() is reponsible for sending SYN + ACK ibm.com/developerworks/aix/library/au-tcpsystemcalls/#connect
    – Zephyr
    Jan 20, 2018 at 11:12
  • 2
    @Zephyr I'm certain I'm correct. Write a server that calls listen() but never calls accept(), then do a packet capture while the client connects to the port. I don't see where on that site it says that accept() sends the SYN/ACK.
    – Barmar
    Jan 22, 2018 at 4:30
  • It says: The server, after processing the SYN, sends a SYN ACK packet using the tcp_output (), ip_output (), and if_output () sequence. Nothing about accept() triggering this.
    – Barmar
    Jan 22, 2018 at 4:31
0

listen(), listens to requests that came to the server.

When a request is made (assume we use TCP, if you use UDP you will not use listen or accept its not connection oriented protocol like TCP) then it made the 3 way hand shake of TCP, if the server currently handling a request then the request gets moved to a queue. The queue has a size you can specify, the max number of pending request is OS dependent, then there is another queue that accept function, take a request from each time listen, get called and return then return new socket to be used in this connection and the (address, port) of the request to listen().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.