REVERSE ADDRESS RESOLUTION PROTOCOL (RARP) USING SOCKETS

Server program

#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
 int sd,sd1,i,clilen,byterecv,sport;
 struct sockaddr_in ser,cli;
 char smsg[1024],rmsg[1024];
char ip[5][50]={"192.1.1.134","192.1.1.135","192.1.1.136","192.1.1.137",
"192.1..138"};
char mac[5][50]={"00-01-6C-47-EB-D3","00-01-6C-47-EB-D4",
"00-01-6C-47-EB-D5","00-01-6C-47-EB-D6","00-01-6C-47-EB-D7"};

printf("\n\t Enter the Port : ");
scanf("%d",&sport);
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  printf("\n\t Error in socket");
  return 0;
}
bzero(&ser,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(sport);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sd,(struct sockaddr*)&ser,sizeof(ser)) <0)
{
  printf("\n\t Error in bind");
  return 0;
}
if(listen(sd,5) <0)
{
 printf("\n\t Error in Listen");
 return 0;
}

clilen=sizeof(cli);
if((sd1=accept(sd,(struct sockaddr*)&cli,&clilen)) <0)
{
 printf("\n\t Error in Accept");
 return 0;
}

 printf("\n\t Accepted ");
  while(1)
 {
  byterecv=recv(sd1,rmsg,1024,0);
  rmsg[byterecv]='\0';
  if(strcmp(rmsg,"exit")==0)
 {
  close(sd1);
  close(sd);
break;
}
else
for(i=0;i<5;i++)
 {
  if(strcmp(mac[i],rmsg)==0)
   {
   strcpy(smsg,ip[i]);
   printf("\nRecv MAC address =%s",rmsg);
   printf("\nThe IP address =%s",ip[i]);
   break;
   }
 }
 send(sd1,smsg,strlen(smsg),0);
  }
 printf("\n");
 return 0;
 }







Client program

#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
 int sd,sd1,i,clilen,byterecv,sport;
 struct sockaddr_in ser,cli;
 char smsg[1024],rmsg[1024];

printf("\n\t Enter the Port : ");
scanf("%d",&sport);
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  printf("\n\t Error in socket");
  return 0;
}
bzero(&ser,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(sport);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(connect(sd,(struct sockaddr*)&ser,sizeof(ser)) <0)
{
  printf("\n\t Error in connect");
  return 0;
}
  while(1)
 {
  printf("\nEnter the MAC address  " );
  scanf("%s",smsg);
  if(strcmp(smsg,"exit")!=0)
 {
  send(sd,smsg,strlen(smsg),0);
 }
 else
 {
 send(sd,smsg,strlen(smsg),0);
 close(sd);
 break;
 }
byterecv=recv(sd,rmsg,1024,0);
rmsg[byterecv]='\0';
printf("The recv IP address = %s",rmsg);
}
return 0;
}
OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 11rarpser.c
[11ca013@mcalinux network]$ ./a.out
         Enter the Port : 3326
         Accepted
Recv MAC address =00-01-6C-47-EB-D3
The IP address =192.1.1.134
Recv MAC address =00-01-6C-47-EB-D4
The IP address =192.1.1.135
Recv MAC address =00-01-6C-47-EB-D5
The IP address =192.1.1.136
Recv MAC address =00-01-6C-47-EB-D6
The IP address =192.1.1.137
Recv MAC address =00-01-6C-47-EB-D7
The IP address =192.1.1.138

CLIENT

[11ca013@mcalinux network]$ cc 11rarpcli.c
[11ca013@mcalinux network]$ ./a.out
         Enter the Port : 3326
Enter the MAC address  00-01-6C-47-EB-D3
The recv IP address = 192.1.1.134
Enter the MAC address  00-01-6C-47-EB-D4
The recv IP address = 192.1.1.135
Enter the MAC address  00-01-6C-47-EB-D5
The recv IP address = 192.1.1.136
Enter the MAC address  00-01-6C-47-EB-D6
The recv IP address = 192.1.1.137
Enter the MAC address  00-01-6C-47-EB-D7
The recv IP address = 192.1.1.138

Enter the MAC address  exit

0 Response to " REVERSE ADDRESS RESOLUTION PROTOCOL (RARP) USING SOCKETS"

Post a Comment



Your IP Address is:

Browser: