Sunday, 20 March 2011

NPM- 6th ex

6th ex:-


PROGRAM:

#include<netdb.h>
#include<sys/socket.h>
int main()
{
 char hname[50],ptr[40],**pptr,*ptr1,ipadd[40];
 char str[128];
 struct hostent *hptr,*dptr;
 size_t l;
 struct in_addr sa;
 int ch;
 while(ch!=4)
 {
  printf("Select one DNS operation\n");
  printf("1.Get-Host-Name\n");
  printf("2.Get-Host-by-Name\n");
  printf("3.Get-Host-by-Address\n");
  printf("4.Exit\n");
  printf("Enter your choice :  ");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    gethostname(hname,l);
    printf("Host name: %s\n",hname);
   break;
   case 2:
    printf("Enter the host name :  ");
    scanf("%s",ptr);
    if((hptr=gethostbyname(ptr))==NULL)
    perror("Error");
    printf("Official Name : %s\n",hptr->h_name);
    printf("Type %d\n",hptr->h_addrtype);
    pptr=hptr->h_addr_list;
    for(pptr;*pptr!=NULL;*pptr++)
    printf("Address : %s\n",inet_ntop(hptr->h_addrtype,*pptr,str,sizeof(str)));
    for(pptr=hptr->h_aliases;*pptr!=NULL;*pptr++)
    printf("the name is %s \n",*pptr);
   break;
   case 3:
    printf("Enter the IP address :  ");
    scanf("%s",ipadd);
    inet_pton(AF_INET,ipadd,&sa.s_addr);
    dptr=gethostbyaddr(&sa.s_addr,4,AF_INET);
    printf("Host Name : %s\n",dptr->h_name);
   break;
   case 4:
   exit(0);
  }
 }
}

OUTPUT:

[it86091@vecit ~]$ cc program6.c
[it86091@vecit ~]$ ./a.out

Select one DNS operation
1.Get-Host-Name
2.Get-Host-by-Name
3.Get-Host-by-Address
4.Exit
Enter your choice :  1
Host name: vecit

Select one DNS operation
1.Get-Host-Name
2.Get-Host-by-Name
3.Get-Host-by-Address
4.Exit
Enter your choice :  2
Enter the host name :  vecit
Official Name : vecit
Type 2
Address : 127.0.0.1
the name is localhost.localdomain
the name is localhost

Select one DNS operation
1.Get-Host-Name
2.Get-Host-by-Name
3.Get-Host-by-Address
4.Exit
Enter your choice :  3
Enter the IP address :  127.0.0.1
Host Name : vecit

Select one DNS operation
1.Get-Host-Name
2.Get-Host-by-Name
3.Get-Host-by-Address
4.Exit
Enter your choice :  4
[it86091@vecit ~]$

No comments:

Post a Comment