Vous êtes sur la page 1sur 33

SIMULATION OF ARP/RARP

//Source Code:
#include<stdio.h>
#include<string.h>
struct file
{
char phyadd[30];
char logadd[30];
}f[15];

void rarp(char padd[30]);


void arp(char ladd[30]);
FILE *fp;
int i,n;
int main()
{
int ch;
char padd[30],ladd[30];
fp=fopen("data.txt","r");
while(!feof(fp))
{
fscanf(fp,"%s%s",f[i].logadd,f[i].phyadd);
i++;
}
fclose(fp);
n=i;
do
{
printf("1.ARP\t2.RARP\t3.EXIT");
printf("\nenter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the logical address: ");
scanf("%s",ladd);
arp(ladd);
break;
case 2:
printf("\nEnetr the physical address: ");
scanf("%s",padd);
rarp(padd);
break;
case 3:
exit(0);
return;
}
}
while(ch!=3);
}
void rarp(char padd[30])
{
for(i=0;i<n;i++)
{
if(!strcmp(padd,f[i].phyadd))
{
printf("\nThe corresponding logical address: %s\n",f[i].logadd);
break;
}
}
if(i==n)
{
printf("\nErrorr address not found");
}
}
void arp(char ladd[30])
{
for(i=0;i<n;i++)
{
if(!strcmp(ladd,f[i].logadd))
{
printf("\nthe corresponding physical address is: %s\n",f[i].phyadd);
break;
}
}
if(i==n)
{
printf("\nerror address not found");
}
}
File name (Data1.txt)

1000 aaa-111
2000 bbb-111
3000 ccc-111

OUTPUT:

"arpgan.c" 78L, 1061C written


[sdl01@localhost vidyasekar]$ cc arpgan.c
[sdl01@localhost vidyasekar]$ ./a.out
1.ARP 2.RARP 3.EXIT
enter your choice: 1
Enter the logical address: 1000
the corresponding physical address is: aaa-111

1.ARP 2.RARP 3.EXIT


enter your choice: 2
Enter the physical address: aaa-111
The corresponding logical address: 1000

1.ARP 2.RARP 3.EXIT


enter your choice: 2
Enetr the physical address: 5000
Errorr address not found

1.ARP 2.RARP 3.EXIT


enter your choice: 3
[sdl01@localhost vidyasekar]$
CYCLIC REDUDANCY CHECK

//Source Code:
#include<stdio.h>
#include<string.h>
main()
{
int data1[25],div[10],rem[25],a[25],n,m,x;
int i,j,k,h,q=0,l,f=0;
printf("CRC Generator");
printf("enter the size of the data");
scanf("%d",&n);
printf("Enter the data in binary");
for(i=0;i<n;i++)
{
scanf("%d",&data1[i]);
}
printf("Enter the size of the divisor");
scanf("%d",&m);
printf("Enter the divisor in binary");
for(i=0;i<m;i++)
{
scanf("%d",&div[i]);
}
h=n;
for(i=0;i<m-1;i++)
{
data1[h++]=0;
}
for(i=0;i<m;i++)
{
if(data1[i]==div[i])
{
rem[i]=0;
}
else
rem[i]=1;
}
for(k=m;k<(n+m-1);k++)
{
if(rem[1]==1)
{
rem[m]=data1[k];
for(l=1;l<=m;l++)
{
if(rem[l]==div[l-1])
{
rem[l-1]=0;
}
else
rem[l-1]=1;
}
}
else
{
rem[m]=data1[k];
rem[m+1]=data1[k+1];
for(h=2;h<=m+1;h++)
{
if(rem[h]==div[h-2])
{
rem[h-2]=0;
}
else
rem[h-2]=1;
}
k++;
}
}
printf("The generated crc is ");
for(j=1;j<m;j++)
{
printf("%d",rem[j]);
}
printf("\nenter the size of crc");
scanf("%d",&x);
printf("\n enter the communicated crc \n");
for(i=0;i<x;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=x;i++)
{
if(a[i-1]!=rem[i])
{
q=1;
}
}
if(q==1)
{
printf("\nError");
}
else
{
for(i=0;i<m-1;i++)
{
data1[h++]=rem[i+1];
}
for(i=0;i<m;i++)
{
if(data1[i]==div[i])
{
rem[i]=0;
}
else
rem[i]=1;
}
for(k=m;k<(n+m-1);k++)
{
if(rem[1]==1)
{
rem[m]=data1[k];
for(l=1;l<=m;l++)
{
if(rem[l]==div[l-1])
{
rem[l-1]=0;
}
else
rem[l-1]=1;
}
}
else
{
rem[m]=data1[k];
rem[m+1]=data1[k+1];
for(h=2;h<=m+1;h++)
{
if(rem[h]==div[h-2])
{
rem[h-2]=0;
}
else
rem[h-2]=1;
}
k++;
}
}
printf("\nthe remainder in checker is");
for(j=1;j<m;j++)
{
printf("%d",rem[j]);
}
for(i=1;i<m;i++)
{
if(rem[i]==1)
{
f=1;
printf("\nError\n");
}
}
if(f==0)
{
printf("\nNo Error");
}
}
}
OUTPUT:

