Sunteți pe pagina 1din 28

ARP/RARP

#include<stdio.h> #include<conio.h> #include<string.h> static char*logical[]={"192.168.3.9","192.168.3.105"}; static char*physical[]={"00.13.21.B4.4A.68","00.0F.FE.A4.91.56"}; int protocol; int k,s,i; char padd[20],ladd[15]; int x,j; void main() { clrscr(); printf("1.RARP\n"); printf("2.ARP\n"); printf("\n ENTER YOUR CHOICE"); scanf("%d",&protocol); switch(protocol) { case 1: printf("YOUR ARE IN RARP\n"); printf("\n ENTER YOUR PHYSICAL ADDRESS:"); scanf("%s",&padd); for(i=0;i<2;i++) { x=strcmp(padd,physical[i]); if(x==0) { printf("\n\n THE LOGICAL ADDRESS IS:"); printf("%s",logical[i]); } } break; case 2: printf("YOUR ARE IN ARP\n"); printf("\n ENTER YOUR LOGICAL ADDRESS:"); scanf("%s",&ladd);

for(j=0;j<2;j++) { x=strcmp(ladd,logical[j]); if(x==0) { printf("\n\n THE PHYSICAL ADDRESS IS:"); printf("%s",physical[i]); } } break; default: printf("\n NO MATCH FOUND"); break; } getch(); }

SAMPLE OUTPUT:
1. RARP 2. ARP ENTER YOUR CHOICE: 1 YOU ARE IN RARP ENTER YOUR PHYSICAL ADDRESS: 00.13.21.B4.4A.68 THE LOGICAL ADDRESS IS: 192.168.3.9 1. RARP 2. ARP ENTER YOUR CHOICE: 2 YOU ARE IN ARP ENTER YOUR LOGICAL ADDRESS: 192.168.3.9 THE PHYSICAL ADDRESS ID: 00.13.21.B4.4A.68.

BITSTUFFING
#include<stdio.h> #include<conio.h> void main() { int input[200],output[200],n,i,k=1,count=0; clrscr(); printf("Enter the total number of bits\n"); scanf("%d",&n); printf("Enter the bits\n"); for(i=1;i<=n;i++) scanf("%d",&input[i]); for(i=1;i<=n;i++) { if(input[i]==1) { count++; if(count==5) { output[k]=1; k++; output[k]=0; count=0; } else output[k]=1; } else { output[k]=0; count=0; } k++; } printf("\nBefore bit stuffing:\n"); for(i=1;i<=n;i++) printf("%d ",input[i]); printf("\nAfter bit stuffing:\n"); for(i=1;i<k;i++) printf("%d ",output[i]); getch(); }
4

SAMPLE OUTPUT:
Enter the total number of bits 7 Enter the bits 1 1 1 1 1 0 1 Before bit stuffing: 1111101 After bit stuffing: 11111001

CYCLIC REDUDANCY CHECK(CRC)


#include<stdio.h> #include<conio.h> void main() { int opt; int a[20],b[20],c[20],d[20],dn,dn1,din,din1,i,k,n; clrscr(); printf("OPTIONS\n 1.Sender side,\n 2.Receiver Side\n"); printf("Enter your Choice"); scanf("%d",&opt); switch(opt) { case 1: printf("Enter number of digits of data"); scanf("%d",&dn); printf("Enter the data one by one"); for(i=1;i<=dn;i++) scanf("%d",&a[i]); printf("Enter the number of digits of divisor"); scanf("%d",&din); printf("Enter the Divisor starting with 1 and end with 1"); for(i=1;i<=din;i++) scanf("%d",&b[i]); din1=din-1; for(i=1;i<=din;i++) { a[dn+i]=0; c[i]=a[i]; } for(k=1;k<=dn;k++) { if(c[1]==0) { for(i=1;i<=din;i++) { d[i]=0; } }

else { for(i=1;i<=din;i++) { d[i]=b[i]; } } for(n=1;n<=din;n++) { if(c[n]==d[n]) c[n-1]=0; else c[n-1]=1; } c[din]=a[din+k]; } printf("The data is:"); for(i=1;i<=dn;i++) printf("%d",a[i]); for(i=1;i<=din1;i++) printf("%d",c[i]); break; case 2: printf("Enter number of digits of data:"); scanf("%d",&dn1); printf("Enter the data one by one:\n"); for(i=1;i<=dn1;i++) scanf("%d",&a[i]); printf("Enter the number of digits of divisor:"); scanf("%d",&din); printf("Enter the Divisor starting with 1 and end with 1:\n"); for(i=1;i<=din;i++) scanf("%d",&b[i]); din1=din-1; dn=dn1-din+1; for(i=1;i<=din;i++) { c[i]=a[i]; }

