Computer Network R20 Laqb Manval New
Computer Network R20 Laqb Manval New
COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
Prepared By:
N.Bharathi M.Tech.,
L.Dana Ratna Kishore M.Tech.,
(Assistant Professor)
TABLE OF CONTENTS
Exercise
Number Program Name
Write a program for Hamming Code generation for error detection and
4 correction.
Write a Program to implement on a data set of characters the three CRC
5 polynomials – CRC 12, CRC 16 and CRC CCIP.
EXERCISE 1
PROGRAM :
Repeater
A Repeater operates at the physical layer of OSI Model
Its job is to regenerate the signal over the same network before the signal
becomes too weak or corrupted so as to extend the length to which the signal
can be transmitted over the same network.
An important point to be noted about repeaters is that they do not
amplify(increase the volume) the signal. When the signal becomes weak, they
copy the signal bit by bit and regenerate it at the original strength.
It is a 2 port device.
Hub
A hub is basically a multiport repeater.
A hub connects multiple wires coming from different branches, for example, the
connector in star topology which connects different stations.
Hubs cannot filter data, so data packets are sent to all connected devices.
Also, they do not have the intelligence to find out the best path for data packets
which leads to inefficiencies and wastage.
Also called as ethernet hub, active hub, network hub, repeater hub
Operates at the physical layer of OSI Model.
Bridge
A bridge operates at the data link layer.
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
Gateway
A gateway, as the name suggests, is a passage to connect two networks
together that may work upon different networking models.
They basically work as the messenger agents that take data from one system,
interpret it, and transfer it to another system.
Gateways are also called protocol converters and can operate at any network
layer.
Gateways are generally more complex than switches or routers.
Gateway is also called a protocol converter. Protocol means set of rules that
must be obeyed by both sender and receiver.
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
EXERCISE 2
AIM: Write a Program to implement the data link layer farming methods such
as i) Character stuffing ii) bit stuffing.
Begin
Step 1: Initialize I and j as 0
Step 2: Declare n and pos as integer and a[20],b[50],ch as character
Step 3: read the string a
Step 4: find the length of the string n, i.e n-strlen(a)
Step 5: read the position, pos
Step 6: if pos > n then
Step 7: print invalid position and read again the position, pos
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
Step 8: end if
Step 9: read the character, ch
Step 10: Initialize the array b , b[0…5] as ’d’, ’l’, ’e’, ’s’ ,’t’ ,’x’
respectively
Step 11: j=6;
Step 12: Repeat step[(13to22) until i<n
Step 13: if i==pos-1 then
Step 14: initialize b array,b[j],b[j+1]…b[j+6] as‘d’, ‘l’, ‘e’ ,’ch, ’d’, ‘l’,‘e’
respectively
Step 15: increment j by 7, i.e j=j+7
Step 16: end if
Step 17: if a[i]==’d’ and a[i+1]==’l’ and a[i+2]==’e’ then
Step 18: initialize array b, b[13…15]=’d’, ‘l’, ‘e’ respectively
Step 19: increment j by 3, i.e j=j+3
Step 20: end if
Step 21: b[j]=a[i]
Step 22: increment I and j;
Step 23: initialize b array,b[j],b[j+1]…b[j+6] as‘d’, ‘l’,‘e’ ,’e’,‘t’, ‘x’,‘\0’ respectively
Step 24: print frame after stiuffing
Step 25: print b
End
PROGRAM :
i) Character stuffing
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
char a[20],b[50],ch;
printf("enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("enter position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position, Enter again :");
scanf("%d",&pos);
}
printf("enter the character\n");
ch=getche();
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("\nframe after stuffing:\n");
printf("%s",b);
}
Output:
AIM: Implement the data link layer framing methods such as and bit stuffing.
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
The new technique allows data frames to contain an arbitrary number if bits and allows character
codes with an arbitrary no of bits per character. Each frame begins and ends with special bit pattern,
01111110, called a flag byte. When ever the senders data link layer encounters five consecutive one’s in
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
the data, it automatically stuffs a 0 bit into the out going bit stream. This bit stuffing is analogous to
character stuffing, in which a DLE is stuffed into the out going character stream before DLE in the data
ALGORITHM:
Begin
Step 1: Read frame length n
Step 2: Repeat step (3 to 4) until i<n(: Read values in to the input frame (0’s and
1’s) i.e.
Step 3: initialize I i=0;
Step 4: read a[i] and increment i
Step 5: Initialize i=0, j=0,count =0
Step 6: repeat step (7 to 22) until i<n
Step 7: If a[i] == 1 then
Step 8: b[j] = a[i]
Step 9: Repeat step (10 to 18) until (a[k] =1 and k<n and count <5)
Step 10: Initialize k=i+1;
Step 11: Increment j and b[j]= a[k];
Step 12: Increment count ;
Step 13: if count =5 then
Step 14: increment j,
Step 15: b[j] =0
Step 16: end if
Step 17: i=k;
Step 18: increment k
Step 19: else
Step 20: b[j] = a[i]
Step 21: end if
Step 22: increment I and j
Step 23: print the frame after bit stuffing
Step 24: repeat step (25 to 26) until i< j
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
#include<stdio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame length:");
scanf("%d",&n);
printf("Enter input frame (0's & 1's only):");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1;a[k]==1 && k<n && count<5;k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}
}
else
{
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
b[j]=a[i];
}
i++;
j++;
}
printf("After stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]);
}
Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
EXERCISE 3
AIM: To Write a Program to implement data link layer farming method
checksum.
NAME OF THE EXPERIMENT: ii)Bit Stuffing.
ALGORITHM:
Step 1: Read frame length n
Step 2: Repeat step (3 to 4) until i<n(: Read values in to the input frame (0’s and
1’s) i.e.
Step 3: initialize i, j, dl, dil;
Step 4: read data1[i], data2[i] increment i
Step 5: Initialize i=0, j=0
Step 6: repeat step (7 to 22) until i < dl
Step 7: If a[i] == 1 then
Step 8: b[j] = a[i]
Step 9: Repeat step (10 to 18) until (a[k] =1 and k<n and count <5)
Step 10: checksum[i] = com(newdata[i]);
Step 11: Increment data1[i], data2[i]
Step 12: newdata[i] = add(newdata[i], checksum[i]);
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
Step 24 : return 1
PROGRAM :
#include<stdio.h>
#include<conio.h>
int add(int, int);
int com(int);
void main()
{
int i, j, dl, dil;
int data1[10], data2[10], newdata[10], comp[10], checksum[10];
printf("\n Enter the data length=");
scanf("%d", &dl);
printf("\n Enter the data1 : \n");
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
printf("%d", newdata[i]);
}
printf("\n Compliment : ");
for (i = 0; i < dl; i++) {
comp[i] = com(newdata[i]);
printf("%d", comp[i]);
}
}
Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
EXERCISE 4
AIM: Write a program for Hamming Code generation for error detection
and correction
NAME OF THE EXPERIMENT: Hamming Code generation for error detection and correction
AIM: Implement a program for Hamming Code generation for error detection and correction
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
Hamming code is an error-correcting code used to ensure data accuracy during transmission or
storage. Hamming code detects and corrects the errors that can occur when the data is moved or stored from
the sender to the receiver. This simple and effective method helps improve the reliability of communication
systems and digital storage. It adds extra bits to the original data, allowing the system to detect and correct
single-bit errors.
ALGORITHM:
Step 1: Write the bit positions starting from 1 in binary form (1, 10, 11, 100, etc).
Step 2: All the bit positions that are a power of 2 are marked as parity bits (1, 2, 4,
8, etc).
Step 3: All the other bit positions are marked as data bits.
Step 4: Each data bit is included in a unique set of parity bits, as determined its bit position in binary form
Step 5: Parity bit 1 covers bits positions binary 1 in the least significant position (1, 3, 5, 7, 9, 11, etc).
Step 6: Parity bit 2 bits positions a 1 in the second position least significant bit (2, 3, 6, 7, 10, 11, etc).
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
Step 7:Parity bit 4 bits a 1 in the third position from the least significant bit (4–7, 12–15, 20–23, etc).
Step 8: Parity bit 8 covers all the bits positions whose binary representation includes a 1 in the fourth
position from the least significant bit bits (8–15, 24–31, 40–47, etc).
Step 9: Each parity bit covers all bits where the bitwise AND of the parity position and the bit position is
non-zero.
Step 10: Since we check for even parity set a parity bit to 1 if the total number of ones in the positions it
checks is odd. Set a parity bit to 0 if the total number of ones in the positions it checks is even.
PROGRAM :
#include <math.h>
#include <stdio.h>
// Store input bits
int input[32];
// Store hamming code
int code[32];
int ham_calc(int, int);
void solve(int input[], int);
// Function to calculate bit for
// ith position
int ham_calc(int position, int c_l)
{
int count = 0, i, j;
i = position - 1;
// Traverse to store Hamming Code
while (i < c_l) {
// If current boit is 1
if (code[j] == 1)
count++;
}
// Update i
i = i + 2 * position;
}
if (count % 2 == 0)
return 0;
else
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
return 1;
}
// Function to calculate hamming code
void solve(int input[], int n)
{
int i, p_n = 0, c_l, j, k;
i = 0;
// Find msg bits having set bit
// at x'th position of number
while (n > (int)pow(2, i) - (i + 1)) {
p_n++;
i++;
}
c_l = p_n + n;
j = k = 0;
// Traverse the msgBits
for (i = 0; i < c_l; i++) {
// Driver Code
void main()
{
// Given input message Bit
input[0] = 0;
input[1] = 1;
input[2] = 1;
input[3] = 1;
int N = 4;
// Function Call
solve(input, N);
}
Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
EXERCISE 5
THEORY:
CRC method can detect a single burst of length n, since only one bit per column will be changed,
a burst of length n+1 will pass undetected, if the first bit is inverted, the last bit is inverted and all other
bits are correct. If the block is badly garbled by a long burst or by multiple shorter burst, the probability
that any of the n columns will have the correct parity that is 0.5. so the probability of a bad block being
expected when it should not be 2 power(-n). This scheme some times known as Cyclic Redundancy
Code
ALGORITHM/FLOWCHART:
Begin
Step 1: Declare I,j,fr[8],dupfr[11],recfr[11],tlen,flag,gen[4],genl,frl,
rem[4] as integer
Step 2: initialize frl=8 and genl=4
Step 3: initialize i=0
Step 4: Repeat step(5to7) until i<frl
Step 5: read fr[i]
Step 6: dupfr[i]=fr[i]
Step 7: increment i
Step 8: initialize i=0
Step 9: repeat step(10to11) until i<genl
Step 10: read gen[i]
Step 11: increment i
Step 12: tlen=frl+genl-1
Step 13: initialize i=frl
Step 14: Repeat step(15to16) until i<tlen
Step 15: dupfr[i]=0
Step 16: increment i
Step 17: call the function remainder(dupfr)
Step 18: initialize i=0
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50],c[50],d[50];
int i,j,k,l;
printf("Enter frame:"); /*read the frame*/
gets(a);
strcpy(d,a);
printf("Enter generated frame:"); /*read the generated frame*/
gets(b);
l=strlen(b); /*finding length of the generator*/
for(i=0;i<l-1;i++)
strcat(a,"0");
k=strlen(a); /*finding length of the frame*/
for(i=0;i<l;i++)
c[i]=a[i];
for(i=l;i<k;i++)
{
if(c[0]=='1')
{ /* performing modulo-2 operation*/
for(j=0;j<l;j++)
c[j]=c[j]^b[j];
}
else
{
for(j=0;j<1;j++)
{
if(c[j]=='0')
c[j]='0';
else
c[j]='1';
}
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
}
printf("\n");
if(i!=k)
{
/* performing last modulo-2 operation and finding reminder*/
for(j=0;j<(l-1);j++)
c[j]=c[j+1];
c[j]=a[i];
}
}
printf("CRC /reminder: \n");
for(i=1;i<l;i++)
printf("%c",c[i]); /*displays the crc*/
printf("\n The transmitted frame:"); /*displays the Transmitted frame*/
printf("%c",d[i]);
for(i=1;i<l;i++)
printf("%c",c[i]);
getch();
}
Output:
EXERCISE 6
ALGORITHM/FLOWCHART:
Step1:Initializewindowsize,sent=0,ack,;
Step 2: Set(next_seq_num) to 0
Step 3: Set(window_start) to 0.
Step 4: While true
Step 5: Send frames in the window
Step 6: For i = window_start to WS-1:
Step 7: sent++;
Step 8: if(sent == windowsize)
Step 9: break;
Step 10: if(ack == windowsize)
PROGRAM :
#include<stdio.h>
int main()
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
{
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}
printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);
if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}
Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
EXERCISE 7
ALGORITHM/FLOWCHART:
Step 8: gets(sender);
Step 9: for(i=0;i<winsize;i++)
PROGRAM :
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
#include<stdio.h>
#include<conio.h>
void main()
{
char sender[50],receiver[50];
int i,winsize;
printf("\n ENTER THE WINDOWS SIZE : ");
scanf("%d",&winsize);
printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");
printf("\n ENTER THE DATA TO BE SENT: ");
fflush(stdin);
gets(sender);
for(i=0;i<winsize;i++)
receiver[i]=sender[i];
receiver[i]=NULL;
printf("\n MESSAGE SEND BY THE SENDER:\n");
puts(sender);
printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
for(i=0;i<winsize;i++);
printf("\n ACK:%d",i);
printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
puts(receiver);
printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
getch();
}
Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
EXERCISE 8
THEORY:
This program defines a `frame_t` struct to represent a frame in the protocol, and three functions:
`send_frame` to send a new frame over the network and wait for an ACK, `receive_ack` to mark a frame as
acknowledged, and `stop_and_wait` to implement the Stop and Wait algorithm
ALGORITHM/FLOWCHART:
Step1: Initialize the window size (WS) to 1.
Step 11: Repeat steps 6 and 7 until all frames have been sent and acknowledged.
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
PROGRAM :
SENDER.C
// ARQ - client server
// sender.c
//connection if he ready to receive else wait
// till he does not ready or timeout fails .
/*server.c and client.c - implementation of go-back-n ARQ in C
Server.c implements a reliable data transfer over UDP in C
client.c implements a reliable data transfer client over UDP in C
Both of these programs use the go-back-n ARQ, that is lost data is
automatically resent. These programs are hardcoded to transfer
to compile:
gcc -o server server.c
gcc -o client client.c
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MAXLINE 80
char buffer[255] ;
#define SERVER_ADDR "127.0.0.1"
#define SERVER_PORT_NUM 6000
#define t_max_retransmission 3
typedef int bit_32_var ;
typedef char bit_8_var ;
typedef struct pdu_field
{
bit_32_var SN ;
bit_8_var data[MAXLINE] ;
bit_8_var status ;
}PDU_FIELD;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
if ( counter_1 > 0 )
{
if ( recv_data[counter_1].SN == send_data[counter_1].SN + 1 )
printf ( "\n server responding = %s ", recv_data[counter_1].data ) ;
else
{
if ( retransmission_counter < t_max_retransmission )
{
printf ( "incorrect ack - sending the same data - \n" ) ;
counter_1 -= 1 ;
retransmission_counter++ ;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
}
else
{
printf ( "Time out : sending next data \n " ) ;
retransmission_counter = 0 ;
}
}
}
}
write ( sfd , "Nothingtotransmit:\n" , 10 ) ;
printf ( "Transmission finished : \n" ) ;
exit ( 1 ) ;
}
int main ()
{
bit_32_var socket_fd ;
struct sockaddr_in config_client ;
socket_fd = socket ( AF_INET , SOCK_STREAM , 0 ) ;
if ( socket_fd < 0 )
{
printf ( "client : failed to create socket \n" ) ;
exit ( 1 ) ;
}
memset ( &config_client , 0 , sizeof ( struct sockaddr_in )) ;
config_client.sin_family = AF_INET ;
config_client.sin_port = htons ( SERVER_PORT_NUM ) ;
inet_aton ( SERVER_ADDR , &config_client.sin_addr ) ;
connect ( socket_fd , (struct sockaddr *)&config_client , sizeof ( config_client ) ) ;
printf ( "connect successfully\n" ) ;
system ( "clear" ) ;
str_cli ( stdin , socket_fd ) ;
exit ( 0 );
}
RECEIVE.C
to compile:
gcc -o server server.c
gcc -o client client.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#define SUCCESS 1
#define FAILURE 0
#define NO_OF_FRAMES 10
typedef int BIT_32_VAR_INT ;
typedef char BIT_8_VAR_CHAR ;
void error ( BIT_8_VAR_CHAR *msg )
{
perror ( msg ) ;
exit ( 1 ) ;
}
typedef struct pdu_data
{
BIT_32_VAR_INT SN ;
BIT_8_VAR_CHAR data[80] ;
BIT_32_VAR_INT status ;
}PDU_DATA;
int main () {
PDU_DATA send_data[10] , recv_data[10] ;
BIT_32_VAR_INT socket_fd , newsocket_fd , port_no , client_len ;
// char buffer[256] ;
struct sockaddr_in serv_addr , cli_addr ;
int sizeof_read_data , counter_1 ;
//char buf[256] ;
socket_fd = socket ( AF_INET , SOCK_STREAM , 0 ) ;
if ( socket_fd < 0 )
{
error ( "error in socket opening " ) ;
}
serv_addr.sin_family = AF_INET ;
serv_addr.sin_addr.s_addr = INADDR_ANY ;
serv_addr.sin_port = htons ( 6000 ) ;
listen ( socket_fd , 5 ) ;
if ( newsocket_fd < 0 )
error ( "error in accept " ) ;
while ( 1 )
{
if ( sizeof_read_data < 0 )
error ( "error in reading from socket " ) ;
send_data[counter_1].status = SUCCESS ;
write ( newsocket_fd , &send_data[counter_1] , sizeof ( send_data[counter_1]) ) ;
}
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
Output:
EXERCISE 9
AIM: Write a program for congestion control using leaky bucket algorithm
THEORY:
In the network layer, before the network can make Quality of service guarantees, it must know what
traffic is being guaranteed. One of the main causes of congestion is that traffic is often bursty.
ALGORITHM/FLOWCHART:
Step1: Initialize the bucket size (BUCKET_SIZE) and leak rate (LEAK_RATE).
Step 2: Add data to the bucket at a constant rate (DATA_RATE).
Step 3: If the bucket is full (i.e., the amount of data in the bucket is greater than or equal to
BUCKET_SIZE), discard any additional data.
Step 11: Repeat steps 6 and 7 until all frames have been sent and acknowledged
PROGRAM :
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{ int
packets[8],i,j,clk,b_size,o_rate,i_rate,p_sz_rm=0,p_sz,p_time;
for(i=0;i<5;++i)
{ packets[i]=rand()%10;
if(packets[i]==0) --i;
}
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
EXERCISE 10
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
ALGORITHM/FLOWCHART:
Begin
Step1: Declare array path [5] [5], min, a [5][5], index, t[5];
Step2: Declare and initialize st=1,ed=5
Step 3: Declare variables i, j, stp, p, edp
Step 4: print “enter the cost “
Step 5: i=1
Step 6: Repeat step (7 to 11) until (i<=5)
Step 7: j=1
Step 8: repeat step (9 to 10) until (j<=5)
Step 9: Read a[i] [j]
Step 10: increment j
Step 11: increment i
Step 12: print “Enter the path”
Step 13: read p
Step 14: print “Enter possible paths”
Step 15: i=1
Step 16: repeat step(17 to 21) until (i<=p)
Step 17: j=1
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],path[5][5],st=1,stp,ed=5,edp,i,j,p,t[5],min,index;
printf("enter the cost matrix:\n"); /*reading the cost matrix*/
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
scanf("%d",&a[i][j]);
printf("enter number of paths:\n"); /*reading the no.of paths*/
scanf("%d",&p);
printf("enter the possible paths:\n"); /*reading the possible paths*/
for(i=1;i<=p;i++)
for(j=1;j<=5;j++)
scanf("%d",&path[i][j]);
for(i=1;i<=p;i++)
{
t[i]=0;
stp=st;
for(j=1;j<=5;j++)
{
ed=path[i][j+1];
t[i]=t[i]+path[stp][edp]; /*calculating the distance*/
if(edp==ed) /*checking if already transformed*/
break;
else
stp=edp;
}
}
min=t[st];
index=st;
for(i=1;i<=p;i++) /*finding the minimum distance path*/
{
if(min>t[i])
{
min=t[i];
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
index=i;
}
}
printf("\n minimum cost %d",min);
printf("\n minimum cost path:");
for(i=1;i<=p;i++)
{
printf("-->%d",path[index][i]); /*displaying minimum cost path*/
if(path[index][i] == ed)
break;
}
getch();
}
Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
EXERCISE 11
AIM: Obtain Routing table at each node using distance vector routing algorithm for a
given subnet.
Distance vector routing algorithms are not preferable for complex networks and take longer to converge.
ALGORITHM:
Begin
Step1: Create struct node unsigned dist[20],unsigned from[20],rt[10]
Step2: initialize int dmat[20][20], n,i,j,k,count=0,
Step3: write "the number of nodes "
Step4: read the number of nodes "n"
Step5: write" the cost matrix :"
Step6: intialize i=0
Step7: repeat until i<n
Step8: increment i
Step9: initialize j=0
Step10: repeat Step(10-16)until j<n
Step11: increment j
Step12:read dmat[i][j]
Step13:intialize dmat[i][j]=0
Step14:intialize rt[i].dist[j]=dmat[i][j]
Step15:intialize rt[i].from[j]=j
Step16:end
Step17:start do loop Step (17-33)until
Step18:intilialize count =0
Step19:initialize i=0
Step20:repeat until i<n
Step21:increment i
Step22:initialize j=0
Step23:repeat until j<n
Step24:increment j
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
Step25:initialize k=0
Step26:repeat until k<n
Step27:increment k
Step28:if repeat Step(28-32) until rt[i].dist[j]>dmat[i][k]+rt[k].dist[j]
Step29:intialize rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j]
Step30:intialize rt[i].from[j]=k;
Step31:increment count
Step32:end if
Step33:end do stmt
Step34:while (count!=0)
Step35:initialize i=0
Step36:repeat Steps(36-44)until i<n
Step37:increment i
Step38:write ' state values for router',i+1
Step39:initialize j=0
Step40:repeat Steps ( 40-43)until j<n
Step41:increment j
Step42:write 'node %d via %d distance % ',j+1,rt[i].from[j]+1,rt[i].dist[j]
Step43:end
Step44:end
PROGRAM :
#include<stdio.h>
#include<conio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
EXERCISE 12
ALGORITHM/FLOWCHART:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
step28: if'edge[i]j]!=99
step29: if'min>edge[i][j] repeat step (29-32)
step30: intialize min=edge[i][j]
step31: intialize u=i
step32: intialize v=j
step33: calling function p=find(u);
step34: calling function q=find(v);
step35: if'P!=q repeat steps(35-39)
step36: intialize t[i][0]=u
step37: intialize t[i][1]=v
step38: initialize mincost=mincost+edge[u][v]
step39: call function sunion(p,q)
step40: else repeat steps(40-42)
step41: Intialize t[i][0]=-1;
step42: Intialize t[i][1]=-1;
step43: intialize min=99;
step44; write"Minimum cost is %d\n Minimum spanning tree is",mincost
step45: Initialize i=0
step46: repeat until i<n
step47: increment i
step48: if't[i][0]!=-1 && t[i][1]!=-1'repeat step(48-50)
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
PROGRAM :
#include<stdio.h>
int p,q,u,v,n;
int min=99,mincost=0;
int t[50][2],i,j;
int parent[50],edge[50][50];
main()
{
printf("\n Enter the number of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%c\t",65+i);
parent[i]=-1;
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%c",65+i);
for(j=0;j<n;j++)
scanf("%d",&edge[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(edge[i][j]!=99)
if(min>edge[i][j])
{
min=edge[i][j];
u=i;
v=j;
}
p=find(u);
q=find(v);
if(p!=q)
{
t[i][0]=u;
t[i][1]=v;
mincost=mincost+edge[u][v];
sunion(p,q);
}
else
{
t[i][0]=-1;
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)
t[i][1]=-1;
}
min=99;
}
printf("Minimum cost is %d\n Minimum spanning tree is\n" ,mincost);
for(i=0;i<n;i++)
if(t[i][0]!=-1 && t[i][1]!=-1)
{
printf("%c %c %d", 65+t[i][0], 65+t[i][1],
edge[t[i][0]][t[i][1]]);
printf("\n");
}
}
sunion(int l,int m)
{
parent[l]=m;
}
find(int l)
{
if(parent[l]>0)
l=parent[l];
return l;
}
Output:
D.N.R.COLLEGE OF ENGINEERING&TECHNOLOGY
(Approved by AICTE, New Delhi & Affiliated to JNTUK ,Kakinada)
(Accredited with A++Grade by NAAC)
Balusumudi, BHIMAVARAM–534202.W.G.Dist.,(A.P.)