1
votes

We create 2 connections between Java server and C++ client. The Java ServerSocket object call accept() twice and each returns 1 socket; the C++ client call connect() twice. We assume the port of the 1st Socket returned by accept() should be the same as the port of the first client socket calling connect(), and same for 2nd pair sockets.

But by examine the port number on each socket, I have seen the abnormal case occasionally, in which the 1st Socket from accept() connect to 2nd client socket calling connect(), and 2nd socket from accept() connect to the 1st client socket.

Both server and client are single threaded here for establishing connection. How can the sequence be reversed ?

2

2 Answers

1
votes

The order of the initial synchronization packets from the two connections may be switched on the network. For example if the "first" connection's initial SYN packet is dropped and that packet isn't resent until after the initial SYN packet of the "second" connection. In addition, even if your server and your client are singly threaded, the system code that handles the TCP stack may be multithreaded.

0
votes

You don't know what the 'sequence of connect from client' was. The connect is a three-way handshake with packet retries. The sequence in the backlog queue will be the sequence of receipt of the final ACK, about which you don't have any knowledge.