for(k=1;k<=dn;k++) { if(c[1]==0) { for(i=1;i<=din;i++) { d[i]=0; } } else { for(i=1;i<=din;i++) { d[i]=b[i]; } } for(n=1;n<=din;n++) { if(c[n]==d[n]) { c[n-1]=0; } else { c[n-1]=1; } } c[din]=a[din+k]; } printf("The Remainder is:"); for(i=1;i<=din1;i++) { printf("%d",c[i]); } break; default: printf("Invalid Option"); break; } getch(); }

SAMPLE OUTPUT:
SENDER SIDE: Options: 1.Sender Side 2.Receiver Side. Enter your option: 1 Enter number of digits of data: 6 Enter the data one by one: 1 0 0 1 0 0 Enter number of digits of divisor: 4 Enter the Divisor starting with 1 and end with 1 1 1 0 1 The data is: 100100001.

RECEIVER SIDE: Options: 1. Sender Side 2. Receiver Side. Enter your option: 2 Enter number of digits of data: 9 Enter the data one by one: 1 0 0 1 0 0 0 0 1 Enter number of digits of divisor: 4 Enter the Divisor starting with 1 and end with 1 1 1 0 1 The Remainder is: 000

10

OSPF ALGORITHM
#include<stdio.h> #include<stdlib.h> #include<conio.h> void main() { int graph[15][15],s[15],pathestimate[15],mark[15]; int num_of_vertices,source,i,j,u,predecessor[15]; int count=0; int minimum(int a[],int m[],int k); void printpath(int,int,int[]); clrscr(); printf("\nEnter the number of vertices\n"); scanf("%d",&num_of_vertices); if(num_of_vertices<=0) { printf("\n This is meaningless\n"); exit(1); } printf("\n Enter the adjacent matrix\n"); for(i=1;i<=num_of_vertices;i++) { printf("\nEnter the elements of row%d\n",i); for(j=1;j<=num_of_vertices;j++) { scanf("%d",&graph[i][j]); } } printf("\nEnter the source vertex\n"); scanf("%d",&source); for(j=1;j<=num_of_vertices;j++) { mark[j]=0; pathestimate[j]=999; predecessor[j]=0; } pathestimate[source]=0;

11

while (count<num_of_vertices) { u=minimum(pathestimate,mark,num_of_vertices); s[++count]=u; mark[u]=1; for(i=1;i<=num_of_vertices;i++) { if(graph[u][i]>0) { if(mark[i]!=1) { if(pathestimate[i]>pathestimate[u]+graph[u][i]) { pathestimate[i]=pathestimate[u]+graph[u][i]; predecessor[i]=u; } } } } } for(i=1;i<=num_of_vertices;i++) { printpath(source,i,predecessor); if(pathestimate[i]!=999) printf("->(%d)\n",pathestimate[i]); } getch(); } int minimum(int a[],int m[],int k) { int mi=999; int i,t; for(i=1;i<=k;i++) { if(m[i]!=1) { if(mi>=a[i]) { mi=a[i]; t=i; } } }
12

return t; } void printpath(int x,int i,int p[]) { printf("\n"); if(i==x) { printf("%d",x); } else if(p[i]==0) printf("No path from %d to %d",x,i); else { printpath(x,p[i],p); printf("..%d",i); } }

13

SAMPLE OUTPUT:
Enter the number of vertices 4 Enter the adjacent matrix Enter the elements of row1 1 2 3 4 Enter the elements of row2 9 8 7 6 Enter the elements of row3 4 5 6 7 Enter the elements of row4 1 2 8 9 Enter the source vertex 1 1->(0) 1..2->(2) 1..3->(3) 1..4->(4)

14

SLIDING WINDOW
#include<stdio.h> void main() { int m,k,f,bs,i,b[20],in=0,s,r; printf("Enter the Buffer Size\n"); scanf("%d",&bs); printf("Enter Sliding window size\n"); scanf("%d",&s); k=s; for(i=0;i<bs;i++) { printf("\nSender[%d]:",i); scanf("%d",&b[i]); in=in+1; } printf("\nEnter Starting point of sliding window\n"); scanf("%d",&f); s=s+f; while(bs>f) { if(s<=bs) { printf("\nData in Sliding window\n"); for(i=f;i<s;i++) printf("%d",b[i]); s++; }else { m=bs-k; printf("\nData in Sliding window\n"); for(i=m;i<bs;i++) printf("%d",b[i]); } r=b[f]; printf("\nData Received\nReceiver[%d]:%d",f,r); f++; printf("\nACK %d\n",f); } }

