Dev Oskii
Dev Oskii

Reputation: 929

What controls whether TCP segments are delivered to socket passed to accept() or socket returned by accept()?

In TCP socket code, we create 2 sockets. The first that accepts new connections, and the second that accepts data from the client and is created when the new connection is established.

What control bit in the TCP header allows the server to know whether to deliver this segment to the ServerSocket (the one to which connection requests are sent) or the Socket (the socket created for communication once the connection is established)?

Upvotes: 1

Views: 849

Answers (2)

user207421
user207421

Reputation: 310840

What control bit in the TCP header allows the server to know whether to deliver this segment to the ServerSocket (the one to which connection requests are sent) or the Socket (the socket created for communication once the connection is established)?

Apart from SYN, there isn't such a control bit. If a TCP packet arrives and there is a established TCP connection with that 4-tuple {source IP, source port, destination IP, destination port}, it is delivered to the local socket for that connection. If not, it could be an ACK to a SYN-ACK if there is one outstanding with that sequence number, which causes that half-formed connection to be placed on the listen backlog queue. Otherwise it is an error and causes an RST to be issued.

Upvotes: 2

Prabhu
Prabhu

Reputation: 3541

In very simplified manner:

The connection request message from a TCP client is identified as SYN packet. This has the SYN bit set. On such packets the control would be taken over by listen and accept calls on listen_sd; The process ends up in new socket - conn_sd.

conn_sd is identified by local IP:Port and remote IP:Port (listen_sd just by local IP:Port)

Other packets with no SYN bit set (data messages or control signaling messages like ACK (ack bit would be set), are destined to a conn_sd.

EDIT: as noted by @EJP in comments, ACK for the SYN packet from server is delivered to listen_fd.

Further reading:

How does the socket API accept() function work?

Upvotes: 0

Related Questions