Saturday 4 January 2014

TCP introduction - I


TCP is the core protocol in TCP/IP Stack. It provides a reliable transmission way over IP which is stateless and not reliable. Nowadays, most of the applications such as email(SMTP), web(HTTP) is based on TCP.

This blog together with the next blog will talk about TCP protocol in brief.

How TCP works:

  1. Application data is devided into TCP segments, the segments will be transferred over IP.
  2. when TCP segments are transferred to the peer, the sender will setup a timer, if it does not receive the response in time, it will resend the segment.
  3. when the receiver gets a segment, it will send back an ack to acknowledge it has received the package. The acknowledge probably is not sent immediately.
  4. TCP use checksum to check if the package is valid.
  5. TCP provides some kind of traffic control.

So the basic steps for a TCP transmission is:
l         Client/server establish the connections.
l         Sender sends packages to the receiver. Retransmission will be done if no ack is received.
l         Receiver use checksum to verify if the package is valid. If it is, it will send the ack to the sender.
l         Terminates the connections.

TCP establishment steps:

The dataflow to establish a connection

detailed status:
0: client is closed status and server is listening status.
1. client sends a ‘SYN’ package.
2. server receives ‘SYN’ and the ‘SYN+ACK’ package
3. client receives ‘SYN+ACK’ and sends ‘ACK’ package.

TCP disconnection steps:


There are other possibities about closing a connection. We just give one example.
Detailed steps:
  1. client sends a FIN to the server.
  2. server sends a ACK to the client.
  3. server sends a FIN to the client
  4. client sends a ACK to the server.
Note:
Half-close: after the socket shutdown the sending channel, it can still receive the application data from the peer. This status is called half-close (FIN_WAIT_2 status in the above diagram).
Time-wait: 2 times than the MSL (maximum segment lifetime).
Reason. 
1. the last ack probably not received by the server, then the server will resend the ack but if the client is already shutdown. The server will get the exception.
2. there may be multiple FINs from server to the client due to the network glitch. If the client has been shutdown and then receives a FIN, it will confuse the new TCP session.

No comments:

Post a Comment