15

SAMPLE OUTPUT:
Enter the Buffer Size 5 Enter Sliding window size 3 Sender[0]:1 Sender[1]:2 Sender[2]:3 Sender[3]:4 Sender[4]:5 Enter Starting point of sliding window 2 Data in Sliding window 345 Data Received Receiver[2]:3 ACK 3 Data in Sliding window 345 Data Received Receiver[3]:4 ACK 4 Data in Sliding window 345 Data Received Receiver[4]:5 ACK 5

16

CHAT USING TCP PROTOCOL


SERVER: #include<iostream.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netinet/in.h> #include<unistd.h> #include<time.h> #include<string.h> int main() { int sockfd,connfd; char str1[50]="\0",str2[50]="\0"; socklen_t len; struct sockaddr_in servaddr,cliaddr; if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) { cout<<"Error in socket creation"; return(0); } bzero(&servaddr,sizeof(struct sockaddr_in)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(6000); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) { cout<<"Binding error"; return(0); } listen(sockfd,100); len=sizeof(cliaddr); if((connfd=accept(sockfd,(struct sockaddr*)&cliaddr,&len))==-1) { cout<<"Accept error"; return(0); } cout<<"connected"<<endl;

17

while(1) { read(connfd,str2,40); cout<<endl<<"Message Received from client:"<<str2<<endl; if(!strcasecmp(str2,"quit")) break; cout<<endl<<"Server message:"; cin.getline(str1,40); write(connfd,str1,40); if(!strcasecmp(str1,"quit")) break; } close(connfd); return(0); }

18

CLIENT: #include<iostream.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> #include<unistd.h> #include<time.h> #include<string.h> int main() { int sockfd; char str1[50]="\0",str2[50]="\0"; socklen_t len; struct sockaddr_in cliaddr; if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) { cout<<"Error in socket creation"; return(0); } bzero((void*)&cliaddr,sizeof(struct sockaddr_in)); cliaddr.sin_family=AF_INET; cliaddr.sin_port=htons(6000); connect(sockfd,(struct sockaddr*)&cliaddr,sizeof(cliaddr)); while(1) { cout<<"Client message:"; cin.getline(str1,40); write(sockfd,str1,40); if(!strcasecmp(str1,"quit")) break; read(sockfd,str2,40); cout<<endl<<"Message Received from server:"<<str2<<endl; if(!strcasecmp(str2,"quit")) break; } close(sockfd); return(0); }

19

SAMPLE OUTPUT:
CLIENT: [ite4046@linuxserver ~]$ g++ client.cpp -o cli [ite4046@linuxserver ~]$ ./cli Client message: Hello! I am your client Message Received from server: Hi! I am your Master Client message: quit [ite4046@linuxserver ~]$ SERVER: [ite4046@linuxserver ~]$ g++ server.cpp -o ser [ite4046@linuxserver ~]$ ./ser connected Message Received from client: Hello! I am your client Server message: Hi! I am your Master Message Received from client: quit [ite4046@linuxserver ~]$

20

CHAT USING UDP PROTOCOL


SERVER: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<errno.h> #include<unistd.h> int main(int argc,char *argv[]) { socklen_t sid,clen; char buffer[1024]; struct sockaddr_in saddr,caddr; int n; if(argc<2) { fprintf(stderr,"ERROR: port is not given \n"); exit(1); } sid=socket(AF_INET,SOCK_DGRAM,0); if(sid<0) perror("socket_create"); bzero((char*)&saddr,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(atoi(argv[1])); saddr.sin_addr.s_addr=INADDR_ANY; if(bind(sid,(struct sockaddr *)&saddr,sizeof(saddr))<0) perror("socket_bind"); clen=sizeof(caddr); bzero(buffer,1024); n=recvfrom(sid,buffer,1023,0,(struct sockaddr*)&caddr,&clen); if(n<0) perror("receive"); printf("The msg is %s", buffer); close(sid); return 0; }

21