[sdl01@localhost sap5]$ cc crc.c


[sdl01@localhost sap5]$ ./a.out
CRC Generatorenter the size of the data6
Enter the data in binary1
0
0
1
0
0
Enter the size of the divisor4
Enter the divisor in binary1
1
0
1
The generated crc is 001
Enter the size of crc3
Enter the communicated crc
0
0
1

The remainder in checker is000


No Error

[sdl01@localhost sap5]$ ./a.out


Enter the size of the data6
Enter the data in binary1
1
0
0
1
1

Enter the size of divisor4


Enter the divisor in binary1
0
0
1
The generated CRC is:000
Enter the size of CRC:3

Enter the the communicated CRC:


0
0
0
The remainder in checker is101
Error
BIT STUFFING

//Source Code:
#include<stdio.h>
//#include<conio.h>
int main()
{
int count=0;
char c;
FILE *f1,*f2;
//clrscr();
f1=fopen("sr41.c","r");
f2=fopen("sr42.c","w");
while((c=getc(f1))!=EOF)
{
if(c=='1')
count=count+1;
if(c=='0')
count=0;
fputc(c,f2);
if(count>=5)
{
count=0;
fputc('0',f2);
}
}
fclose(f1);
fclose(f2);
f1=fopen("sr41.c","r");
f2=fopen("sr42.c","r");
printf("FILENAME:sr42.c");
printf("\nBefore BitStuffing \n");
while((c=getc(f1))!=EOF)
{
printf("%c",c);
}
printf("\nAfter BITSTUFFING:\n");
while((c=getc(f2))!=EOF)
{
printf("%c",c);
}
fclose(f1);
fclose(f2);
//getch();
return 1;
}
File name (sr41.c)
111111111111111111111111111111

OUTPUT:

FILENAME:sr42.c
Before BitStuffing:
111111111111111111111111111111

After BITSTUFFING:
111110111110111110111110111110111110
TCP CHAT SERVER

//Source Code:
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,MAX);
read(sockfd,buff,sizeof(buff));
printf("From client:%s\t Toclient:",buff);
bzero(buff,MAX);
n=0;
while((buff[n++]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
if(strncmp("exit",buff,4)==0)
{
printf("server exit ..\n");
break;
}
}
}

int main()
{
int sockfd,connfd,len;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed");
exit(0);
}
else
printf("socket successfully created");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA*)&servaddr,sizeof(servaddr)))!=0)
{
printf("socket bind failed");
exit(0);
}
else
printf("socket successfully created");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA*)&servaddr,sizeof(servaddr)))!=0)
{
printf("socket bind failed");
exit(0);
}
else
printf("spocket successfully binded..\n");
if((listen(sockfd,5))!=0)
{
printf("listen failed\n");
exit(0);
}
else
printf("server listening..\n");
len=sizeof(cli);
connfd=accept(sockfd,(SA*)&cli,&len);
if(connfd<0)
{
printf("server accepted failed...\n");
exit(0);
}
else
printf("server accept client ..\n");
func(connfd);
close(sockfd);
}
TCP CHAT CLIENT

//Source code
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,sizeof(buff));
printf("entewr the string");
n=0;
while((buff[n++]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff));
printf("From server%s",buff);
if((strncmp(buff,"exit",4))==0)
{
printf("client exit");
break;
}
}
}
int main()
{
int sockfd,connfd;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed../n");
exit(0);
}
else
printf("socket successfully created../n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))!=0)
{
printf("connection with server failed");
exit(0);
}
else
printf("connection to hte server..\n");
func(sockfd);
close(sockfd);
}
OUTPUT:

TCP CHAT SERVER:

[sdl01@localhost ~]$ cc chatserver.c


[sdl01@localhost ~]$ ./a.out
socket successfully created
socket successfully binded..
server listening..
server accept client ..
From client: hello
Toclient: how are you
From client: i am fine
Toclient:ok thank you
From client: exit
Toclient:exit
server exit ..
[sdl01@localhost ~]$

TCP CHAT CLIENT

[sdl01@localhost ~]$ cc chatclient.c


