Say two hosts, A and B, both attempt to start a connection with each other, but the SYN from A gets to B before B's SYN arrives at A.
My answer is that A would sent SYN ACK and would ignore the SYN from B. Am I correct?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communitySay two hosts, A and B, both attempt to start a connection with each other, but the SYN from A gets to B before B's SYN arrives at A.
My answer is that A would sent SYN ACK and would ignore the SYN from B. Am I correct?
Two connections would be made. TCP is not state aware in that fashion - neither connection would have any concept of the other connection.
For instance:
Connection 1:
192.168.1.5 sends SYN to 192.168.1.6 on port 80.
Connection 2:
192.168.1.6 sends SYN to 192.168.1.5 on port 80.
For this to continue, both would need a listening service on port 80, so each would have something listening on TCP for port 80 and that service would receive the SYN and respond with a SYN-ACK:
Connection 1:
192.167.1.6 responds with SYN-ACK to 192.168.1.5 on port 80
Connection 2:
192.167.1.5 responds with SYN-ACK to 192.168.1.6 on port 80
Keep in mind, these listening services are on opposite machines - no way to know that the other has also received a SYN, so no reason they shouldn't send an SYN-ACK.
As the TCP protocol dictates, once the originating side receives the SYN-ACK, it will respond:
Connection 1:
192.168.1.5 sends ACK to 192.168.1.6 on port 80.
Connection 2:
192.168.1.6 sends ACK to 192.168.1.5 on port 80.
You now have two independent connections with completed TCP handshakes. As mentioned in the comments by SvW: if this is a bad thing, it would be up to whatever application initiated the connections to determine that this state was present and to figure out which connection to break down - that part isn't TCP's job.
Remember that a TCP socket is a quadruplet srcHost:srcPort:dstHost:dstPort and that for a connection to be established one host must listen on a specific port.
So for the first connection, A:portA:B:portB socket will be (B listens on portB) :
And for the second connection, A:portA':B:portB' (A listens on portA')
Assuming that both hosts use the same pair of ports, then a connection would be established. In practice this does not happen, both because when hosts establish a connection, they use a random local port and a well known remote port, so both hosts would not use the same port pair. Then even if they did, the timing would have to be just right such that the SYNs pass each other in flight. If one host gets the SYN before sending its own, then it would respond with an RST.