CLIENT: #include <netdb.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <errno.h> int main(int argc,char *argv[]) { int sid,n; char buffer[1024]; struct sockaddr_in saddr; struct hostent *hen; if(argc<3) { fprintf(stderr,Error: Host name and port is not given \n); exit(1); } sid=socket(AF_INET,SOCK_DGRAM,0); if(sid<0) perror("Socket create"); hen=gethostbyname(argv[1]); if(hen==NULL) { fprintf(stdout,"Host not found"); exit(1); } saddr.sin_family=AF_INET; saddr.sin_port=htons(atoi(argv[2])); bcopy((char *)hen->h_addr,(char *)&saddr.sin_addr.s_addr,hen->h_length); if(bind(sid,(struct sockaddr *)&saddr,sizeof(saddr))<0) perror("Socket_bind"); printf("Enter the data u want to send : \n"); fgets(buffer,1023,stdin); n=sendto(sid,buffer,1023,0,(struct sockaddr *)&saddr,sizeof(saddr)); if(n<0) perror(Error in send to); close(sid); return(0); }

22

SAMPLE OUTPUT:
CLIENT: [ite4046@linuxserver]$ cc udpclient.c [ite4046@linuxserver]$ cc udpclient.c -o ucli [ite4046@linuxserver]$ ./ucli linuxserver 3927 Enter the data u want to send : Hello, how are you? [ite4046@linuxserver]$ SERVER: [ite4046@linuxserver]$ cc udpserver.c [ite4046@linuxserver]$ cc udpserver.c -o user [ite4046@linuxserver]$ ./user 3927 The msg is Hello, how are you? [ite4046@linuxserver]$

23

FILE TRANSFER PROTOCOL USING TCP


SERVER: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<errno.h> int main(int argc,char *argv[]) { int sid,newsid,clen; char buffer[1024]; struct sockaddr_in saddr,caddr; int n; if(argc<2) fprintf(stderr,"ERROR: port no is not given \n"); sid=socket(AF_INET,SOCK_STREAM,0); if(sid<0) perror("socket"); bzero((char*)&saddr,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(atoi(argv[1])); saddr.sin_addr.s_addr=INADDR_ANY; if(bind(sid,(struct sockaddr*)&saddr,sizeof(saddr))<0) perror("socket_bind"); listen(sid,5); clen=sizeof(caddr); newsid=accept(sid,(struct sockaddr*)&caddr,&clen); if(newsid<0) perror("socket_accept"); bzero(buffer,1024); n=read(newsid,buffer,1023); if(n<0) perror("socket read"); printf("The content of the file : \n %s",buffer); return 0; }

24

CLIENT: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<errno.h> #include<netdb.h> int main(int argc, char *argv[1]) { int sid; FILE *fhandle; struct sockaddr_in saddr; char buffer[1024]; char fname[50]; int n; struct hostent *hen; if(argc<3){ fprintf(stderr,"enter hostname and portnumber"); exit(1);} sid=socket(AF_INET,SOCK_STREAM,0); if(sid<0) perror("socket"); hen=gethostbyname(argv[1]); if(hen==NULL){ printf("Host is not found"); exit(1);} saddr.sin_family=AF_INET; saddr.sin_port=htons(atoi(argv[2])); bcopy((char*)hen->h_addr,(char*)&saddr.sin_addr.s_addr,hen->h_length); if(connect(sid,(struct sockaddr*)&saddr,sizeof(saddr))<0) perror("socket_connect"); printf("\n Enter the file name u want to send :"); scanf("%s",&fname); if((fhandle=fopen(fname,"r"))==NULL){ printf("\n Unable to open file"); return 0; }bzero(buffer,1024); fread(buffer,1023,1,fhandle); n=write(sid,buffer,strlen(buffer)); if(n<0) perror("socket_write"); return 0; }
25

SAMPLE OUTPUT:
CLIENT: [ite4046@linuxserver ~]$ ./ftpclient linuxserver 1289 Enter the file name u want to send: welcome.c [ite4046@linuxserver ~]$ SERVER: [ite4046@linuxserver ~]$ ./ftpserver 1289 The content of the file: #include<stdio.h> void main() { printf(welcome); }

26

DNS
#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<netdb.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> int main(int argc,char *argv[1]) { struct hostent *hen; if(argc!=2) { fprintf(stderr,"Enter the hostname \n"); exit(1); } hen=gethostbyname(argv[1]); if(hen==NULL) { fprintf(stderr,"Host not found \n"); } printf("Hostname is %s \n",hen->h_name); printf("IP address is %s \n",inet_ntoa(*((struct in_addr *)hen->h_addr))); }

27

SAMPLE OUTPUT:
[ite4046@linuxserver ~]$cc dns.c o dn [ite4046@linuxserver ~]$./dn www.gmail.com Host name is googlemail.l.google.com IP address is 209.85.153.83 [ite4046@linuxserver ~] $

28

S-ar putea să vă placă și