[sdl01@localhost ~]$ ./a.out
socket successfully created.
connection to the server..
enter the string hello
From server how are you
enter the string i am fine
From serverok thank you
enter the string exit
From serverexit
client exit
[sdl01@localhost ~]$
TCP ECHO SERVER

//Source code
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
FILE *fp;
bzero(buff,MAX);
read(sockfd,buff,sizeof(buff));
printf("From client:%s\n",buff);
fp=fopen("sfile","a");
fputs(buff,fp);
close(fp);
}

int main()
{
int sockfd,connfd,len;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed");
exit(0);
}
else
printf("socket successfully created");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA*)&servaddr,sizeof(servaddr)))!=0)
{
printf("socket bind failed");
exit(0);
}
else
printf("spocket successfully binded..\n");
if((listen(sockfd,5))!=0)
{
printf("listen failed\n");
exit(0);
}
else
printf("server listening..\n");
len=sizeof(cli);
connfd=accept(sockfd,(SA*)&cli,&len);
if(connfd<0)
{
printf("server accepted failed...\n");
exit(0);
}
else
printf("server accept client ..\n");
func(connfd);
close(sockfd);
}
TCP CHAT CLIENT

//Source code
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,sizeof(buff));
printf("entewr the string");
n=0;
while((buff[n++]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff));
if((strncmp(buff,"exit",4))==0)
{
printf("client exit");
break;
}
}
}
int main()
{
int sockfd,connfd;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed../n");
exit(0);
}
else
printf("socket successfully created../n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))!=0)
{
printf("connection with server failed");
exit(0);
}
else
printf("connection to hte server..\n");
func(sockfd);
close(sockfd);
}
OUTPUT:

TCP ECHO SERVER:

[sdl01@localhost ~]$ cc echoserver.c


[sdl01@localhost ~]$ ./a.out
socket successfully created
socket successfully binded..
server listening..
server accept client ..
From client: hello
From client:how are you

TCP ECHO CLIENT

[sdl01@localhost ~]$ cc echoclient.c


[sdl01@localhost ~]$ ./a.out
socket successfully created.
connection to the server..
enter the string hello
from server hello
enter the string how are you
From server how are you
enter the string

File Name Sfile


Hello
How are you
DOMAIN NAME SYSTEM

//Source code
#include<stdio.h>
#include<string.h>
struct file
{
char domain[30];
char address[30];
}f[15];
FILE *fp;
int i,n;
int main()
{
char name[25];
fp=fopen("data301.txt","r");
{
while(!feof(fp))
{
fscanf(fp,"%s%s",f[i].domain,f[i].address);
i++;
}
fclose(fp);
n=i;
printf("Enter the domain name");
scanf("%s",&name);
for(i=0;i<n;i++)
{

if(!strcmp(name,f[i].domain))
{
printf("coresponding ip address is:%s\n",f[i].address);
break;
}
}
if(i=n)
{
printf("erorr:address not foud");
}
}
}
OUTPUT

[sdl01@localhost qwer]$ ./a.out


Enter the domain namewww.yahoo.com
coresponding ip address is:172.182.25.35
[sdl01@localhost qwer]$

File name Data301.txt

www.yahoo.com 172.182.25.35
www.google.com 123.45.65.78
www.rediff.com 456.65.76.89
HYPER TEXT TRANSFER PROTOCOL

//Source code
#include<stdio.h>
int main()
{
char domain[40];
FILE *f1;
int found;
char c;
printf("\n\n Enter the domain where are you");
scanf("%s",domain);
if(strcmp(domain,"www.google.com")==0)
{
f1=fopen("google301.txt","r");
while((c=getc(f1))!=EOF)
{
printf("%c",c);
}
fclose(f1);
}
else if(strcmp(domain,"www.yahoo.com")==0)
{
f1=fopen("yahoo.txt","r");
while((c=getc(f1))!=EOF)
{
printf("%c",c);
}
fclose(f1);
}
else if(strcmp(domain,"www.rediff.com")==0)
{
f1=fopen("rediff.txt","r");
while((c=getc(f1))!=EOF)
{
printf("%c",c);
}
fclose(f1);
}
else
printf("page not found");

}
OUTPUT

[sdl01@localhost qwer]$ ./a.out


Enter the domain where are youwww.google.com
welcome to google

File name google301.txt

Welcome to google

File name yahoo.txt

Welcome to yahoo

File name rediff.txt

Welcome to rediff
SLIDING WINDOW PROTOCOL

//Source code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void getdata()
{
FILE *f1;
char c;
clrscr();
printf("\n\t\tEnter the data\n\t\t");
f1=fopen("sc.txt", "w");
while((c=getchar())!='\n')
putc(c, f1);
putc('\n', f1);
fclose(f1);
}

