TCP connection basics
What is TCP
The TCP is the most widely used transmission protocol. It is the backbone of all Internet traffic. The idea behind the TCP is to provide two communicating points (we can call them host A and host B) with a reliable, stream-oriented data link. "Stream-oriented" means that neither the host A, nor the host B have to worry about how the data travels across the connection. A just puts the stream of bytes in and B receives exactly the same stream of bytes on its side. It is the responsibility of the TCP to split the data into packets for transmission through the network, retransmit lost packets, make sure there are no data overruns, etc.
The TCP is strictly a "point-to-point" protocol: only two parties can engage in a connection and no third party can "join in".
Before any data can be transmitted one of the hosts has to establish a connection to another host. This is similar to placing a telephone call: one of the parties has to call the other end.
The host that takes initiative to establish a connection is said to be opening an "outgoing connection" or "performing an active open". This is like dialing a telephone number of the desired party, only the number is the IP address of another host.
The host that accepts the "call" is said to be accepting an "incoming connection" or "performing a passive open". This is similar to picking up the phone when it starts ringing.
Once connection has been established, both parties can "say something" (send data) at any time and the TCP will make sure that all data sent on one end arrives to the other end.
TCP connections are expected to be closed (terminated) properly- there is a special exchange of messages between the host to let each other know that connection is being terminated. This is called "graceful disconnect". There is also a "reset" (abort) which is much simpler and is akin to hanging up abruptly. Finally, there is a "discard" way to end the connection is which the host simply "forgets" that there was a connection.
The TCP connection can be closed purposefully, or it can timeout.
A TCP connection in progress is fully defined by 4 parameters: IP address and the port number on host A and the IP address and port number on host B. When the host is performing an active open, it has to "dial" not just the IP address of the target host, but also the port number on this host. Ports are not physical- they are just logical subdivisions of the IP address (65536 ports per IP). If the IP is a telephone number of the whole office then the port is an extension. The "calling" host is also calling not just from its IP address but also from specific port.