TCP Protocol
The Transmission Control Protocol (TCP) provides fairly reliable transfers and it attempts to deliver the packets in order at their destination. This makes it very suitable for any application which would like to transfer a sequence of bytes (e.g., a file transfer, an e-mail or the Web) to its destination in a somewhat reliable and sequential fashion. TCP is the most ubiquitous protocol on today’s Internet and is used by many of the Internet applications that we use on an everyday basis. The Web is built on TCP/IP as HTTP uses TCP to manage the individual conversations between Web clients and Web servers by dividing the HTTP messages into smaller pieces and delivering them in order at the destination.
TCP employs the use of a number of events and controls to implement its behaviour and it is therefore far more complicated an implementation than UDP. Briefly, TCP therefore includes the following features:
1. Connection Oriented: When a TCP connection is created, the underlying protocol implemented a handshake mechanism in order to establish a TCP connection. TCP connections therefore have state and have a lifetime until they are disconnected. The procedure is broadly as follows. A client sends a SYN packet to the desired location, the TCP server, which is a minimal 40-byte packet which contains the sender’s and destination addresses. When the server receives this packet it will respond with an ACK packet, which is an acknowledgement of the server’s willingness to accept the connection.
2. Multiple TCP Connections: The connection request at the server is implemented using an accept() method. A TCP server can block an accept call or use an asynchronous event to indicate that the server should accept the connection. Once accepted, the server typically spawns a new thread and creates a socket to manage this new connection. A server therefore generally acts as a listener and multi-plexor for managing multiple TCP connections and brokers these requests to actual TCP sockets that create a one-to-one TCP relationship with the client.
3. Data Control Mechanisms: attempt to deliver data in a reliable in-order fashion and therefore employ the use of ACKs to acknowledge delivery of packets as and when they arrive. The TCP receiver also implements a buffer that is used to slot the packets in the correct order before data is delivered to the application. Therefore, even though data might arrive in the incorrect order in the first instance, the packets are re-ordered before being passed to the application.
4. TCP Events: Most C++ TCP implementations allow an application to attach itself as a listener to a socket.4 Since sockets can be senders and/or receivers of information, clients and servers receive events based on their role in the TCP connection. A client TCP socket (or the socket that is brokered to deal with the incoming connection at the server) therefore can receive Connect, Send, Receive and Disconnected events. Briefly, a client is notifed of a Connect event, when it receives an ACK back from the server indicating that the server has accepted the connection request. A Send event is passed to the client when the data has been sent to the server (either using edge- or level-trigger mechanisms) and after a connect event has been received. A Receive event is received by the client when data has arrived and is ready for collection (for asynchronous notifcation) and a Disconnected event means that the connection has been terminated. A server in the TCP implementation generally acts as a broker to create new sockets for dealing with new connections and therefore it is typically only interested in receiving Accept and Receive events. When a client has requested a connection (by sending a SYN packet), the server passes an Accept event to the listener for server connections. The application can use this event in order to call the accept() socket method in order to accept this connection (however, in Java this behaviour is implemented through a blocking call to the ServerSocket accept method). Thereafter, a server socket can be notifed with a Receive event when data has arrived and is ready for collection.