void send()
{
FILE *f1, *f2;
char c;
int i, k=0, windowsize=2, randno, k1;
f1=fopen("sc.txt", "r");
f2=fopen("ms.txt", "w");
loop:
for(i=0; i<windowsize;i++)
{
c=getc(f1);
if(c!='\n')
{
printf("%c\n", c);
putc(c, f2);
k++;
}
else
goto end;
}
fclose(f1);
randomize();
randno=random(k);
f1=fopen("sc.txt", "a");
putc(randno, f1);
fclose(f1);
k1=randno;
printf("%d\n", k);
getch();
f1=fopen("sc.txt", "r");
fseek(f1, k, SEEK_SET);
goto loop;
end:
fclose(f1);
fclose(f2);
}

void disp()
{
int choice;
printf("\n\tChoose an option\n");
printf("\n\t1.Send");
printf("\n\t2.Quit");
scanf("%d", &choice);
if(choice==1)
send();
}

void main()
{
clrscr();
getdata();
disp();
getch();
}
OUTPUT

Enter the data


computer

Choose an option

1.Send
2.Quit1

c
o
2
m
p
4
u
t
6
e
r
8

File name (sc.txt)


computer

File name (ms.txt)


computer
SIMULATION OF BGP/OSPF ROUTING PROTOCOL

//Source code
#include<stdio.h>
int main()
{
int i, j, k, n, a[10][10], b[10][10];
printf("\n\n\nEnter the no. of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("Enter the distance between the host%d%d", i, j);
scanf("%d", &a[i][j]);
}
else
{
a[i][j]=0;
}
b[i][j]=a[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d", a[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[i][j];
if(i==j)
{
b[i][j]=0;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if(i!=j)
if(a[i][j]>a[i][k]+a[k][j])
b[i][j]=a[i][k]+a[k][j];
}
}
}
printf("\n");
printf("Output matrix");
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", b[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT

Enter the no. of nodes3


Enter the distance between the host01 7
Enter the distance between the host02 5
Enter the distance between the host10 6
Enter the distance between the host12 10
Enter the distance between the host20 7
Enter the distance between the host21 20
0 7 5
6 0 10
7 20 0

Output matrix
0 7 5
6 0 10
7 14 0
UDP CHAT SERVER

//Source code
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#define MAX 100
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n,clen;
struct sockaddr_in cli;
clen=sizeof(cli);
for(;;)
{
bzero(buff,MAX);
recvfrom(sockfd,buff,sizeof(buff),0,(SA*)&cli,&clen);
printf("\tFrom Client: %s\t To Client: ",buff);
bzero(buff,MAX);
n=0;
while((buff[n++]=getchar())!='\n');
sendto(sockfd,buff,sizeof(buff),0,(SA*)&cli,clen);
if(strncmp("exit",buff,4)==0)
{
printf("\nServer Exit..\n");
break;
}
}
}
int main()
{
int sockfd;
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
{
printf("\nSocket Creation Failed..\n");
exit(0);
}
else
printf("\nSocket Successfully Created...\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA*)&servaddr,sizeof(servaddr)))!=0)
{
printf("\nSocket Bind Failed...\n");
exit(0);
}
else
printf("\nSocket Successfully Binded...\n");
func(sockfd);
close(sockfd);
}
UDP CHAT CLIENT

//Source code
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#define MAX 100
#define PORT 43454
#define SA struct sockaddr
int main()
{
char buff[MAX];
int sockfd,len,n;
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
{
printf("\nSocket Creation Failed...\n");
exit(0);
}
else
printf("\nSocket Successfully Created....\n");
bzero(&servaddr,sizeof(len));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
len=sizeof(servaddr);
for(;;)
{
printf("\nEnter String: ");
n=0;
while((buff[n++]=getchar())!='\n');
sendto(sockfd,buff,sizeof(buff),0,(SA*)&servaddr,len);
bzero(buff,sizeof(buff));
recvfrom(sockfd,buff,sizeof(buff),0,(SA*)&servaddr,&len);
printf("\nFrom Server: %s\n",buff);
if(strncmp("exit",buff,4)==0)
{
printf("\nClient Exit...\n");
break;
}
}
close(sockfd);
}
OUTPUT

UDP CHAT SERVER:


Socket Successfully Created...

Socket Successfully Binded...


From Client: Hi,How are You?
To Client: Fine, How About You?
From Client: Fine friend
out You?
To Client: good bye
From Client: bye
Bye
To Client: exit

Server Exit..

UDP CHAT CLIENT:


Socket Successfully Created....

Enter String: Hi,How are You?

From Server: Fine, How About You?

Enter String: Fine friend

From Server: good bye

Enter String: bye Bye

Client Exit...

Vous aimerez peut-être aussi