ADDRESS RESOLUTION PROTOCOL (ARP) 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","-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(ip[i],rmsg)==0)
   {
   strcpy(smsg,mac[i]);
   printf("\nRecv ip address =%s",rmsg);
   printf("\nThe MAC address =%s",mac[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 ip 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("send mac address = %s",rmsg);
}
return 0;
}
OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 10arpser.c
[11ca013@mcalinux network]$ ./a.out

         Enter the Port : 3326

         Accepted
Recv ip address =192.1.1.134
The MAC address =00-01-6C-47-EB-D3
Recv ip address =192.1.1.135
The MAC address =00-01-6C-47-EB-D4
Recv ip address =192.1.1.136
The MAC address =00-01-6C-47-EB-D5
Recv ip address =192.1.1.137
The MAC address =00-01-6C-47-EB-D6

CLIENT

[11ca013@mcalinux network]$ cc 11rarpcli.c
[11ca013@mcalinux network]$ ./a.out

         Enter the Port : 3326

Enter the MAC address  192.1.1.134
The recv IP address = 00-01-6C-47-EB-D3
Enter the MAC address  192.1.1.135
The recv IP address = 00-01-6C-47-EB-D4
Enter the MAC address  192.1.1.136
The recv IP address = 00-01-6C-47-EB-D5
Enter the MAC address  192.1.1.137
The recv IP address = 00-01-6C-47-EB-D6

Enter the MAC address  exit

0 Response to " ADDRESS RESOLUTION PROTOCOL (ARP) USING SOCKETS"

Post a Comment



Your IP Address is:

Browser: