0% found this document useful (0 votes)
176 views16 pages

CCN Lab Answer

The document contains 5 questions related to network programming concepts. Each question provides code to simulate a different concept: 1. CRC calculation 2. Traditional checksum calculation 3. Byte stuffing and unstuffing 4. Bit stuffing and unstuffing 5. Converting an address between hex, decimal, and binary notations For each question, the code provided implements the relevant algorithm to perform the simulation. Main functions are included to test and demonstrate the algorithms.

Uploaded by

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

CCN Lab Answer

The document contains 5 questions related to network programming concepts. Each question provides code to simulate a different concept: 1. CRC calculation 2. Traditional checksum calculation 3. Byte stuffing and unstuffing 4. Bit stuffing and unstuffing 5. Converting an address between hex, decimal, and binary notations For each question, the code provided implements the relevant algorithm to perform the simulation. Main functions are included to test and demonstrate the algorithms.

Uploaded by

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

Q1. Write a program to simulate the calculation of CRC.

//“crc.h”

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
void crc(char *data, char *data2)
{
int i,j,keylen,msglen;
char input[100], key[30],temp[30],quot[100],rem[30],key1[30];

strcpy(input,data);
strcpy(key,data2);

keylen=strlen(key);
msglen=strlen(input);

printf("\n");
printf("DATA is:: ");

for(i=0;i<msglen;i++)
{
printf("%c",input[i]);
}
printf("\n");

printf("KEY is:: ");

for(i=0;i<keylen;i++)
{
printf("%c",key[i]);
}
printf("\n");
strcpy(key1,key);

for (i=0;i<keylen-1;i++)
input[msglen+i]='0';

for (i=0;i<keylen;i++)
temp[i]=input[i];

for (i=0;i<msglen;i++)
{
quot[i]=temp[0];

if(quot[i]=='0')
for (j=0;j<keylen;j++)
key[j]='0';
else
for (j=0;j<keylen;j++)
key[j]=key1[j];

for (j=keylen-1;j>0;j--)
{
if(temp[j]==key[j])
rem[j-1]='0';
else
rem[j-1]='1';
}

rem[keylen-1]=input[i+keylen];

strcpy(temp,rem);
}

strcpy(rem,temp);

printf("\nQuotient is:: ");

for (i=0;i<msglen;i++)
printf("%c",quot[i]);

printf("\nRemainder is:: ");

for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);

printf("\nFinal data is:: ");


for (i=0;i<msglen;i++)
printf("%c",input[i]);

for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\n");
printf("\n");
}

#include "crc.h"
int main() {

char data[100],key[100];
printf("Enter Data: ");
scanf("%s",data);
printf("Enter Key: ");
scanf("%s",key);
crc(data , key);
return 0;
}
Q2. Write a program to simulate the calculation of traditional checksum.

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

int sender(int b[10],int k)


{
int checksum,sum=0,i;
printf("\n****SENDER****\n");
for(i=0;i<k;i++)
sum+=b[i];
printf("SUM IS: %d",sum);

checksum=~sum;
printf("\nSENDER's CHECKSUM IS:%d",checksum);
return checksum;
}
int receiver(int c[10],int k,int scheck)
{
int checksum,sum=0,i;
printf("\n\n****RECEIVER****\n");
for(i=0;i<k;i++)
sum+=c[i];
printf(" RECEIVER SUM IS:%d",sum);
sum=sum+scheck;
checksum=~sum;
printf("\nRECEIVER's CHECKSUM IS:%d",checksum);
return checksum;
}
int main()
{
int a[10],i,m,scheck,rcheck;
printf("\nENTER SIZE OF THE STRING:");
scanf("%d",&m);
printf("\nENTER THE ELEMENTS OF THE ARRAY:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
scheck=sender(a,m);
rcheck=receiver(a,m,scheck);
if(rcheck==0)
printf("\n\nNO ERROR IN TRANSMISSION\n\n");
else
printf("\n\nERROR DETECTED");
return 0;
}
Q3. Write and test a program that simulates the byte stuffing and byte unstuffing.

//bytedestuff.h

#include<stdio.h>
#include<string.h>
int m=0,n=0,o,p;
char b[50];
char * destuff(char *ia)
{

strcpy(b,ia);
n=0;
for(m=0;m<strlen(a);m++)
{
if(a[m]=='1')
n++;
else
n=0;
if(n==5)
{
m=m+1;
for(o=m;o<strlen(a);o++)

for(p=0;p<8;p++)
{
a[o]=a[o+1+p];

}
a[o]='\0';

m=m-1;
}
}

return a;

}
//bytestuff.h
#include<stdio.h>
#include<string.h>

int i=0,j=0,k,l=0;
char a[500],flag[10];

char * stuff(char *ia, char *iflag){

strcpy(flag,iflag);
strcpy(a,ia);
for(i=0;i<strlen(a);i++)
{
if(a[i]=='1')
j++;
else
j=0;
if(j==5)
{
for(k=strlen(a);k>i;k--)
{
a[k+8]=a[k];
}
for(l=0;l<8;l++)
{
a[i+l+1]=flag[l];

}
}
}

return a;
}

#include"bytestuff.h"
#include"bytedestuff.h"
int main()
{
char flag[10]="********",a[50]="10111110",*stuff_,*destuff_;
printf("\n\n");
printf("Enter data is:: %s\n",a);
stuff_=stuff(a,flag);
printf("Stuff data is:: %s\n",stuff_);
destuff_=destuff(stuff_);
printf("Destuff data is:: %s\n",destuff_);
printf("\n\"*\" represents \"0\"\n\n");
return 0;
}
Q4. Write and test a program that simulates the bit stuffing and bit unstuffing.

// bitdestuff.h
#include<stdio.h>
#include<string.h>

int m=0,n=0,o;
char b[50];
char * destuff(char *ia)
{

strcpy(b,ia);
n=0;
for(m=0;m<strlen(a);m++)
{
if(a[m]=='1')
n++;
else
n=0;
if(n==5)
{
m=m+1;
for(o=m;o<strlen(a);o++)
a[o]=a[o+1];
a[o]='\0';
m=m-1;
}
}

return a;

}
// bitstuff.h

#include<stdio.h>
#include<string.h>

int i=0,j=0,k;
char a[50];

char * stuff(char *ia){

strcpy(a,ia);
for(i=0;i<strlen(a);i++)
{
if(a[i]=='1')
j++;
else
j=0;
if(j==5)
{
for(k=strlen(a);k>i;k--)
{
a[k+1]=a[k];
}
a[i+1]='*';
}
}

return a;
}

#include"bitstuff.h"
#include"bitdestuff.h"
int main()
{
char a[50]="10111110",*stuff_,*destuff_;
printf("\n\n");
printf("Enter data is:: %s\n",a);
stuff_=stuff(a);
printf("Stuff data is:: %s\n",stuff_);
destuff_=destuff(stuff_);
printf("Destuff data is:: %s\n",destuff_);
printf("\n\"*\" represents \"0\"\n\n");
return 0;

}
Q5. Write a program to change an address in any notation to two other notations.

#HEX to DEC to HEX

#!/usr/bin/python
import random

data=[0,0]

for x in range(2):

k=random.randint(0,15)
if k==15:
data[x]='f'
elif k==14:
data[x]='e'
elif k==13:
data[x]='d'
elif k==12:
data[x]='c'
elif k==11:
data[x]='b'
elif k==10:
data[x]='a'
else:
data[x]=k

data=''.join(str(e) for e in data)


print "Generated Hex:: "+data

data2=str(int(data,16))

print "Converted to Decimal:: "+data2

data2=str(hex(int(data2,10)))
data2=list(data2)
del data2[:2]
data3=[0]
if len(data2)==1:
data2=data3+data2
data2=''.join(str(e) for e in data2)
print"Converted to Hexadecimal:: "+ data2
#BIN to DEC to BIN

#!/usr/bin/python
import random
data=[0,0,0,0,0,0,0,0]
for x in range(8):
k=random.randint(0,1)
data[x]=k

data=''.join(str(e) for e in data)


print"Generated binary::"+data
print"Converted to Decimal:: "+str(int(data,2))
data2=int(data,2)
data2=str(bin(data2))
data2=list(data2)
del data2[:2]
data2=''.join(data2)
#print"Conversion to binary(size conflict):: "+ data2

size=len(data2)
data3=[0]
if size<8:
for x in range(7-size):
data3.append(0);
elif size==8:
data3.pop()

data3=''.join(str(e) for e in data3)


#print "data3:: "+ data3
data2=data3+data2
#print "Final data with proper length:: " +data2
print "Converted to Binary Again :: " +data2
Q6. Write a program to find the class of a given address in dotted decimal notation.

#!/usr/bin/python
import random
data=[]
for x in range(4):
gen=random.randint(0,255)
data.append(gen)
data1=[]
for x in range(4):
k=bin(data[x])
k=list(k)
if len(k)<10:
for x in range(10-len(k)):
k.insert(2+x,0);
k="".join(str(e) for e in k)
data1.append(k[2:])
data1=".".join(data1)

data=".".join(str(f) for f in data)


print "Generated IP address is(decimal):: " + data
print "Generated IP address is(binary):: " + data1

if data1[0]=='0':
print("Class A")
if data1[0:2]=='10':
print("Class B")
if data1[0:3]=='110':
print("Class C")
if data1[0:4]=='1110':
print("Class D")
if data1[0:4]=='1111':
print("Class E")
Q7. Write a program to find the first and the last address in a block given any
addresses in the block and assuming classless addressing.

#!/usr/bin/python
import random
data=[]
for x in range(4):
gen=random.randint(0,255)
data.append(gen)
data1=[]
for x in range(4):
k=bin(data[x])
k=list(k)
if len(k)<10:
for x in range(10-len(k)):
k.insert(2+x,0);
k="".join(str(e) for e in k)
data1.append(k[2:])
data1=".".join(data1)

data=".".join(str(f) for f in data)


print "Generated IP address is(decimal):: " + data
print "Generated IP address is(binary):: " + data1

if data1[0]=='0':
print("Class A")
if data1[0:2]=='10':
print("Class B")
if data1[0:3]=='110':
print("Class C")
if data1[0:4]=='1110':
print("Class D")
if data1[0:4]=='1111':
print("Class E")
Q8. Write a program to simulate the distance-vector algorithm.

#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the nodes
printf("\nEnter the cost matrix :\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//initialise the distance equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose arbitary vertex k and we calculate the direct
distance from the node i to k using the cost matrix
//and add the distance from k to node j
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculate the minimum distance
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<nodes;i++)
{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
{
printf("\t\nnode %d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}}
printf("\n\n"); return 0; }
Q9. Write a program to simulate the link-state algorithm.

#include <stdio.h>
#include <string.h>
int main()
{
int count,src_router,i,j,k,w,v,min;
int cost_matrix[100][100],dist[100],last[100];
int flag[100];
printf("\n Enter the no of routers:");
scanf("%d",&count);
printf("\n Enter the cost matrix values:");
for(i=0;i<count;i++)
{
for(j=0;j<count;j++)
{
printf("\n%d->%d:",i,j);
scanf("%d",&cost_matrix[i][j]);
if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000;
}
}
printf("\n Enter the source router:");
scanf("%d",&src_router);
for(v=0;v<count;v++)
{
flag[v]=0;
last[v]=src_router;
dist[v]=cost_matrix[src_router][v];
}
flag[src_router]=1;
for(i=0;i<count;i++)
{
min=1000;
for(w=0;w<count;w++)
{
if(!flag[w])
if(dist[w]<min)
{
v=w;
min=dist[w];
}
}
flag[v]=1;
for(w=0;w<count;w++)
{
if(!flag[w])
if(min+cost_matrix[v][w]<dist[w])
{
dist[w]=min+cost_matrix[v][w];
last[w]=v;
}
}
}
for(i=0;i<count;i++)
{
printf("\n%d==>%d:Path taken:%d",src_router,i,i);
w=i;
while(w!=src_router)
{
printf("\n<--%d",last[w]);w=last[w];
}
printf("\n Shortest path cost:%d\n",dist[i]);
}

return 0;
}

Q10. Write a program to simulate the path-vector algorithm.

#include <stdio.h>
#include<conio.h>
int main()
{
int n;
int i,j,k;
int a[10][10],b[10][10];
printf("\n Enter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter the distance between the host %d - %d:",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
{
a[i][j]=a[i][k]+a[k][j];
}
}
}
}
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;
}
}
}
printf("\n The output matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
return 0;
}

Q11. Write a TCP client-server program


#client
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip=socket.gethostbyname(socket.gethostname())
port=3500
address=(ip,port)
client.connect(address)
while True:
data=raw_input()
client.send(data)
print client.recv(1024)
if data=="disconnect":
break
#server
import socket
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip=socket.gethostbyname(socket.gethostname())
port=3500
address=(ip,port)
server.bind(address)
server.listen(1)
print"[*] Start Listening::",ip,"/",port
client,addr=server.accept()
print"[*] Got a connection from::",addr[0],"/",addr[1]
while True:
data=client.recv(1024)
print"[*] Received::",data
if data=="Hello":
client.send("Hello Client")
elif data=="disconnect":
client.send("Goodbye :)")
client.close()
break
else:
client.send("we can only response to Hello and disconnect")

Q12. Write a UDP client-server program.


#client
import socket

client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
ip=socket.gethostbyname(socket.gethostname())
port=4500
address=(ip,port)
client.sendto("this is the client",address)

#server

import socket

server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
ip=socket.gethostbyname(socket.gethostname())
port=4500
address=(ip,port)
server.bind(address)
data,addr=server.recvfrom(1024)
print data
print addr

You might also like