0% found this document useful (0 votes)
9 views38 pages

NWLAB PART B

Computer networks lab manual

Uploaded by

poojachotu22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views38 pages

NWLAB PART B

Computer networks lab manual

Uploaded by

poojachotu22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 38

Program 1: Write a program in C/C++ for error detecting code using

CRC-CCITT(16-bits).

#include<stdio.h>

char m[50],g[50],r[50],q[50],temp[50];

void calrem()
{ int i,j;
for(i=1;i<=16;i++)
r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48));
}

void shiftl()
{ int i;
for(i=1;i<=16;i++) r[i-1]=r[i];
}

void crc(int n)
{
int i,j;
for(i=0;i<n;i++) temp[i]=m[i];
for(i=0;i<n;i++) r[i]=m[i];

for(i=0;i<n-16;i++)
{
if(r[0] == '1')
{
q[i]='1';
calrem();
}
else
{
q[i]='0';
shiftl();
}
r[16]=m[17+i];
r[17]='\0';

for(j=0;j<=17;j++)
temp[j]=r[j];
}

q[n-16]='\0';
}

void caltrans(int n)

Dept of ISE, Dr.Ambedkar Institute of Technology 1


{
int i,k=0;
for(i=n-16;i<n;i++)
m[i]=((int)m[i]-48)^((int)r[k++]-48)+48;
m[i]='\0';
}

int main()
{
int n,i=0;
char ch,flag=0;
printf("Enter the frame bits:");
while((ch=getc(stdin))!='\n')
m[i++]=ch;
n=i;
for(i=0;i<16;i++)
m[n++]='0';
m[n]='\0';
printf("\nMessage after appending 16 zeros:%s",m);
for(i=0;i<=16;i++)
g[i]='0';
g[0]=g[4]=g[11]=g[16]='1';g[17]='\0';
printf("\nGenerator :%s\n",g);
crc(n);
printf("\n\nQuotient:%s",q);
printf("\nThe final remainder is:%s",r);
caltrans(n);
printf("\nTransmitted frame:%s",m);
printf("\n\n Enter transmitted frame which contains %d
bits:",n);
scanf("\n %s",m);
printf("CRC checking\n");
crc(n);
printf("\nRemainder is:%s",r);
for(i=0;i<16;i++)
{
if(r[i] == '0')
{
flag = 0;
continue;
}
else
{
flag = 1;
break;
}
}
if (flag == 1)
printf("\n\nError during transmission\n");
else
printf("\n\nReceived frame is correct\n");
}

Dept of ISE, Dr.Ambedkar Institute of Technology 2


Input/Output:

[root@localhost network]# cc 1.c


[root@localhost network]# ./a.out
Enter the frame bits:1000

Message after appending 16 zeros:10000000000000000000


Generator :10001000000100001

SENDER

intermediate remainder
*********************************

remainder 1 : 00010000001000010
remainder 2 : 00100000010000100
remainder 3 : 01000000100001000
remainder 4 : 1000000100001000

Quotient:1000
The final remainder is:1000000100001000
Transmitted frame:10001000000100001000

Enter transmitted frame which contains 20 bits:1000000100001000

RECEIVER
CRC checking

intermediate remainder
*********************************

remainder 1 : 000100100011000?0
remainder 2 : 00100100011000?00
remainder 3 : 0100100011000?000
remainder 4 : 100100011000?000
Remainder is:100100011000?000

Error during transmission

[root@localhost network]# ./a.out


Enter the frame bits:1000

Message after appending 16 zeros:10000000000000000000


Generator :10001000000100001

SENDER

intermediate remainder
*********************************

Dept of ISE, Dr.Ambedkar Institute of Technology 3


remainder 1 : 00010000001000010
remainder 2 : 00100000010000100
remainder 3 : 01000000100001000
remainder 4 : 1000000100001000

Quotient:1000
The final remainder is:1000000100001000
Transmitted frame:10001000000100001000

Enter transmitted frame which contains 20 bits:10001000000100001000

RECEIVER
CRC checking

intermediate remainder
*********************************

remainder 1 : 00000000000000000
remainder 2 : 00000000000000000
remainder 3 : 00000000000000000
remainder 4 : 0000000000000000
Remainder is:0000000000000000

Received frame is correct

Dept of ISE, Dr.Ambedkar Institute of Technology 4


Program : Write a program in C/C++ for frame sorting techniques
used in buffer.

#include<stdio.h>
struct frame
{
int id;
char data[2];
};

int main()
{
struct frame buff[20],tempf[20],hilpul;
char text[20];
int flag,i,x,j,m,len,Temp,n,nof;
static int k,temp1[20];
printf("Enter The Text\n");
gets(text);
len=strlen(text);
nof=len/2;
if((nof * 2) != len)
nof++;
for(i=1;i<=nof;i++)
{
buff[i].id=i;
for(j=0;j<=1;j++){
buff[i].data[j]=text[k];
k++;}
buff[i].data[j] = '\0';
}
printf("\nSENDER");
printf("\nFragmented Frame\n");

for(i=1;i<=nof;i++){
printf("id=%d\t",buff[i].id);
printf("data=%s\n",buff[i].data);}
for(m=1;m<=nof;m++)
{
set:n=rand()%(nof+1);
for(i=1;i<=nof;i++)
{
if(temp1[i]==n)
{
goto set;
}
}
temp1[m]=n;
tempf[m].id=buff[n].id;
strcpy(tempf[m].data,buff[n].data);

Dept of ISE, Dr.Ambedkar Institute of Technology 5


}
printf("\n\n\nRECEIVER");
printf("\nFragmented frame not in a sequence\n");
for(i=1;i<=nof;i++)
{
printf("id=%d\t",tempf[i].id);
printf("data=%s\n",tempf[i].data);
}
printf(" Reassembling......\n\n");
for(i=1;i<=nof;i++)
{
for(j=i+1;j<=nof;j++)
{ printf("tempf[j].id=%d tempf[i].id=
%d\n",tempf[j].id,tempf[i].id);
if(tempf[j].id<tempf[i].id)
{hilpul=tempf[i];
tempf[i]=tempf[j];

tempf[j]=hilpul;
}
printf("swapped: %d | %d \n",tempf[j].id,tempf[i].id);
sleep(1);
}
}

printf("\nAfter Reassembling the frame\n");

for(i=1;i<=nof;i++)
{
printf("id=%d\t",tempf[i].id);
printf("data=%s\n",tempf[i].data);
}
for(i=1;i<=nof;i++)
printf("%s",tempf[i].data);
printf("\n");
return 0;
}

Dept of ISE, Dr.Ambedkar Institute of Technology 6


Input/Output:

[root@localhost network]# cc 2.c


[root@localhost network]# ./a.out
Enter the Text:
hi how are you

enter the farme length for network :3

SENDER
Fragmented Frame
id=1 data=hi
id=2 data=how
id=3 data= ar
id=4 data=e y
id=5 data=ou

RECEIVER
Fragmented frame not in a sequence
id=1 data=hi
id=4 data=e y
id=3 data= ar
id=5 data=ou
id=2 data=how
Reassembling......

tempf[j].id=4 tempf[i].id= 1
swapped: 4 | 1
tempf[j].id=3 tempf[i].id= 1
swapped: 3 | 1
tempf[j].id=5 tempf[i].id= 1
swapped: 5 | 1
tempf[j].id=2 tempf[i].id= 1
swapped: 2 | 1
tempf[j].id=3 tempf[i].id= 4
swapped: 4 | 3
tempf[j].id=5 tempf[i].id= 3
swapped: 5 | 3
tempf[j].id=2 tempf[i].id= 3
swapped: 3 | 2
tempf[j].id=5 tempf[i].id= 4
swapped: 5 | 4
tempf[j].id=3 tempf[i].id= 4
swapped: 4 | 3
tempf[j].id=4 tempf[i].id= 5
swapped: 5 | 4

After Reassembling the frame


id=1 data=hi

Dept of ISE, Dr.Ambedkar Institute of Technology 7


id=2 data=how
id=3 data= ar
id=4 data=e y
id=5 data=ou
hi how are you

[root@localhost network]#

Dept of ISE, Dr.Ambedkar Institute of Technology 8


Program 2: Write a program for Distance Vector algorithm to find
suitable path for transmission.

#include<stdio.h>
int size;
int main()
{
int l,k=1,mat[10]
[10],neighb,update[10],router,ans=1,j,i,dwn,choice,via[10];

while(ans)
{
printf("\nEnter size of matrix:");
scanf("%d",&size);
printf("\nEnter no. of neighbour nodes:");
scanf("%d",&neighb);
printf("\nEnter router name:");
scanf("%d",&router);
printf("size of matrix=%d\nno. of neighbours =%d\nthe router is=
%d",size,neighb,router);
for(i=1;i<=size;i++)
mat[router][i]=999;
mat[router][router]=0;
printf("\nenter available table entries of neighbours......\n\n");
for(i=1;i<=neighb;i++)
{
printf("enter node neighbour no %d : ",k);
scanf("%d",&update[k]);
printf("enter table values for node %d",update[k]);
for(j=1;j<=size;j++)
{
printf("\n%d | ",j);
scanf("%d",&mat[update[k]][j]);
}
printf("enter weight for %d to %d: ",router,update[k]);
scanf("%d",&mat[router][update[k]]);
k++;
}
k--;
for(i=1;i<=size;i++)
for(j=1;j<=k;j++)
if((mat[router][i]) > (mat[router][update[j]] +
mat[update[j]][i]))
{mat[router][i]=mat[router][update[j]]+mat[update[j]]
[i];
via[i]=update[j];}

printf("table for %d",router);


for(i=1;i<=size;i++)
printf("\n%d | %d | %d",i,mat[router][i],via[i]);

Dept of ISE, Dr.Ambedkar Institute of Technology 9


printf("\ntake values for next exchange 1/0 ?");

scanf("%d",&ans);
k=1;
printf("\nrouter UP/DOWN 1/0 ? : ");
scanf("%d",&choice);
if(choice==0)
{
printf("\nenter the router to make down: ");
scanf("%d",&dwn);
for(l=1;l<=size;l++)
if(via[l]==dwn)
mat[router][l]=999;
}
printf("table for %d",router);
for(i=1;i<=size;i++)
printf("\n%d | %d",i,mat[router][i]);

}
}

Dept of ISE, Dr.Ambedkar Institute of Technology 10


Input/Output:

[root@localhost network]# cc 3.c


[root@localhost network]# ./a.out

Enter size of matrix:4

Enter router name:3

Enter no of neighbour nodes:3


size of matrix=4
no. of neighbours =3
the router is=3
enter available table entries of neighbours......

enter node neighbour no 1 : 2


enter table values for node 2
1 | 10

2 | 0

3 | 20

4 | 25

enter weight for 1 to 2: 10


enter node neighbour no 2 : 4
enter table values for node 4
1 | 15

2 | 25

3 | 30

4 | 0

enter weight for 1 to 4: 10


table for 1
1 | 0 | 0

2 | 10 | 0

3 | 30 | 2

4 | 10 | 0

taken values for next exchange 1/0? 1


router UP/DOWN 1/0? : 0
enter the router to make down : 2
table for 1

Dept of ISE, Dr.Ambedkar Institute of Technology 11


1 | 0

2 | 999

3 | 999

4 | 10

starting new exchange.....

Dept of ISE, Dr.Ambedkar Institute of Technology 12


Program 4: Write a program in C/C++ for spanning tree algorithm
(Kruskal’s/Prim’s) to find loop less path.

Kruskal’s Algorithm

#include <stdio.h>
#include <unistd.h>
#define MAX 100

int parent[MAX],t[MAX][2],cost[MAX][MAX],a[MAX];
int main()
{
int i,j,n;
printf("Enter the number of nodes\n");
scanf("%d",&n);

printf("Enter the cost between nodes\n");

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
}
}
for(i=1;i<=n;i++)
{
parent[i]=0;
}
kruskal(n);
return (0);
}

int find (int v)


{
while(parent[v])
{
v=parent[v];
}
}

void union1(int i,int j)


{
parent[j]=i;
}

int kruskal(int n)
{
int i,j,k,u,v,mincost,res1,res2,sum;

Dept of ISE, Dr.Ambedkar Institute of Technology 13


sum=0;
for(k=1;k<n;k++)
{
mincost=999;
for(i=1;i<n;i++)
{
or(j=1;j<=n;j++)
{
if(i==j)
continue;

if(cost[i][j] < mincost)


{
u=find(i);
v=find(j);
if(u!=v)
{
res1=i;
res2=j;
mincost=cost[i][j];
}
}
}
}
union1(res1,find(res2));
t[k][1]=res1;
t[k][2]=res2;
a[k]=mincost;
sum+=mincost;
}
printf("\n Spanning tree edges \n");
for(i=1;i<n;i++)
{
printf("%d--->%d = %d\n",t[i][1],t[i][2],a[i]);
}
printf("The cost of the minimal spanning tree = %d\n",sum);

Dept of ISE, Dr.Ambedkar Institute of Technology 14


Input/Output:

[root@localhost network]# cc 4.c


[root@localhost network]# ./a.out
Enter the number of nodes
4
Enter the cost between nodes
0 10 999 40
10 0 30 20
999 30 0 999
40 20 999 0

Spanning tree edges


1--->2 = 10
2--->4 = 20
2--->3 = 30
The cost of the minimal spanning tree = 60

Dept of ISE, Dr.Ambedkar Institute of Technology 15


Prim’s algorithm

#include <stdio.h>
#include <unistd.h>
int main()
{
int n,i,j,source;
int cost[10][10],s[10],d[10],ne[10];
printf("Enter the number of nodes\n");
scanf("%d",&n);

printf("Enter the cost between nodes\n");


for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
}
}

printf("Enter the source node\n");


scanf("%d",&source);

for(i=1;i<=n;i++)
{
s[i]=0;
ne[i]=source;
d[i]=cost[source][i];
}
s[source]=1;
prims(n,cost,s,d,ne);
return(0);
}

int prims(int n,int cost[][10],int s[],int d[],int ne[])


{
int i,j,u,v,small,sum,k;
int t[10][2],a[10];
k=1,sum=0;
for(i=1;i<=n-1;i++) {
small=999;u=0;
for(j=1;j<=n;j++){
if(s[j] == 0){
if(d[j] < small){
small=d[j];
u=j;
}
}
}

Dept of ISE, Dr.Ambedkar Institute of Technology 16


t[k][1]=u;
t[k][2]=ne[u];
a[k]=small;
k++;
sum+=cost[u][ne[u]];
s[u]=1;
for(v=1;v<=n;v++)
{
if(s[v]==0)
{
if(cost[u][v]<d[v])
{
d[v]=cost[u][v];
ne[v]=u;
}
}
}
}
if(sum>999)
{
printf("The minimal spanning tree doesn't exists\n");
exit(0);
}
else
{
printf("Spanning tree exist\n");
for(i=1;i<n;i++)
{
printf("%d---->%d = %d \n",t[i][1],t[i][2],a[i]);
}
printf("The cost of the minimal spanning tree = %d\
n",sum);

}
}

Dept of ISE, Dr.Ambedkar Institute of Technology 17


Input/Output:

[root@localhost network]# cc 4b.c


[root@localhost network]# ./a.out
Enter the number of nodes
4
Enter the cost between nodes
0 10 999 40
10 0 30 20
999 30 0 999
40 20 999 0
Enter the source node
4
Spanning tree exist
2---->4 = 20
1---->2 = 10
3---->2 = 30
The cost of the minimal spanning tree = 60

Dept of ISE, Dr.Ambedkar Institute of Technology 18


Program 5: Using TCP/IP sockets,write a client-server program to
make client sending the filename & server to send back contents of
requested file if present.

Socket Client

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>

#define SERV_TCP_PORT 5000


#define SERV_HOST_ADDR "127.0.0.1"
#define BUFLEN 1500

int main(int argc, char *argv[])


{
int sockfd;
struct sockaddr_in serv_addr;
char buf[BUFLEN];

bzero((char*)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDR);
serv_addr.sin_port=htons(SERV_TCP_PORT);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("socket error");

if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)


printf("connect error");

while(1){
printf("Enter the file name\n");
scanf("%s",buf);
write(sockfd,buf,BUFLEN);
printf("File contents\n");
read(sockfd,buf,BUFLEN);
printf("%s\n",buf);
}
close(sockfd);
return(0);
}

Dept of ISE, Dr.Ambedkar Institute of Technology 19


Socket Server

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>

#define SERV_TCP_PORT 5000


#define SERV_HOST_ADDR "127.0.0.1"
#define BUFLEN 1500

int main(int argc,char *argv[])


{

int sockfd,newsockfd,clilen,childpid,fd;
struct sockaddr_in cli_addr,serv_addr;
char buffer[BUFLEN];

if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("socket error");

bzero((char*) &serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(SERV_TCP_PORT);

if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)


printf("Bind error");
while(1){
listen(sockfd,5);
for(;;)
{ // listen(sockfd,5);
printf("server process");
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr
*)&cli_addr,&clilen);
if(newsockfd<0)
printf("server accept error");
else
printf("Server: request accepted \n");

if((childpid=fork())<0)
printf("server:fork error");
else if(childpid==0)
{
close(sockfd);while(1){

Dept of ISE, Dr.Ambedkar Institute of Technology 20


read(newsockfd,buffer,BUFLEN);if(sizeof(buffer)<=1)exit(0);
if((fd=open(buffer,O_RDONLY))<0)
strcpy(buffer,"Server:file does not exist");
read(fd,buffer,BUFLEN);
write(newsockfd,buffer,BUFLEN);

}
close(newsockfd);exit(0);
}}
}}

Dept of ISE, Dr.Ambedkar Institute of Technology 21


Input/Output:

[root@localhost network]# cc 5server.c


[root@localhost network]# cc 5client.c
[root@localhost network]# cc 5server.c -o ser
[root@localhost network]# cc 5client.c -o cli
[root@localhost network]# ./ser
server processServer: request accepted
[root@localhost network]# ./cli&
Enter the file name:1.c
#include<stdio.h>

char m[50],g[50],r[50],q[50],temp[50];
void calrem()
{ int i,j;
for(i=1;i<=16;i++)
r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48;
}

void shiftl()
{ int i;
for(i=1;i<=16;i++) r[i-1]=r[i];
}

[root@localhost network]# cc 5server.c -o ser


[root@localhost network]# cc 5client.c -o cli
[root@localhost network]# ./ser
server processServer: request accepted
[root@localhost network]# ./cli&
Enter the file name:2.c

file does not exists.


[root@localhost network]#

Dept of ISE, Dr.Ambedkar Institute of Technology 22


Program 6: Write a C/C++ program for client server communication
using message queues or FIFO as IPC channels to make client send
name of file & server to send back the contents of requested file.

FIFO

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FIFO1 "fifo1"
#define FIFO2 "fifo2"
#define PERMS 0666

int readfd,writefd;
void client(int,int),server(int,int);

int main()
{
pid_t childpid;

if((mkfifo(FIFO1,PERMS)) < 0)
printf("Can't create 1\n");
if((mkfifo(FIFO2,PERMS)) <0)
printf("Can't create 2\n");

if((childpid = fork()) == 0)
{
readfd= open(FIFO1,O_RDONLY,0);
writefd= open(FIFO2,O_WRONLY,0);
server(readfd,writefd);
close(readfd);
close(writefd);
exit(0);
}
writefd = open(FIFO1,O_WRONLY,0);
readfd = open(FIFO2,O_RDONLY,0);
client(readfd,writefd);
close(readfd);
close(writefd);
unlink(FIFO1);
unlink(FIFO2);
exit(1);
}

void client(int readfd,int writefd)


{
size_t len;
ssize_t n;

Dept of ISE, Dr.Ambedkar Institute of Technology 23


char buff[512];

printf("Enter the Filename\n");


scanf("%s",buff);
write(writefd,buff,strlen(buff));

while ( (n = read(readfd, buff, 512)) > 0)


write(1, buff, n);
}

void server(int readfd,int writefd)


{
int fd;
ssize_t n;
char buff[512];

read(readfd, buff, 512);

if ( (fd = open(buff, O_RDWR)) < 0)


{
strcpy(buff,"File doesnot exist\n");
write(writefd,buff,strlen(buff));
exit(0);
}
else
{
while ( (n = read(fd, buff, 512)) > 0)
write(writefd, buff, n);

}
}

Dept of ISE, Dr.Ambedkar Institute of Technology 24


Input/Output:

[root@localhost network]# g++ 6fifo.c -w


[root@localhost network]# ./a.out
Enter the Filename
1.c
#include<stdio.h>

char m[50],g[50],r[50],q[50],temp[50];
void calrem()
{ int i,j;
for(i=1;i<=16;i++)
r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48;
}

void shiftl()
{ int i;
for(i=1;i<=16;i++) r[i-1]=r[i];
}

[root@localhost network]# g++ 6fifo.c -w


[root@localhost network]# ./a.out
Enter the Filename
2.c

file does not exist.

Dept of ISE, Dr.Ambedkar Institute of Technology 25


Message Queues

//Message server//

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/msg.h>
#include<string.h>

struct mesgq{
long mtype;
char mtext[1500];
};

int msgid;

int main()
{
int fd,nob;char fname[20];
struct mesgq buff1,buff2;
msgid=msgget((key_t)0x1,IPC_CREAT|0666);
printf("\n Server process");
for(;;){
msgrcv(msgid,&buff1,1500,0,0);
printf("\n Filename received :%s",buff1.mtext);
buff2.mtype=buff1.mtype;
fd=open(buff1.mtext,O_RDWR);
if(fd<0)
strcpy(buff2.mtext,"File doesnot exist\n");
else{
nob=lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
read(fd,buff2.mtext,nob);
}
msgsnd(msgid,&buff2,1500,0);
}
return 0;
}

Dept of ISE, Dr.Ambedkar Institute of Technology 26


//Message client//

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/msg.h>
#include<string.h>

struct mesgq{
long mtype;
char mtext[1500];
};

int msgid;

int main()
{

char fname[20];
struct mesgq buff,recvbuff;

printf("Enter the mesg type:");


scanf("%d",&buff.mtype);
printf("\n Enter the Filename");
scanf("%s",buff.mtext);
msgsnd(msgid,&buff,1500,0);
recvbuff.mtype=buff.mtype;
msgrcv(msgid,&recvbuff,1500,buff.mtype,0);
printf("\nThe File contents:\n %s",recvbuff.mtext);

return 0;
}

Dept of ISE, Dr.Ambedkar Institute of Technology 27


Input/Output:

[root@localhost network]# cc 6mesg.c


[root@localhost network]# cc 6mesgcli.c
[root@localhost network]# cc 6mesg.c -o ser
[root@localhost network]# cc mesgcli.c -o cli
[root@localhost network]# ./ser
[root@localhost network]# ./cli&
Enter the Filename
1.c
#include<stdio.h>

char m[50],g[50],r[50],q[50],temp[50];
void calrem()
{ int i,j;
for(i=1;i<=16;i++)
r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48;
}

void shiftl()
{ int i;
for(i=1;i<=16;i++) r[i-1]=r[i];
}

[root@localhost network]# ./a.out


Enter the Filename
2.c

file does not exist.

Dept of ISE, Dr.Ambedkar Institute of Technology 28


Program 7: Write a program in C/C++ for a simple RSA algorithm to
encrypt & decrypt the data.

#include<stdio.h>
#include<math.h>

int c,m;
int mult(unsigned int x, unsigned int y, unsigned int n)
{
unsigned long int k=1;
int j;
for(j=1;j<=y;j++)
k=(k*x)%n;
return (unsigned int)k;
}

int main()
{
rsa();
return 1;

int calcprime(int n)
{
int i,lim,prime=1;
lim=n/2;
if(n==3||n==5)
return 1;
for(i=2;i>=lim;i++)
{
if((n%i)==0)
{
prime=0;
return prime;
}
}
return prime;
}

int calcgcd(int a,int b)


{
int gcd,den,num,rem;
if(a==0)
{
gcd=b;
return gcd;
}
else if(b==0)
{

Dept of ISE, Dr.Ambedkar Institute of Technology 29


gcd=1;
return gcd;
}
if(a>b)
{
num=a;
den=b;
rem=a%b;
}
else
{
num=b;
den=a;
rem=b%a;
}
while(rem!=0)
{
num=den;
den=rem;
rem=num%den;
}
gcd=den;
return gcd;
}

int rsa()
{
int t,p,q,z,d,e,b,n,mv,i,j;
char tmp[25];
int ttmp[25];
printf("\nEnter two prime numbers:");
printf("\nEnter two prime numbers such that their product is less
than 128:");
scanf("%d%d",&p,&q);
if(calcprime(p))
{
if(calcprime(q))
{
n=p*q;
z=(p-1)*(q-1);
for(i=2;i<z;i++)
{
if((calcgcd(z,i))==1)
{
e=i;
printf("\ne-%d\n",e);
break;
}
}
for(j=n;j>=1;j--)
{
if(((e*j)%z)==1)

Dept of ISE, Dr.Ambedkar Institute of Technology 30


{
d=j; //private key
printf("d-%d\n",d);
break;
}
}
printf("\nEnter a text:");
scanf("%s",tmp);
printf("\nEntered data is %s",tmp);
for(i=0;i<strlen(tmp);i++)
{
mv=tmp[i];
c=mult(mv,e,n);
m=mult(c,d,n);
printf("\nThe cypher text for %c=%d\n",tmp[i],c);
ttmp[i]=(int)c;
}
printf("\nThe encoded text is :");
for(i=0;i<strlen(tmp);i++)
printf("'%d' ",ttmp[i]);
}
else
printf("\nq is an invalid prime number");
}
else
printf("\np is an invalid prime number");
printf("\n");
return;
}

Dept of ISE, Dr.Ambedkar Institute of Technology 31


Input/Output:

[root@localhost network]# cc 7.c -w


[root@localhost network]# ./a.out

Enter two prime numbers:


5 7
e=5
d=29
Enter a value less than 35 : 31
The cipher data is : 26
The actual message is : 31

Dept of ISE, Dr.Ambedkar Institute of Technology 32


Program 8: Write a program in C/C++ for Hamming code generation
for error detection/correction.

#include<stdio.h>
#include<math.h>
int main()
{
int a[11]={0,0,0,0,0,0,0,0,0,0,0};
static int r[4];
int pos=0,i,temp[7];

printf("The Enter the data\n");


for(i=0;i<7;i++)
{
scanf("%d",&temp[i]);
}
a[2]=temp[0];
a[4]=temp[1];
a[5]=temp[2];
a[6]=temp[3];
a[8]=temp[4];
a[9]=temp[5];
a[10]=temp[6];
a[0]=(a[0]+a[2]+a[4]+a[6]+a[8]+a[10])%2;
a[1]=(a[1]+a[2]+a[5]+a[6]+a[9]+a[10])%2;
a[3]=(a[3]+a[4]+a[5]+a[6])%2;
a[7]=(a[7]+a[8]+a[9]+a[10])%2;
printf("code with parity\n");
for(i=0;i<=10;i++)
printf("%d ",a[i]);

printf("\n enter the position of error\n");


scanf("%d",&pos);
if(a[pos-1]==0)
a[pos-1]=1;
else
a[pos-1]=0;
printf("\ncode with error\n");
for(i=0;i<=10;i++)
printf("%d ",a[i]);
r[0]=(a[0]+a[2]+a[4]+a[6]+a[8]+a[10])%2;
r[1]=(a[1]+a[2]+a[5]+a[6]+a[9]+a[10])%2;
r[2]=(a[3]+a[4]+a[5]+a[6])%2;
r[3]=(a[7]+a[8]+a[9]+a[10])%2;
printf("\nError position is ");
for(i=3;i>=0;i--)
printf("%d ",r[i]);
pos=(r[3]*8)+(r[2]*4)+(r[1]*2)+(r[0]*1);

printf("\nThe error pos is in %d\n",pos);

Dept of ISE, Dr.Ambedkar Institute of Technology 33


if(a[pos-1]==0)
a[pos-1]=1;
else
a[pos-1]=0;
printf("\nThe corrected code without error\n");
for(i=0;i<=10;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}

Dept of ISE, Dr.Ambedkar Institute of Technology 34


Input/Output:

[root@localhost network]# cc 8.c -w


[root@localhost network]# ./a.out
The Enter the data
0 1 1 0 1 0 1
code with parity
1 0 0 0 1 1 0 0 1 0 1
enter the position of error
6

code with error


1 0 0 0 1 0 0 0 1 0 1
Error position is 0 1 1 0
The error pos is in 6

The corrected code without error


1 0 0 0 1 1 0 0 1 0 1

Dept of ISE, Dr.Ambedkar Institute of Technology 35


Program 9: Write a program in C/C++ for congestion using leaky
bucket algorithm.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
main()
{
int
packets[8],i,j,clk,b_size,o_rate,i_rate,p_remain,p_sz_rm=0,p_sz,p_time,f
lag=0;
system("clear");
for(i=0;i<5;++i)
packets[i]=rand()%10;

printf("\n Enter the output rate:");


scanf("%d",&o_rate);
printf("\n Enter the Bucket Size:");
scanf("%d",&b_size);

for(i=0;i<5;++i)
{

if((packets[i]+p_sz_rm)>b_size)
{
if(packets[i]>b_size)
printf("\n Incoming packet size (%d) is Greater than bucket
capacity - REJECTED",packets[i]);
else
printf("\n Bucket capacity exceeded - REJECTED!!");
}

else
{ p_remain=4-i;
p_sz=packets[i];
p_sz_rm+=p_sz;
printf("\n_________________________\n");
printf("\n Incoming Packet :%d",p_sz);
printf("\n Transmission left:%d",p_sz_rm);

p_time=rand()%10;
printf("\n Next Packet Will come at :%d",p_time);

for(clk=0;clk<p_time && p_sz_rm>0;clk++)


{
printf("\n Time Left:%d",p_time-clk);
sleep(1);
printf(" - Transmitted!!");

Dept of ISE, Dr.Ambedkar Institute of Technology 36


if(p_sz_rm<=o_rate)
p_sz_rm=0;
else
p_sz_rm-=o_rate;

printf(" Bytes Remaining:%d",p_sz_rm);


}

if(p_time-clk>0)
printf(" \n Time left %d No packets to transmit!!",p_time-
clk);
}
}
return(0);
}

Dept of ISE, Dr.Ambedkar Institute of Technology 37


Input/Output:

[root@localhost network]# cc 9.c


[root@localhost network]# ./a.out

Enter the output rate:5


Enter the Bucket Size:4

_________________________

Incoming Packet :3
Transmission left:3
Next Packet Will come at :5
Time Left:5 - Transmitted!! Bytes Remaining:0
Time left 4 No packets to transmit!!
Incoming packet size (6) is Greater than bucket capacity - REJECTED
Incoming packet size (7) is Greater than bucket capacity - REJECTED
Incoming packet size (5) is Greater than bucket capacity - REJECTED
_________________________

Incoming Packet :3
Transmission left:3
Next Packet Will come at :6
Time Left:6 - Transmitted!! Bytes Remaining:0
Time left 5 No packets to transmit!!

[root@localhost network]# ./a.out

Enter the output rate:6

Enter the Bucket Size:3

_________________________

Incoming Packet :3
Transmission left:3
Next Packet Will come at :5
Time Left:5 - Transmitted!! Bytes Remaining:0
Time left 4 No packets to transmit!!
Incoming packet size (6) is Greater than bucket capacity - REJECTED
Incoming packet size (7) is Greater than bucket capacity - REJECTED
Incoming packet size (5) is Greater than bucket capacity - REJECTED
_________________________

Incoming Packet :3
Transmission left:3
Next Packet Will come at :6
Time Left:6 - Transmitted!! Bytes Remaining:
Time left 5 No packets to transmit!!

Dept of ISE, Dr.Ambedkar Institute of Technology 38

You might also like