Chat Program using UDP for Linux

Hi, Today we will create two sample programs which will demonstrate the use of Sockets in UDP through a chat mechanism. Here we will write two c programs. 1) ram.c 2) shayam.c . Both will create sockets and use the two functions 1) sendto() 2) recvfrom() functions provided in the linux system calls for sending and recieving UDP datagrams over sockets.

Shayam Program

Here is shayam.c . It will run on localhost (127.0.0.1). We can change the ip according to our need. It will listen on the port 1235. For sending message to remote machine, it will use the port and ip of remote machine.(127.0.0.1: 1234). We can modify them according to our need.

/*
 A chat program using UDP and sockets in linux through system programming
 by Sandeep Nandal 2012
*/

/* It's the program which runs on shyam's end and communicate with ram's end.*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv){
	int x, z;
	char *host_ip = "127.0.0.1";
	short host_port = 1235;
	char *remote_ip = "127.0.0.1";
	short remote_port = 1234;
	int i;
	int host_sock, remote_sock;
	char datagram[512];
	int remote_len;

	struct sockaddr_in host_add;
	struct sockaddr_in remote_add;

	host_sock = socket(AF_INET, SOCK_DGRAM, 0);
	
	host_add.sin_family = AF_INET;
	host_add.sin_port = htons(host_port);
	host_add.sin_addr.s_addr = inet_addr(host_ip);

	z = bind(host_sock, (struct sockaddr *)&host_add, sizeof(host_add));

	while(1){		
		printf("Shayam: ");
		bzero(datagram, 512); 
		fgets(datagram, 512, stdin);

		remote_sock = socket(AF_INET, SOCK_DGRAM, 0);
		remote_add.sin_family = AF_INET;
		remote_add.sin_port = htons(remote_port);
		remote_add.sin_addr.s_addr = inet_addr(remote_ip);

		/* Here we are sending the udp message as datagram to remote machine */
		x = sendto(remote_sock, datagram, strlen(datagram), 0, (struct sockaddr *)&remote_add, sizeof(remote_add));
		if(x != -1){
			puts("Message sent. Waiting for response.........");
		}
		close(remote_sock);
		bzero(datagram, 512);

		/* Here we are waiting for recieving a message from remote machine */
		z = recvfrom(host_sock, datagram, 512, 0, (struct sockaddr *)&remote_add, &remote_len);
		datagram[z] = 0;
		printf("Ram:%s", datagram);

	}
	close(host_sock);
	return 0;
}

Compile the program using gcc compiler as

$ gcc shayam.c -o shayam

Ram Program

Here is ram.c program which can run on same machine or remote, we need to specify it’s ip addresses accordingly. For localhost (127.0.0.1) and for remote we can enter the actual ip of that machine.

/*
 A chat program using UDP and sockets in linux through system programming
 by Sandeep Nandal 2012
*/

/* It's the program which runs on Ram's end and communicate with shyam's end.*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv){
	int x, z;
	char *host_ip = "127.0.0.1"; /*Here we have used localhost address for host machine */
	short host_port = 1234; 	/* It's the port of host machine */
	char *remote_ip = "127.0.0.1"; /* Ip address of remote machine*/
	short remote_port = 1235; 	/* port of remote machine */
	int i;
	int host_sock, remote_sock;
	char datagram[512];
	int remote_len;

	struct sockaddr_in host_add;
	struct sockaddr_in remote_add;


	host_sock = socket(AF_INET, SOCK_DGRAM, 0);
	
	host_add.sin_family = AF_INET;
	host_add.sin_port = htons(host_port);
	host_add.sin_addr.s_addr = inet_addr(host_ip);

	z = bind(host_sock, (struct sockaddr *)&host_add, sizeof(host_add));

	while(1){
		puts("Waiting for response.........");
		bzero(datagram, 512);

		/* Here we are waiting for recieving a message from remote machine */
		z = recvfrom(host_sock, datagram, 512, 0, (struct sockaddr *)&remote_add, &remote_len);
		datagram[z] = 0;
		printf("Shayam:%s", datagram);
		printf("Ram: ");
		bzero(datagram, 512);
		fgets(datagram, 512, stdin);

		remote_sock = socket(AF_INET, SOCK_DGRAM, 0);
		remote_add.sin_family = AF_INET;
		remote_add.sin_port = htons(remote_port);
		remote_add.sin_addr.s_addr = inet_addr(remote_ip);

		/* Here we are sending the udp message as datagram to remote machine */
		x = sendto(remote_sock, datagram, strlen(datagram), 0, (struct sockaddr *)&remote_add, sizeof(remote_add));
		if(x != -1){
			printf("Message sent. ");
		}
		close(remote_sock);
	}
	close(host_sock);
	return 0;
}

Compile the program using gcc compiler as

$ gcc ram.c -o ram

Now run both the programs. shayam program will let you enter the message first and ram will be listening to the socket for the message. Once message is sent by shayam, it will be recieved by ram and then ram will let you enter the message for shayam. so in this way both the programs will keep communicating using sockets.

Twit this with your twitter acount :

Leave a Reply