Arp
Upcoming SlideShare
Loading in...5
×
 

Arp

on

  • 627 views

 

Statistics

Views

Total Views
627
Views on SlideShare
627
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Arp Arp Document Transcript

  • /* SERVER */#include<stdio.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<stdlib.h>#include<unistd.h>#include<string.h>#include<errno.h>#define port 7564int main(){ int sock,con,byte_rec,i=0; char send_data[1024],rec_data[1024]; struct sockaddr_in ser,cli; int sin_size; char ip[5][20]={"127.0.0.1","127.0.0.2","127.0.0.3","127.0.0.4","127.0.0.5"}; char mac[5][20]={"15.8.12.0.0.1","15.8.12.0.0.2","15.8.12.0.0.3","15.8.12.0.0.4","15.8.12.0.0.5"}; //socket creation if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
  • { perror("Socketn"); exit(1);}ser.sin_family=AF_INET;ser.sin_port=htons(port);ser.sin_addr.s_addr=INADDR_ANY;bzero(&(ser.sin_zero),8);//binding the socketif(bind(sock,(struct sockaddr*)&ser,sizeof(struct sockaddr))==-1){ perror("cant bindn"); exit(1);}//listening to clients connection requestif(listen(sock,3)==-1){ perror("listenn"); exit(1);}printf("Waiting n");
  • sin_size=sizeof(struct sockaddr_in);//accepting connection of clientcon=accept(sock,(struct sockaddr*)&cli,&sin_size); printf("Connectedn");while(1){ //receiving data from client byte_rec=recv(con,rec_data,1024,0); rec_data[byte_rec]=0; if(strcmp(rec_data,"end")==0) { //closing socket close(con); close(sock); break; } else printf("Received IP address=%sn",rec_data); for(i=0;i<5;i++) { if(strcmp(ip[i],rec_data)==0) { strcpy(send_data,mac[i]); printf("Mac address %s sent to clientnn",mac[i]);
  • break; } } //sending data to client send(con,send_data,strlen(send_data),0); } return 0;}/*CLIENT*/#include<stdio.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<stdlib.h>#include<unistd.h>#include<string.h>#include<netdb.h>#include<errno.h>#define port 7564
  • int main(){ int i,sock,byte_rec; char send_data[1024],rec_data[1024];char ip[5][20]={"127.0.0.1","127.0.0.2","127.0.0.3","127.0.0.4","127.0.0.5"}; struct hostent *host; struct sockaddr_in ser,cli; host=gethostbyname("127.0.0.1"); //socket creation if((sock=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("Socketn"); exit(1); } printf("socket createdn"); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr= *((struct in_addr *)host->h_addr); bzero(&(ser.sin_zero),8); //connecting to server if(connect(sock,(struct sockaddr*)&ser,sizeof(struct sockaddr))==-1)
  • { perror("connectn"); exit(1);}while(1){ //sending data to server printf("nEnter the IP address : "); scanf("%s",send_data);loop1: for(i=0;i<5;i++) { if((strcmp(ip[i],send_data)==0)||(strcmp(send_data,"end")==0)) goto loop2; } if(strcmp(ip[i],send_data)!=0) { printf("SORRY!! Enter a VALID IP ADDRESS"); printf("nEnter the IP address:n"); scanf("%s",send_data); goto loop1; }
  • loop2: if(strcmp(send_data,"end")!=0) { send(sock,send_data,strlen(send_data),0); } else { send(sock,send_data,strlen(send_data),0); //closing socket close(sock); break; } //receiving data from server byte_rec=recv(sock,rec_data,1024,0); rec_data[byte_rec]=0; printf("Received MAC address = %snn",rec_data); } return 0;}Output[12mcs009@mepcolinux:~/networkslab]cat arpoutput.txt[12mcs009@mepcolinux:~]vi arpserver.c
  • [12mcs009@mepcolinux:~]cc arpserver.c[12mcs009@mepcolinux:~]./a.outWaitingConnectedReceived IP address=127.0.0.5Mac address 15.8.12.0.0.5 sent to client[12mcs009@mepcolinux:~]vi arpclient.c[12mcs009@mepcolinux:~]cc arpclient.c[12mcs009@mepcolinux:~]./a.outsocket createdEnter the IP address : 127.0.0.5Received MAC address = 15.8.12.0.0.5[12mcs009@mepcolinux:~/networkslab]