CN Lab
CN Lab
Certificate
Certified that this is the bonafide record of the practical work done
Name
in the Laboratory of _
of the Department of
List of Experiments
S.No. Name of the Experiment Date of Page Marks Remarks
Experiment Number
1. Implement the data link layer framing methods such as character, character-stuffing
and bit stuffing.
Bit Stuffing:
#include<stdio.h>
#include<string.h>
int main(){
char s[100];
int c,i,k;
char r[100];
char p[100];
printf("Enter the bitstring :");
scanf("%s",s);
k=0;
c=0;
for(i=strlen(s) -1 ;i>=0;i--){
r[k++] = s[i];
if(s[i] == '1')
c++;
else
c=0;
if(c==5){
r[k++] = '0';
c = 0;
}
}
r[k] = '\0';
printf("The stuffed string is \n");
printf("%s",strrev(r));
c = 0;
k = 0;
for(i=strlen(r)-1;i>=0;i--){
p[k++] = r[i];
if(r[i]=='1')
c++;
else
c = 0;
if(c==5)
{
c=0;
i--;
}
}
p[k] = '\0';
strrev(p);
printf("The destuffed string is %s",p);
}
OUTPUT:
Byte Stuffing:
#include<stdio.h>
#include<string.h>
int main()
{
char inp[50];
int i,k;
char stuff[50] ="";
char res[50] ="";
char stuff1[50] ="";
printf("Enter the input string ");
scanf("%s",inp);
k = 0;
for(i=strlen(inp)-1;i>=0;i--)
{
stuff[k] = inp[i];
k++;
if(inp[i] == 'E'|| inp[i] == 'e'||inp[i] == 'F'||inp[i] == 'f'){
stuff[k] = 'E';
k++;
}
}
printf("%s\n",strrev(stuff));
k = 0;
for(i=strlen(stuff)-1;i>=0;i--)
{
stuff1[k] = stuff[i];
k++;
if(stuff[i] == 'E' || stuff[i] == 'e' || stuff[i] == 'F' || stuff[i] == 'f'){
i--;
}
}
printf("%s",strrev(stuff1));
}
OUTPUT:
2.Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC CCIP
CODE:
#include<stdio.h>
#include<string.h>
char data[20],div[20],total[100];
int i,j,datalen,divlen,len,flag=1;
void check();
int main()
{
printf("\nEnter the data:");
scanf("%s",&data);
printf("\nEnter the divisor");
scanf("%s",div);
divlen=strlen(div);
datalen=strlen(data);
len=datalen+divlen-1;
for(i=0;i<datalen;i++)
{ total[i]=data[i];
}
for(i=datalen;i<len;i++)
total[i]='0';
check();
for(i=0;i<divlen;i++)
total[i+datalen]=data[i];
printf("\ntransmitted Code Word:%s",total);
printf("\n\nEnter the received code word:");
scanf("%s",total);
check();
for(i=0;i<divlen-1;i++)
if(data[i]=='1')
{
flag=0;
break;
}
if(flag==1)
printf("\nsuccessful!!");
else
printf("\nreceived code word contains errors...\n");
}
void check()
{
for(j=0;j<divlen;j++)
data[j]=total[j];
while(j<=len)
{
if(data[0]=='1')
for(i = 1;i <divlen ; i++)
data[i] = (( data[i] == div[i])?'0':'1');
for(i=0;i<divlen-1;i++)
data[i]=data[i+1];
data[i]=total[j++];
}
}
OUTPUT:
CRC-12
CRC-16
3. Develop a simple data link layer that performs the flow control using the sliding
window protocol, and loss recovery using the Go-Back-N mechanism.
Sliding window protocol:
#include<stdio.h>
#include<stdlib.h>
int count;
void sender(int i){
printf("The sender has transmitted the frame %d :",i);
}
void receiver(int i){
int ack;
printf("Did you receive the frame %d ?(1Y,0Y) ",i);
scanf("%d",&ack);
if(ack == 1)
return;
else{
if(count<4){
count++;
sender(i);
receiver(i);
}
else{
printf("The limit exceeded :");
exit(0);
}
}
}
int main(){
int n,i;
printf("enter the number of frames to be transmitted :");
scanf("%d",&n);
for(i=1;i<=n;i++){
count = 0;
sender(i);
receiver(i);
}}
OUTPUT:
Go-back-N:
#include<stdio.h>
int main(){
int z , i , k, n , t, ws ,ack ;
printf("Enter the number of frames :");
scanf("%d",&n);
printf("Enter the window size :");
scanf("%d",&ws);
i = 1;
t = 0;
while(i<=n){
z = 0;
for(k = i;k<i+ws && k<=n;k++){
printf("The frame %d has been tarnsmitted ",k);
t++;
}
for(k=i;k<i+ws && k<=n;k++){
printf("\nDid you receive acknowledgement for the frame %d ",k);
scanf("%d",&ack);
if(ack==1){
z++;
}
else{
printf("Error in transmitting the frame %d \n",k);
printf("Retransmitting the window ");
break;
}
}
i = i+ z;
}
printf("the total no of transmissions by the sender is %d ", t);
}
OUTPUT:
Selective Repeat:
#include<stdio.h>
int getAck(int w[],int nr[], int j){
int ack[20];
int k = 0;
int i;
for(i=0;i<j;i++){
printf("Enter the acknowledgement for the frame %d ",w[i]);
scanf("%d",&ack[i]);
if(ack[i] == -1){
nr[k++] = w[i];
}
}
return k;
}
int main(){
int n , ws ,k,j,i=1,z;
int w[10],nr[10];
printf("Enter the no of frames :");
scanf("%d",&n);
printf("Enter the window size :");
scanf("%d",&ws);
k = 0;
while(1){
for(j=k;j<ws&&i<=n;j++){
w[j] = i ;
printf("The frame %d is transmitted ",i);
i++;
}
k = getAck(w,nr,j);
if(k == 0 &&i==n)
break;
for(z=0;z<k;z++){
int V;
return min_index;
}
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
scanf("%d",&graph[i][j]);
}
}
dijkstra(graph, 0,V);
return 0;
}
OUTPUT:
5. Implement distance vector routing algorithm for obtaining routing tables at each node.
CODE:
#include<stdio.h>
struct node {
unsigned dist[20];
unsigned from[20];
}rt[10];
int main() {
int dmat[20][20],n,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++ ) {
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j; }
do { count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j]) {
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<n;i++) {
printf("\n\nState value for router %d is \n",i+1);
printf("node\tDistance\tnextHop\n");
for(j=0;j<n;j++) {
printf("%d\t%d\t%d\n",j+1,rt[i].dist[j],rt[i].from[j]+1);
}
printf("\n\n"); }
}
OUTPUT:
6. Write a program for congestion control using Leaky bucket algorithm.
CODE:
#include<stdio.h>
int main(){
int N , R ,B,flag;
int fill=0;
}
}
else{
fill += N-R;
printf("\nThe no of packets transmitted are %d ",R);
if(fill>B){
printf("\nOverflow , %d packets are lost ",fill - B );
fill = B ;
if(fill== B)
printf("\n The bucket is full ");
}
else{
printf("\nNo of packets in the bucket are %d " ,fill );
printf("\nNo packets lost ");
if(fill== B)
printf("\n The bucket is full ");
}
}
printf("\nDo you want to proceed ?(1y,0N)");
scanf("%d",&flag);
}}
OUTPUT:
7. Wireshark
Wireshark is a free opensource network protocol analyzer. It is used for network troubleshooting and communication
protocol analysis. Wireshark captures network packets in real time and display them in human-readable format. It
provides many advanced features including live capture and offline analysis, three-pane packet browser, coloring
rules for analysis.
Getting Wireshark
The Kai Linux has Wireshark installed. You can just launch the Kali Linux VM and open Wireshark there.
Wireshark can also be downloaded from here:
https://www.wireshark.org/download.html
Starting Wireshark
When you run the Wireshark program, the Wireshark graphic user interface will be shown.
Then, you need to choose an interface. If you are running the Wireshark on your
laptop, you need to select WiFi interface. If you are at a desktop, you need to
select the Ethernet interface being used. Note that there could be multiple
interfaces. In general, you can select any interface but that does not mean that
traffic will flow through that interface. The network interfaces (i.e., the physical
connections) that your computer has to the network are shown.
After you select the interface, you can click start to capture the packets as
shown.
The Wireshark interface has five major components:
Capturing Packets
After downloading and installing Wireshark, you can launch it and click the name of an
interface under Interface List to start capturing packets on that interface. For example, if
you want to capture traffic on the wireless network, click your wireless interface.
Do the following steps:
1. Start up the Wireshark program (select an interface and press start to capture
packets).
2. Start up your favorite browser (ceweasel in Kali Linux).
3. In your browser, go to Wayne State homepage by typing www.wayne.edu.
4. After your browser has displayed the http://www.wayne.edu page, stop
Wireshark packet capture by selecting stop in the Wireshark capture window.
This will cause the Wireshark capture window to disappear and the main
Wireshark window to display all packets captured since you began packet
capture see image below:
Color Coding
Filtering Packets
The most basic way to apply a filter is by typing it into the filter box at the top of
the window and clicking Apply (or pressing Enter). For example, type “dns” and
you’ll see only DNS packets. When you start typing, Wireshark will help you
autocomplete your filter.
You can also click Analyze > Display Filters to choose a filter from among the default filters
included in Wireshark. From here, you can add your own custom filters and save them to easily
access them in the future.
Another interesting thing you can do is right-click a packet and select Follow >
TCP Stream.
You’ll see the full TCP conversation between the client and the server. You can
also click other protocols in the Follow menu to see the full conversations for
other protocols, if applicable.
Close the window and you’ll find a filter has been applied automatically. Wireshark is showing you
the packets that make up the conversation.
Inspecting Packets
Click a packet to select it and you can dig down to view
its details.
Steps to capture relevant data :
1) Set the filter as ip.addr == <client ip address>