Computer network programs vtu
Computer network programs vtu
Write a C program to implement the High level Data Link Control (HDLC) protocol frame to
perform the following
i) Bit stuffing
ii) Character Stuffing
i) Bit stuffing
#include<stdio.h>
void main()
{
int i=0, count=0;
char a[100];
printf("Enter the frame(0's & 1's) :");
scanf("%s",a);
printf("\n After bit stuffing\n");
//printf("01111110 ");
for(i=0;a[i];i++)
{
if(a[i]=='1')
count++;
else
count=0;
printf("%c",a[i]);
if(count==5)
{
printf("0");
count=0;
}
}
//printf(" 01111110");
}
Output:
Enter the frame: 1111111100000011111111
After the bit stuffung: 11111 0 111 00000 111110 111
ii. Character stuffing:
#include<string.h>
#include<stdio.h>
void main( )
{
int i=0,j=0,n;
char a[30],b[60];
printf("\nEnter orginal string:\n");
scanf("%s",a);
n=strlen(a);
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
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++;
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("\n After character stuffing:\n");
printf("%s",b);
Output:
Enter the original string: ab dle de dle
After character stuffing: dlestx ab dle dle de dle dle
Program2:
Write a C program to create client (socket)
Server program
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> // read(), write(), close()
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
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));
int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
Execution Steps:
Note: Create two different files cient.c and server.c. Follow the steps given:
1. Open a terminal run the server program
2. Open one more terminal run the client program
3. Run the server program
server side:
admin1@admin1-ThinkCentre-M70q-Gen-2:~$ cc server.c
admin1@admin1-ThinkCentre-M70q-Gen-2:~$ ./a.out
Socket successfully created..
Socket successfully binded..
Server listening..
hello
server accept the client...
From client: ise
To client :
-----------------------------------------------
client Side:
admin1@admin1-ThinkCentre-M70q-Gen-2:~$ cc client.c
admin1@admin1-ThinkCentre-M70q-Gen-2:~$ ./a.out
Socket successfully created..
connected to the server..
Enter the string : ise
From Server : hello
Enter the string :
Program3:
Write a C program for DISTANCE VECTOR ALGORITHM to find suitable path for
transmission
#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;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
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])
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++)
for(j=0;j<nodes;j++)
printf("\n\n");
}
output
Enter the number of nodes : 3
For router 1
For router 2
For router 3
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
min=99;
for(w=1;w<=n;w++)
min=dist[w],u=w; flag[u]=1;
count++;
for(w=1;w<=n;w++)
void main ()
int n,v,i,j,cost[10][10],dist[10];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]); if(cost[i][j]==0)
cost[i][j]=infinity;
scanf("%d",&v);
dij(n,v,cost,dist);
printf("n Shortestpath:n");
for(i=1;i<=n;i++)
if(i!=v)
OUTPUT:
Enter the number of nodes:
5
n Enter the cost matrix:
0 3 999 7 999
3 0 4 2 999
04056
72504
999 999 6 4 0
For the given data, use CRC-CCITT polynomial to obtain CRC code. Verify the program for the
cases
a. Without error
b. With error .
#include<stdio.h>
int a[100], b[100],i,j,len,k,count=0;
//Generator Polynomial:g(x)=x^16+x^12+x^5+1
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,};
int main()
{
void div();
printf("\n Enter the length of Data Frame:");
scanf("%d",&len);
printf("\nEnter the Message\n");
for(i=0;i<len;i++)
scanf("%d",&a[i]);
//Append r(16) degree Zeros to Msg bits
for(i=0;i<16;i++)
a[len++]=0;
//Xr.M(x) (ie. Msg+16 Zeros)
for(i=0; i<len; i++)
b[i]=a[i];
//Number of times to be divided ie. MsgLength
k=len-16;
div();
for(i=0;i<len;i++)
b[i]=b[i]^a[i]; //MOD 2 Substraction
printf("\nData to be transmitted\n");
for(i=0;i<len;i++)
printf("%2d",b[i]);
printf("\n\nEnter the Received Data\n");
for(i=0;i<len;i++)
scanf("%d",&a[i]);
div();
for(i=0;i<len;i++)
if(a[i]!=0)
{
return 0;
}
printf("\n Data Received is ERROR FREE\n");
}
void div()
{
for(i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(j=i;j<17+i;j++)
a[j]=a[j]^gp[count++];
}
count=0;
}
}
OUTPUT
1)Enter the length of Data Frame:
3
Data to be transmitted
1100110000011000110
Data to be transmitted
1100110000011000110
#include<stdio.h>
#include<stdlib.h>
#define RTT 4
#define Timeout 4
#define TOT_FRAMES 7
enum{NO,YES}ACK;
int main()
{
int wait_time, i=1;
ACK= YES; for(;i<=TOT_FRAMES;)
{
if(ACK==YES &&i!=1)
{
printf("\n SENDER:ACK for frame %d Received \n",i-1);
}
printf("\n SENDER:FRAME %d sent, Waiting for ACK..\n",i);
ACK = NO;
wait_time=rand()%4+1;
if(wait_time == Timeout)
{
printf("SENDER:ACK not received for frame %d => TIMEOUT Resending frame...",i);
}
else
{
i++;
}
}
return 0;
}
OUTPUT:
SENDER:FRAME 1 sent, Waiting for ACK..
SENDER:ACK not received for frame 1 => TIMEOUT Resending frame...
SENDER:FRAME 1 sent, Waiting for ACK..
Number of inputs: 3
Output:
Vertex Distance from Source
0 0
1 2
2 6
3 7
4 17
5 22
6 19