SlideShare une entreprise Scribd logo
1  sur  8
/* 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 7564



int 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 socket

if(bind(sock,(struct sockaddr*)&ser,sizeof(struct sockaddr))==-1)

{

    perror("can't bindn");

    exit(1);

}



//listening to clients connection request

if(listen(sock,3)==-1)

{

    perror("listenn");

    exit(1);

}

printf("Waiting n");
sin_size=sizeof(struct sockaddr_in);

//accepting connection of client

con=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.out

Waiting

Connected

Received IP address=127.0.0.5

Mac address 15.8.12.0.0.5 sent to client



[12mcs009@mepcolinux:~]vi arpclient.c

[12mcs009@mepcolinux:~]cc arpclient.c

[12mcs009@mepcolinux:~]./a.out

socket created



Enter the IP address : 127.0.0.5

Received MAC address = 15.8.12.0.0.5




[12mcs009@mepcolinux:~/networkslab]

Contenu connexe

Tendances

Full-Stack JavaScript with Node.js
Full-Stack JavaScript with Node.jsFull-Stack JavaScript with Node.js
Full-Stack JavaScript with Node.js
Michael Lehmann
 

Tendances (20)

Ee
EeEe
Ee
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
Yg byev2e
Yg byev2eYg byev2e
Yg byev2e
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
 
Php arduino
Php arduinoPhp arduino
Php arduino
 
20151224-games
20151224-games20151224-games
20151224-games
 
2016 gunma.web games-and-asm.js
2016 gunma.web games-and-asm.js2016 gunma.web games-and-asm.js
2016 gunma.web games-and-asm.js
 
Introduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy CresineIntroduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy Cresine
 
c++ program for Canteen management
c++ program for Canteen managementc++ program for Canteen management
c++ program for Canteen management
 
Full-Stack JavaScript with Node.js
Full-Stack JavaScript with Node.jsFull-Stack JavaScript with Node.js
Full-Stack JavaScript with Node.js
 
"You shall not pass : anti-debug methodics"
"You shall not pass : anti-debug methodics""You shall not pass : anti-debug methodics"
"You shall not pass : anti-debug methodics"
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureCréer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heure
 
File Handling Program
File Handling ProgramFile Handling Program
File Handling Program
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
PyCon KR 2019 sprint - RustPython by example
PyCon KR 2019 sprint  - RustPython by examplePyCon KR 2019 sprint  - RustPython by example
PyCon KR 2019 sprint - RustPython by example
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲーム
 
プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
 
Institute management
Institute managementInstitute management
Institute management
 
Debugging JavaScript with Chrome
Debugging JavaScript with ChromeDebugging JavaScript with Chrome
Debugging JavaScript with Chrome
 

En vedette

En vedette (7)

Cctv,fds,ids,acs
Cctv,fds,ids,acsCctv,fds,ids,acs
Cctv,fds,ids,acs
 
The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...
 
Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)
 
What's Next in Growth? 2016
What's Next in Growth? 2016What's Next in Growth? 2016
What's Next in Growth? 2016
 
The Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post FormatsThe Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post Formats
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome Economy
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
 

Similaire à Arp

Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
yayaria
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! 
aleks-f
 

Similaire à Arp (20)

UDP.yash
UDP.yashUDP.yash
UDP.yash
 
[4] 아두이노와 인터넷
[4] 아두이노와 인터넷[4] 아두이노와 인터넷
[4] 아두이노와 인터넷
 
tp socket en C.pdf
tp socket en C.pdftp socket en C.pdf
tp socket en C.pdf
 
Winform
WinformWinform
Winform
 
Udp socket programming(Florian)
Udp socket programming(Florian)Udp socket programming(Florian)
Udp socket programming(Florian)
 
Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016
 
Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Codes
CodesCodes
Codes
 
java sockets
 java sockets java sockets
java sockets
 
Assignment7.pdf
Assignment7.pdfAssignment7.pdf
Assignment7.pdf
 
Network security Lab manual
Network security Lab manual Network security Lab manual
Network security Lab manual
 
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingRoberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
Server
ServerServer
Server
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! 
 
Encrypt all transports
Encrypt all transportsEncrypt all transports
Encrypt all transports
 
Tugas 2
Tugas 2Tugas 2
Tugas 2
 
Arduino práctico ethernet
Arduino práctico   ethernetArduino práctico   ethernet
Arduino práctico ethernet
 
nw-lab_dns-server.pdf
nw-lab_dns-server.pdfnw-lab_dns-server.pdf
nw-lab_dns-server.pdf
 

Dernier

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Arp

  • 1. /* 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 7564 int 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)
  • 2. { 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 socket if(bind(sock,(struct sockaddr*)&ser,sizeof(struct sockaddr))==-1) { perror("can't bindn"); exit(1); } //listening to clients connection request if(listen(sock,3)==-1) { perror("listenn"); exit(1); } printf("Waiting n");
  • 3. sin_size=sizeof(struct sockaddr_in); //accepting connection of client con=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]);
  • 4. 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
  • 5. 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)
  • 6. { 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; }
  • 7. 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
  • 8. [12mcs009@mepcolinux:~]cc arpserver.c [12mcs009@mepcolinux:~]./a.out Waiting Connected Received IP address=127.0.0.5 Mac address 15.8.12.0.0.5 sent to client [12mcs009@mepcolinux:~]vi arpclient.c [12mcs009@mepcolinux:~]cc arpclient.c [12mcs009@mepcolinux:~]./a.out socket created Enter the IP address : 127.0.0.5 Received MAC address = 15.8.12.0.0.5 [12mcs009@mepcolinux:~/networkslab]