Transport Layer – Socket API and Network Socket Programming
Howdy fellas !!
Going forward in this series , we now understand the next important topic regarding the mighty ( :-p ) Transport Layer. Many network applications consist of two programs namely a client program and a server program (already seen in the previous posts).
When these programs have executed a client and a server process are created which communicate with each other by reading from and writing through the sockets. A socket is an interface between the transport and application layer within a host.It is also referred to as API (Application Programming Interface).
When creating a network application, a developer has to write the code for both clients and server programs. In a proprietary application of the application layer protocol used by the client and server programs , a single developer or developing team writes the client and server programs.
As the code does not implement a public domain protocol. The other independent developers can not develop code that interoperates with the application. When developing a proprietary type application, the developer needs to first decide whether the application is to run over TCP or UDP.
Sockets Programming with TCP
The socket is a programming interface, with which network applications are built. A process sends messages into and receives messages from the network through socket interface software.
The processes running on different machines communicate with each other by sending messages into sockets. Socket acts as a door between the application process and TCP as shown in figure.
The application developer’ controls everything on the application layer side of the socket but does not have any control over the transport layer side of the socket.The interaction of the client and server takes place as follows:
The client has to initiate contact with the server and when such contact is being initiated, the server should be ready.
That means the server must be a running process (not dormant) when a client initiates contact and the server process must have a socket to welcome the initial contact from the client.
With the server process running, the client process can now initiate a TCP connection to the server. This is done in the client program by creating the socket.
When the client socket is created, the client specifies the address of the server process i.e. the IP address of the server process which is nothing but the IP address of the server host and the port number of the server process.
Then the TCP on the client-side initiates a three-way handshake and establishes a connection with the server.
During the three-way handshake the client process knocks on the welcoming socket of the server process.
The server process responds to this knocking by creating a new socket called connection socket which is dedicated to that particular client.
In the last phase of the three-way handshake a TCP connection is established between the client socket and the connection socket.
The TCP connection is equivalent to a direct virtual pipe between the clients socket and server’s connection socket to allow a reliable byte-stream service between the client process and server process.
Socket Programming in UDP
As discussed in the previous section, when two processes communicate over a TCP connection, it is equivalent to communicating over a virtual pipe between the two processes.
This pipe will remain in place until one of the processes terminates the TCP connection.
Like TCP, the UDP also allows two or more processes running on different hosts to communicate. But there is a major difference.
The first difference is that UDP provides a connectionless service so there is no handshaking process in order to establish the virtual pipe like TCP.
As there is no virtual pipe existing, when a process wants to send a batch of bytes to the other process, the sending process has to attach the address of the destination process.
The destination address is a tuple consisting of the IP address of the destination host and the port number of the destination process. The IP address and port number together are called as “packet“.
UDP provides an unreliable message-oriented service in which there is no guarantee that the bytes sent by the sending process will reach the destination process.
After creating a “packet“, the sending process will push the packet into the network through a socket. This packet is then driven in the direction of destination process.
The code for UDP socket programming is different than that for TCP in the following ways:
- No need for a welcoming socket as no handshaking is needed.
- No streams are attached to the socket.
- The sending host has to create packets.
- The receiving process has to obtain information from each received packet.
Functionalities involved in Network Socket Programming
The socket mechanism allows the programmers to write code for application programs without worrying about the underlying networking details.Two modes of services are available through the socket interface i.e. connectionless and connection-oriented service.
Creating a socket : Before an application program can able to transfer data, it must first create an endpoint for communication, by calling a “socket” .
Assigning an address to a socket : After socket creation , the “bind” system call can be used to assign an address to the socket.
Establishing and accepting connections : A client establishes a connection on a socket by calling “connect” . A connection-oriented server indicates its willingness to receive connection requests by calling ” listen “. After a server calls ‘listen’ ,it can accept the connection requests by calling “accept” .
Transmitting and receiving data : Client and server may transmit data using “write” (in case of conection oriented service) or “send to ” (in case of connectionless service).
Closing a connection : “Close” is used to terminate a connection. The server waits passively most of the time until a client requires a service.
In a nutshell we can summarize :
- The ‘socket’ call creates a TCP/UDP socket.
- The ‘bind’ call binds a well known port number of the server to the socket.
- The ‘listen’ call turns the socket into a listening socket.
- The ‘accept’ call puts the server process to sleep until the arrival of a client connection request.
- The client does an active open.
- The client socket call creates a socket on the client side.
- ‘Connect’ call to establish a TCP connection to the server .
- When TCP connection is established, the ‘accept’ call at the server wakes up and returns the IP address and port address of the given connection.
- Finally , the client and server are ready to exchange the data between them.
We finally reach the end of this informative post . Hope you really enjoyed . Stay tuned for more exciting stuff in this series.
Aric is a tech enthusiast , who love to write about the tech related products and ‘How To’ blogs . IT Engineer by profession , right now working in the Automation field in a Software product company . The other hobbies includes singing , trekking and writing blogs .