Saturday, November 3, 2012

UDP Client Server C Program

Hii all, in this Post you will learn how to write a simple UDP Socket Program in C in linux to send a message to the server and server will reply you.
Unlike TCP, UDP does not have connection establishment.So there is no connet() system call in client and accept() systemcall in server.

So the System calls in the server are

1.socket()
2.bind()
3.recvfrom()
4.sendto(
and I client the system calls are
1.socket()
2.sendto()
3.recvfrom()

The simple UDP Client-Server Program is as follows


udp_server.c


#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<errno.h>
#define MAXSIZE 512
#define MYPORT 5608

int main()

{
int sockfd,len,ret,numbytes;
struct sockaddr_in saddr,caddr;
char recvbuf[MAXSIZE],sendbuf[MAXSIZE];

sockfd = socket(AF_INET,SOCK_DGRAM,0);

if(sockfd < 0) {
printf("soket: %s\n",strerror(errno));
return -1;
}

saddr.sin_family = AF_INET;

saddr.sin_port = htons(MYPORT);
saddr.sin_addr.s_addr = INADDR_ANY;
len = sizeof(struct sockaddr);

ret  = bind(sockfd,(struct sockaddr *)&saddr,len);

if(ret == -1) {
printf("bind server: %s\n",strerror(errno));
return -1;
}

while(1) {

printf("UDP Server Waiting for Client\n");
numbytes = recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr *)&caddr,&len);
if(numbytes == -1)
printf("recv error: %s\n",strerror(errno));
else 
{
recvbuf[numbytes] = '\0';
printf("Message received from client [%s :%d]:",inet_ntoa(caddr.sin_addr),ntohs(caddr.sin_port));
printf("%s\n",recvbuf);
}

strcpy(sendbuf,"hai");
numbytes = sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&caddr,len);
if(numbytes == -1) 
printf("send error: %s\n",strerror(errno));
}
return 0;
}


udp_client.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<errno.h>
#define MAXSIZE 512
#define MYPORT 5608
#define ADDR "127.0.0.1"

int main()

{
int sockfd,len,ret,numbytes;
struct sockaddr_in saddr;
char sendbuf[MAXSIZE],recvbuf[MAXSIZE];

sockfd = socket(AF_INET,SOCK_DGRAM,0);

if(sockfd < 0) {
printf("soket: %s\n",strerror(errno));
return -1;
}

saddr.sin_family = AF_INET;

saddr.sin_port = htons(MYPORT);
saddr.sin_addr.s_addr = inet_addr(ADDR);
len = sizeof(struct sockaddr);
strcpy(sendbuf,"hello");
numbytes =  sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&saddr,len);
if(numbytes == -1)
printf("Send error: %s\n",strerror(errno));
else 
printf("Message %s sent Successfully to [%s :%d]:\n",sendbuf,inet_ntoa(saddr.sin_addr),ntohs(saddr.sin_port));

numbytes = recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&saddr,&len);
if(numbytes == -1)
printf("receive error: %s\n",strerror(errno));
else
printf("Data Received from server:  %s\n",recvbuf);

return 0;

}


Output:
How to Compile and Run?
open two terminals and in 
Terminal1 Terminal2

$gcc -o server udp_server.c $gcc -o client udp_client.c
$./server $./client

Result:

Terminal1:
UDP Server Waiting for Client 
Message received from client [127.0.0.1 :46342]:hello 
UDP Server Waiting for Client 

Terminal2:

Message hello sent Successfully to [127.0.0.1 :5608]: 
Data Received from server:  hai 


1 comment: