API and Socket Programming
- IP Transport
The transport layer is responsible for error-free, end-to-end delivery of data from the source host to the destination host. It corresponds to the transport layer of the OSI model. The functions of the transport layer are: It facilitates the communicating hosts to carry on a conversation; It provides an interface for the users to the underlying network; It can provide for a reliable connection; It can also carry out error checking, flow control, and verification.
IP transport is a technical term used when discussing requirements for moving data between networks. Since all small networks are interconnected, the data being transported must pass through several networks each containing different network devices. In order for your data to move through these networks, a well defined framework consisting of many protocols must be in place. This creates a secure route for the data to reach its destination.
Internet Protocol (IP) provides a packet delivery service across Internet. However, IP cannot distinguish between multiple processes (applications) running on the same computer. In computing, a process is the instance of a computer program that is being executed by one or many threads. It contains the program code and its activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.
A protocol that allows an application to serve as an end-point of communication is known as a transport protocol or an end-to-end protocol. The protocols used in the transport layer are:
- TCP (Transmission Control Protocol) - It is a reliable connection-oriented protocol that transmits data from the source to the destination machine without any error. A connection is established between the peer entities prior to transmission. At the sending host, TCP divides an incoming byte stream into segments and assigns a separate sequence number to each segment. At the receiving host, TCP reorders the segments and sends an acknowledgment to the sender for correct receipt of segments. TCP also manages flow control so that a fast sender does not overwhelm a slow receiver.
- UDP (User Datagram Protocol (UDP) - It is a message-oriented protocol that provides a simple unreliable, connectionless, unacknowledged service. It is suitable for applications that do not require TCP’s sequencing, error control or flow control. It is used for transmitting a small amount of data where the speed of delivery is more important than the accuracy of delivery.
- SCTP (Stream Control Transmission Protocol) - SCTP is a computer networking communications protocol which operates at the transport layer and serves a role similar to the popular protocols TCP and UDP. It is standardized by IETF in RFC 4960. SCTP combines the features of both TCP and UDP. It is message oriented like the UDP, which providing the reliable, connection-oriented service like TCP. SCTP is used for telephony over the Internet.
- Process to Process Communication
If you look at the network layer, it is only responsible for host to host communication. That is, it can only deliver messages to the destination computer. Once a message has been delivered to the destination, it still needs to be handed over to the correct process and transport layer has the responsibility to take care of that.
Suppose you have three application processes running in your machine. Now when your computer receives data from outside that message needs to be directed to the correct application process. This is where the sockets come into play.
Transport layer actually does not directly deliver messages to processes. An intermediate software interface called a socket layer that does the transmission of messages between the underline network and the processes. A process can have one or more sockets through which it can pass data to and from network and each of these sockets have a unique identifier.
- Sockets
A socket is the interface through which a process (application) communicates with the transport layer. Each process can potentially use many sockets. The transport layer in a receiving machine receives a sequence of segments from its network layer. Delivering segments to the correct socket is called demultiplexing. Assembling segments with the necessary information and passing them to the network layer is called multiplexing. Multiplexing and demultiplexing are need whenever a communications channel is shared.
Sockets must have unique identifiers. Each segment must include header fields identifying the socket. These header fields are the source port number field and the destination port number field. Each port number is a 16-bit number: 0 to 65535.
- Multiplexing and Demultiplexing
Transport layer gathers chunks of data it receives from different sockets and encapsulate them with transport headers. Passing these resulting segments to the network layer is called multiplexing.
The reverse process which is delivering data to the correct socket by the transport layer is called demultiplexing. But this still doesn’t explain how the transport layer identifies the correct socket. Port numbers are the ones that do the trick.
- Port Numbers
Port numbers in both TCP and UDP are used to allow the operating system to direct the data to the appropriate application or, more precisely, to the socket that is associated with the communication stream on the application. A port number is just a 16-bit number that is present in both TCP and UDP headers to identify a specific endpoint on a node.
- Client and Server Processes
The process that initiates the communication is called a client and the process that waits to be contacted is called the server. To establish a connection between the client and the server, TCP needs both the IP address and the port number at each end and this combination of the IP address and the port is called a socket address.
The TCP/IP protocol allows systems to communicate even if they use different types of network hardware. For example, TCP, through an Internet connection, transmits messages between a system using Ethernet and another system using Token Ring. TCP controls the accuracy of data transmission. IP, or Internet Protocol, performs the actual data transfer between different systems on the network or Internet. Using TCP binding, you can create both client and server portions of client/server systems. For example, in the client/server type of distributed database system, users on one or more client systems can process information stored in a database on another system, called the server.
- Socket Connection
Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.
On the client-side: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. To make a connection request, the client tries to rendezvous with the server on the server's machine and port. The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. This is usually assigned by the system.
If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.
The client and server can now communicate by writing to or reading from their sockets.
[More to come ...]