DNS USING SOCKETS

Database program

#include<stdio.h>
main()
{
 int n,i;
 FILE *fd;
 struct
 {
  char domain[16];
  char ipaddr[16];
 } address;
 fd=fopen("drsi.txt","w");
printf("\n\t Enter the total No of ADDRESS :");
scanf("%d",&n);
 for(i=0;i<n;i++)
{
  printf("\n\t Enter the Domain name, IP address\n");
  scanf("%s%s",address.domain,address.ipaddr);
 fprintf(fd,"%s\n%s\n",address.domain,address.ipaddr);
}
}

Server program

 #include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#define BACKLOG 5
main()
{
 int sd,sd1,len,port;
 FILE *f1;
 struct
 {
   char dname[16];
   char ipaddr[24];
 } address;
 struct sockaddr_in serv,cli;
 char buf[50],res[50],buf1[155];
 if((sd=socket(AF_INET,SOCK_STREAM,0)) == -1)
 {
   printf("\n\t Error in Creation of Sockets ");
   exit(-1);
 }

 bzero(&(cli.sin_zero),8);
 serv.sin_family=AF_INET;
 printf("\tEnter the port number  : ");
 scanf("%d",&port);
 serv.sin_port=htons(port);
 serv.sin_addr.s_addr=htonl(INADDR_ANY);
 if(bind(sd,(struct sockaddr*)&serv,sizeof(serv)) <0)
 {
  printf("\n\t Error in Bind ");
  exit(-1);
 }
 if(listen(sd,BACKLOG)== -1)
 {
  printf("\n\t Error in Listen ");
   exit(-1);
 }
 len=sizeof(cli);
 if((sd1=accept(sd,(struct sockaddr *)&cli,&len)) <0)
 {
  printf("\n\t Error in accept");
  exit(-1);
 }
 printf("\n\tConnection Established ");
 do
 {
   recv(sd1,buf,50,0);
   printf("\n\t The Given domain is : %s \n",buf);
    f1=fopen("drsi.txt","r");

    while(!feof(f1))
   {
    fscanf(f1,"%s %s",address.dname,address.ipaddr);
    if(strcmp(buf,address.dname)==0)
    {
     strcpy(res,address.ipaddr);
     break;
    }
    else
     strcpy(res,"NIL");
  }
fclose(f1);
 printf("\n\t The IP address for the given domain is : %s \n");
 send(sd1,res,50,0);
 }
while((strcmp(buf,"exit")) != 0);
close(sd1);
}

Client program

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#define MAXDATASIZE 100
main()
{
 int sd,numbytes=0,port;
 struct sockaddr_in serv;
 char buf[MAXDATASIZE],buf1[50];
 if((sd=socket(AF_INET,SOCK_STREAM,0)) == -1)
 {
   printf("\n\t Error in Creation of Sockets ");
   exit(-1);
 }

 bzero(&(serv.sin_zero),8);
 serv.sin_family=AF_INET;
printf("\tEnter the port numbetr  : ");
scanf("%d",&port);
 serv.sin_port=htons(port);
 serv.sin_addr.s_addr=htonl(INADDR_ANY);

 if(connect(sd,(struct sockaddr*)&serv,sizeof(serv)) <0)
 {
  printf("\n\t Error in Connect ");
  exit(-1);
 }
 printf("\n\tConnection Established ");
 do
 {
   printf("\n\t Enter the Domain name : ");
   scanf("%s",buf);
   send(sd,buf,50,0);
   recv(sd,buf1,50,0);
   printf("\n\t Equal IP Address : %s\n",buf1);
 }
while((strcmp(buf,"exit")) != 0);
close(sd);
exit(0);
}






























OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 14samser.c
[11ca013@mcalinux network]$ ./a.out
        Enter the port number  : 3326

        Connection Established
         The Given domain is : www.rajiv.org

         The IP address for the given domain is : 192.1.1.1

         The Given domain is : www.gandhi.in

         The IP address for the given domain is : 192.2.2.2

         The Given domain is : www.selva.com

         The IP address for the given domain is : 192.3.3.3

CLIENT

[11ca013@mcalinux network]$ cc 14samcli.c
[11ca013@mcalinux network]$ ./a.out
        Enter the port numbetr  : 3326

        Connection Established
         Enter the Domain name : www.rajiv.org

         Equal IP Address : 192.1.1.1

         Enter the Domain name : www.gandhi.in

         Equal IP Address : 192.2.2.2

         Enter the Domain name : www.selva.com


         Equal IP Address : 192.3.3.3

0 Response to "DNS USING SOCKETS"

Post a Comment



Your IP Address is:

Browser: