Open the TCP handshake is also known as 3-way handshake (3 - way handshake), because it usually consists of three messages: one from client to server connection requests, one from the server to the client's confirmation, and another from the customer confirmation message to the server side.
Socket constructor call with a client when the connection is established, the protocol associated with the relationship between events:
The client receives the server side sent a confirmation message immediately that the connection has been successfully established.
The Internet is a best (best - effort) network, the client's initial message or a reply message server may be lost during transmission. For this reason, TCP protocol will increase the time intervals repeated several times to send handshake messages. If the TCP client after a period of time the server has not received the return message, timeout and give up the connection. In this case, the constructor will throw an IOException. Connection timeout is usually longer, so after a few minutes to Socket constructor will fail.
"SYN_SENT" is the first and second handshake messages between the professional name of the client state.
Client underlying data structure:
Socket server starts:
When a client connection request arrives, the connection will create a new socket data structure. According to the arrival of a new socket address to set the message grouping: grouping the target packet Internet address and port number (respectively wxyz and q) into the socket's local network address and port number also; group as the source address and port number (abcd: p) then become the socket of the remote Internet address and port number. Note: The new socket's local port number is always consistent with the ServerSocket port number. New state of the socket is set to indicate the "Connecting (Connecting)" (on the server side, jargon called SYN_RCVD), and add it to ServerSocket socket data structure associated with a socket connection is not completely list. Note: ServerSocket does not change their state, their information would not be any change in address.
In addition to creating a new data structure underlying socket, the server's TCP implementation but also to the client send back a TCP handshake to confirm information.
In the receiver to the client sent the three-way handshake before the third message, the server does not think the TCP handshake message has been completed. Article 3 handshake after the arrival of the new status data structure is set to "ESTABLISHED", and move it to another ServerSocket socket data structure, data structures associated with the list, the list can be represented by the ServerSocket's accept ( ) methods to receive the connection was successful. (If the first three handshake message receiver fails, the final will be "Connecting" state of the data structure to delete.)
Server program calls the ServerSocket's accept () method, as long as the socket data structure associated with the list of the arrival of new connection, the method calls to stop blocking. (Note that the call to accept () method, this list may have non-empty state.) At this point, a new connection is removed from the list data structure, and create a Sokcet instance, as the accept () method returns value.