DNS - Domain Name Server - UDP - Networks & DBMS Lab - C Program

Program :

// Client Program : dnsc.c

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
main()
{
    struct sockaddr_in server,client;
    int s,n;
    char b1[100],b2[100];
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=3000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    n=sizeof(server);
    printf("\nEnter canonical address: ");
    scanf("%s",b2);
    sendto(s,b2,sizeof(b2),0,(struct sockaddr *)&server,n);
    recvfrom(s,b1,sizeof(b1), 0,NULL,NULL);
    printf("%s \n",b1);
}

// Server Program : dnss.c

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
main()
{
    FILE *fp;
    struct sockaddr_in server,client;
    int s,n;
    char b1[100],b2[100],a[100];
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=3000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    bind(s,(struct sockaddr *)&server,sizeof(server));
    n=sizeof(client);
    while(1)
    {
        strcpy(b2,"");
        fp=fopen("dns.txt","r");
        recvfrom(s,b1,sizeof b1, 0,(struct sockaddr *)&client,&n);
        while(!feof(fp))
       {
           fscanf(fp,"%s",a);
           if(strcmp(a,b1)==0)
          {
                fscanf(fp,"%s",b2);
                break;
          }
      }
      if(strcmp(b2,"")==0)
     {
           strcpy(b2,"Not found...");
      }
     fclose(fp);
     sendto(s,b2,sizeof b2,0,(struct sockaddr *)&client,n); 
    }
}

/* (DNS.txt)

www.2k8cs.tk  93.170.52.20
www.google.com 192.168.0.7
www.yahoo.com 192.168.0.16
www.2k8618.blogspot.com 66.102.13.191

*/


Output:

Terminal1: (Client)
nn@linuxmint ~ $ gcc dnsc.c -o client
nn@linuxmint ~ $ ./client

Enter canonical address: www.2k8cs.tk
93.170.52.20
nn@linuxmint ~ $ ./client

Enter canonical address: www.2k8618.blogspot.com
66.102.13.191
nn@linuxmint ~ $

Terminal 2: (Server)
nn@linuxmint ~ $ gcc dnss.c -o server
nn@linuxmint ~ $ ./server

2 comments:

  1. small kid programming useless orginal dns queries wont be like that ,

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...