Communication between server- client through socket programming using UDP/IP
In the previous tutorial, I explained about communication between server-client through socket programming. We can also establish server-client communication using UDP/IP. In this tutorial, I will explain how communication occurs between server-client through UDP/IP. Before explanation, let’s see some basic view of terminology.
Three main things are needed to establish connection between server-client models:
1) Transport protocol (TCP or UDP)
3) IP address and port
Transport protocol (UDP)
UDP is a connection-less protocol which stands for user datagram protocol. It is an unreliable protocol but with IP it provides the best communication mechanism. Many characteristics are similar to TCP but not all. IN UDP, the receiver cannot generate the acknowledgement of received packet so sender (client) can send data without waiting for the acknowledgement.
The socket is the end point connection for communication between two machines. It seems like data connectivity path between two wireless terminals. The socket is required at both sides of server and client. You can refer to the tutorial socket and How to create a socket in Linux.
IP address and port
IP address is a unique numerical address of each computer and device. It plays an important role in networking domain with Internet protocol. In server – client model, the server needs to know as to which device is connected with it and with whom server establishes the connection. IP address plays an important role in communication between server-client models.
In terms of networking, the port is the location where information is sent. Various protocols assign different port numbers. The specific port number is required to communicate between server-client according to which protocol is used.
Let’s check how to get user’s IP address. Enter the following command from command terminal:
It displays network information like address, data packet etc. Be sure which type of Internet connection is there in your system.
The IP address of the system is located by “inet addr”. It is your system’s IP address.
For example, inet addr: xxx.xx.xx.xx
Here, x represents numerical value.
Server- client socket programming
Let’s look at server –client communication through socket programming. I assume that you know about the basic C programming and socket. If not, kindly refer to the tutorial socket and How to create socket in Linux before learning this tutorial.
Here I have written two individual programs, one for sever side and one for client side. We need to have a computer with Linux environment. But do not worry; you can communicate with the only single system.
int IPlength, TotalByte;
struct sockaddr_in serverAdd , clientAdd;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
perror(“Socket Creation Failed”);
serverAdd.sin_family = AF_INET;
serverAdd.sin_port = htons(5000);
serverAdd.sin_addr.s_addr = INADDR_ANY;
if (bind(sock,(struct sockaddr *)&serverAdd,sizeof(struct sockaddr)) == -1)
IPlength = sizeof(struct sockaddr);
printf(“nUDP Server Waiting for client Messagen”);
TotalByte = recvfrom(sock,message,512,0,(struct sockaddr *)&clientAdd, &IPlength);
message[TotalByte] = ‘