UDP client and server program in which
i) Client sends a string to the server.
ii) Server reply with a string.
UDP Client.C
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/type.h>
#include <netinet/in.h>
#include <maloc.h>
#define PORT 7200
Void main()
{
Char sendline[128], recvline[129];
Int sockfd, bytesent, byterecv;
Struct sockaddr_in *serveraddr;
Serveraddr.sin_len = sizeof(serveraddr);
Serveraddr.sin_family = AF_INET;
Serveraddr.sin_port = htonl(PORT);
Serveraddr.sin_addr = htonl(INADDR_ANY);
Serveraddr.sin_zero = ’\0’;
sockfd=socket(AF_INET, sock_DGRAM, 0);
if(sockfd<0)
{
printf("Socket Error");
exit(0);
}
printf("Enter String");
scanf("%s", sendline);
bytesent=sendto(sockfd, sendline, strlen(sendline),0,(struct sockaddr_in*)& serveraddr, sizeof(serveraddr));
if(bytesent<0)
{
printf("Sending Error");
exit(0);
}
byterecv=recvfrom(sockfd, recvline, 129, 0, (struct sockaddr_in*) & serveraddr, sizeof(serveraddr));
if(byterecv<0)
{
printf("Receving Error");
exit(0);
}
recvline[strlen(recvline)] = '\0';
printf("received string %s", recvline);
close(sockfd);
}
UDP Server.c
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/type.h>
#include <netinet/in.h>
#include <maloc.h>
#define PORT 7200
Void main()
{
Char sendline[128], recvline[129];
int sockfd, bytesent, byterecv, K;
struct sockaddr_in *serveraddr, *clientaddr;
serveraddr.sin_len = sizeof(serveraddr);
serveraddr.sin_port = htonl(PORT);
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr= htonl("192.168.0.1");
serveraddr.sin_zero='\0';
clientaddr.sin_len=sizeof(clientaddr);
clientaddr.sin_port = htonl(PORT);
clientaddr.sin_family = AF_INET;
clientaddr.sin_addr=htonl("192.168.0.2");
clientaddr.sin_zero = '\0';
sockfd=socket(AF_INET, sock_DGRAM, 0);
if(sockfd < 0)
{
printf("Socket Error");
exit(0);
}
K= bind(sockfd,(struct sockaddr_in*)& serveraddr, sizeof(serveraddr));
if(k<0)
{
printf("Bind Error");
exit(0);
}
byterecv= recvfrom(sockfd, recvline, 129, 0, (struct sockaddr_in*)& clientaddr, sizeof(clientaddr));
if(byterecv<0)
{
printf("Receiving Error");
exit(0);
}
recvline[strlen(recvline)] = '\0';
printf("received string %s", recvline);
printf("Enter String");
scanf("%s", sendline);
bytesent=sentto(sockfd, sendline, strlen(sendline), 0, (struct sockaddr_in*)& clientaddr, sizeof(clientaddr));
if(bytesent<0)
{
printf("Sending Error");
exit(0);
}
close(sockfd);
}
i) Client sends a string to the server.
ii) Server reply with a string.
UDP Client.C
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/type.h>
#include <netinet/in.h>
#include <maloc.h>
#define PORT 7200
Void main()
{
Char sendline[128], recvline[129];
Int sockfd, bytesent, byterecv;
Struct sockaddr_in *serveraddr;
Serveraddr.sin_len = sizeof(serveraddr);
Serveraddr.sin_family = AF_INET;
Serveraddr.sin_port = htonl(PORT);
Serveraddr.sin_addr = htonl(INADDR_ANY);
Serveraddr.sin_zero = ’\0’;
sockfd=socket(AF_INET, sock_DGRAM, 0);
if(sockfd<0)
{
printf("Socket Error");
exit(0);
}
printf("Enter String");
scanf("%s", sendline);
bytesent=sendto(sockfd, sendline, strlen(sendline),0,(struct sockaddr_in*)& serveraddr, sizeof(serveraddr));
if(bytesent<0)
{
printf("Sending Error");
exit(0);
}
byterecv=recvfrom(sockfd, recvline, 129, 0, (struct sockaddr_in*) & serveraddr, sizeof(serveraddr));
if(byterecv<0)
{
printf("Receving Error");
exit(0);
}
recvline[strlen(recvline)] = '\0';
printf("received string %s", recvline);
close(sockfd);
}
UDP Server.c
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/type.h>
#include <netinet/in.h>
#include <maloc.h>
#define PORT 7200
Void main()
{
Char sendline[128], recvline[129];
int sockfd, bytesent, byterecv, K;
struct sockaddr_in *serveraddr, *clientaddr;
serveraddr.sin_len = sizeof(serveraddr);
serveraddr.sin_port = htonl(PORT);
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr= htonl("192.168.0.1");
serveraddr.sin_zero='\0';
clientaddr.sin_len=sizeof(clientaddr);
clientaddr.sin_port = htonl(PORT);
clientaddr.sin_family = AF_INET;
clientaddr.sin_addr=htonl("192.168.0.2");
clientaddr.sin_zero = '\0';
sockfd=socket(AF_INET, sock_DGRAM, 0);
if(sockfd < 0)
{
printf("Socket Error");
exit(0);
}
K= bind(sockfd,(struct sockaddr_in*)& serveraddr, sizeof(serveraddr));
if(k<0)
{
printf("Bind Error");
exit(0);
}
byterecv= recvfrom(sockfd, recvline, 129, 0, (struct sockaddr_in*)& clientaddr, sizeof(clientaddr));
if(byterecv<0)
{
printf("Receiving Error");
exit(0);
}
recvline[strlen(recvline)] = '\0';
printf("received string %s", recvline);
printf("Enter String");
scanf("%s", sendline);
bytesent=sentto(sockfd, sendline, strlen(sendline), 0, (struct sockaddr_in*)& clientaddr, sizeof(clientaddr));
if(bytesent<0)
{
printf("Sending Error");
exit(0);
}
close(sockfd);
}