(Screenshot of output)
dnsserver.c
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "errno.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "arpa/inet.h"
#define MAXBUFFLEN 500
int main(int argc,char *argv[])
{
int sockfd,port,bi;
char buff[MAXBUFFLEN-1];
struct sockaddr_in servaddr,cliaddr;
int addr_len,numbytes;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
{
perror("Socket()");
return 0;
}
printf("\n Enter the port no:");
scanf("%d",&port);
printf("\n The Port no is:%d",port);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = INADDR_ANY;
memset(servaddr.sin_zero,'\0',8);
bi=bind(sockfd,(struct sockaddr*)&servaddr,sizeof(struct sockaddr));
if(bi==-1)
{
perror("bind");
return 0;
}
addr_len = sizeof(struct sockaddr*);
numbytes = recvfrom(sockfd,buff,MAXBUFFLEN-1,0,(struct sockaddr*)&cliaddr,&addr_len);
printf("\n Server Got-Packet from %s",inet_ntoa(cliaddr.sin_addr));
printf("\n Server packet is %d bytes long.",numbytes);
buff[numbytes]='\0';
printf("\n The Server packet contains %s",buff);
close(sockfd);
return 0;
}
dnsclient.c
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "errno.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "arpa/inet.h"
#include "netdb.h"
int main(int argc,char *argv[])
{
int sockfd,port;
struct sockaddr_in servaddr;
struct hostent *he;
int addr_len,numbytes;
if(argc!=3)
{
fprintf(stderr,"Client Usage : %s
exit(1);
}
if((he=gethostbyname(argv[1]))==NULL)
{
perror("gethostbyname error");
exit(1);
}
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
{
perror("Socket.");
return 0;
}
printf("\n Enter the port no:");
scanf("%d",&port);
printf("\n The port no is : %d",port);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr = *((struct in_addr*)he->h_addr);
memset(&(servaddr.sin_zero),'\0',8);
numbytes = sendto(sockfd,argv[2],sizeof(argv[2]),0,(struct sockaddr*)&servaddr,sizeof(struct sockaddr));
printf("\n Sent %d bytes to %s",numbytes,inet_ntoa(servaddr.sin_addr));
close(sockfd);
return 0;
}
Download Source Code
dnsserver.c
dnsclient.c
No comments:
Post a Comment