Socket programming is also a kind of inter process communication (IPC), the different between other IPCs is it can communication between process of different remote terminal.
Socket programming is nothing but the networking program, so it’s important to understand networking terminology of basic networking concept and Open Systems Interconnection model (OSI).
Socket programming falls under transport layer programming of OSI MODEL.
Let’s check the functionality of transport layer.
Transport layer provides host to host communication for processes on remote terminal. Transport layer takes data from application layer of one terminal and break into small chunk of data or packets, ensures that data must be received in the same sequence in which it was sent by provides error checking mechanisms and data flow controls, Passes to lower network layer. And on other end of remote terminal data is retrieved as small chunk of data or packets by transport layer and passes to application layer.
Transport layer implements TCP/IP and UDP.
It’s important for Socket programming to understand the concept of TCP/IP and UDP.
TCP/IP: Transmission control protocol/Internet protocol.
UDP : User Datagram protocol.
Difference between TCP/IP and UDP.
| TCP/IP | UDP |
| Connection oriented protocol | Connectionless oriented protocol |
| Reliable and in order packet delivery | Unreliable and may or may not be in order packet delivery |
| TCP/IP Header 20 bytes(Large amount of data) | UDP Header 8 bytes(small amount of data) |
| Complicated process of acknowledgment, bigger overhead. | NO acknowledgment, small overhead. |
| Application: HTTP and FTP | Application: Video streaming |
There are important networking terminologies like socket, Port no and internet protocol.
Internet protocol address help’s in identification of remote terminal and the port no help’s identification of particular application on remote terminal.
What is socket?
A socket is defined as “the endpoint in a connection.”, socket is created with help of socket() system call which return’s a socket descriptor. Socket is a method for communication between a client program and a server program in a network.
What is internet protocol address?
An Internet Protocol address (IP address) is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. An IP address serves two principal functions: host or network interface identification and location addressing.
What is port no?
Port # is a method of identifying the end application, in computer world there are around 65535 (2^16)port #, but 0- 1024 port # are reserved as default port for well known application as given below.
| Application | Port no |
| TCP | 6 |
| UDP | 17 |
| FTP | 21 |
| DNS | 53 |
| Apache | 80 |
You can see port no information in Linux with command: /etc/services.
What is Byte Order?
Different kinds of computers use different conventions for the ordering of bytes within a word. Some computers put the most significant byte in lower memory address first ( “big-endian” order), and others puts Least significant byte in to the lower memory address first (“little-endian” order).
(Check my blog on big endian and little endian: http://tinyurl.com/plt6q74)
Below are byte order conversion API
| htons() | host to network short |
| htonl() | host to network long |
| ntohs() | network to host short |
| ntohl() | network to host long |
Block diagram for Data communication between two terminals.
The above diagram gives a clear idea of how the communication between two terminal.
Let’s take an example of data that need to be transmitted from terminal 1 to terminal 2 via FTP server having port no 21, The File Transfer Protocol (FTP) is a standard network protocol used to transfer computer files from one host to another host over a TCP-based network, such as the Internet.
One the best example is when a data need to be copied from one terminal to an embedded system or POWER-PC. In this case FTP server is run with User-password and path to data is set, while accessing from embedded system or POWER-PC , User-password is set in boot parameter of POWER-PC for accessing data on the terminal.
In this context, embedded system tries to access data with unknown port no with TCP protocol, The control is targeted to terminal via TCP on Port no 21 for FTP.
Below is the Socket programming API.
Socket Address Structures:
#include <netinet/in.h>
struct sockaddr_in {
short sin_family; // e.g. AF_INET
unsigned short sin_port; // e.g. htons(3490)
struct in_addr sin_addr; // see struct in_addr, below
char sin_zero[8]; // For padding, to make it same size as struct sockaddr
struct in_addr{
unsigned long s_addr; // load with inet_aton()
};
Sin in above structure means “sockaddr_in”
sin_family:
Family specifies the communication protocol used.
AF_UNIX Unix
AF_INET Internet AF mean’s address family,
#define AF_INET 2 /* internetwork: UDP, TCP, etc. */
#define AF_UNIX 1 /* local to host (pipes, portals) */
We stick with AF_INET.
- AF_UNIX: address format is UNIX pathname
- AF_INET: address format is host and port number
sin_port:
port can be any integer greater than 1024. Lower port numbers are reserved for specific services. For example, 21 is for FTP (see <netinet/in.h>).This port number should be agreed by both server and clients.
sin_addr:
s_addr:
s_addr is the 32-bit Internet address for the server, the constant INADDR_ANY (defined in <netinet/in.h>) can be used to tell the system that we will accept a connection on any Internet interface for the system.
sin_zero[8]:
For padding, to make it same size as struct sockaddr, As struct sockaddr_in needs to be cast to struct sockaddr it has to be kept the same size, sin_zero is an unused member whose sole purpose is to pad the structure.
Here are few basic socket API:
Flow Diagram for TCP/IP Connection.
Procedure:
- Step 1: Server and client create a stream socket s with the socket() System call.
- Step 2: Sever bind socket to a local address and port no with bind() System call, (optionak)incase of client.
- Step 3: Server uses the listen() system call to alert the TCP/IP machine to sense any incoming connection.
- Step 4: Server accepts the connection from client,
- Step 5: Client connects socket to a foreign host with the connect () system call. and link/connection is establish between client and server.
- step 6:On accepting the connection by server a new socket descriptor is obtained for all communication between client and server based on new socket descriptor .
- Step 7and 8: Server reads and writes data on socket, client reads and writes data on socket, by using send() and recv() calls, until all data has been exchanged.
- Step 9: Sever closes socket with the close() call. Client closes socket and end the TCP/IP session with the close() call or Go to step 5.
Flow Diagram for UDP Connection.
Procedure:
- Step 1: Server and client create a datagram socket with the socket() System call.
- Step 2: Sever bind socket to a local address and port no with the bind() System call, optional incase of client.
- Step 3: As UDP is connectionless protocol it does not use accept() and connect() system call. just data is transmitted from client to server and vice versa with any data acknowledgement.
- Step 4 and 5 Server reads and writes data on socket, client reads and writes data on socket, by using sendto() and recvfrom() calls, until all data has been exchanged.
- Step 6: Sever closes socket with the close() call. or Go to step 5.
- Step 7: client closes socket with the close() call



