0% found this document useful (0 votes)
18 views24 pages

Computer network programs vtu

The document contains multiple C programs demonstrating various networking and data transmission protocols, including HDLC bit and character stuffing, socket programming for client-server communication, distance vector algorithm, Dijkstra's algorithm for shortest path, CRC error-checking, and Stop and Wait protocol. Each program includes code snippets, execution steps, and sample outputs. The programs illustrate key concepts in computer networking and data integrity methods.

Uploaded by

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

Computer network programs vtu

The document contains multiple C programs demonstrating various networking and data transmission protocols, including HDLC bit and character stuffing, socket programming for client-server communication, distance vector algorithm, Dijkstra's algorithm for shortest path, CRC error-checking, and Stop and Wait protocol. Each program includes code snippets, execution steps, and sample outputs. The programs illustrate key concepts in computer networking and data integrity methods.

Uploaded by

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

Program1

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

// Function designed for chat between client and server.


void func(int connfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);

// read the message from client and copy it in buffer


read(connfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;

// and send that buffer to client


write(connfd, buff, sizeof(buff));

// if msg contains "Exit" then server exit and chat ended.


if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
}
}

// 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));

// assign IP, PORT


servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);

// Binding newly created socket to given IP and verification


if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");

// Now server is ready to listen and verification


if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);

// Accept the data packet from client and verification


connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server accept failed...\n");
exit(0);
}
else
printf("server accept the client...\n");

// Function for chatting between client and server


func(connfd);

// After chatting close the socket


close(sockfd);
}
client program
#include <arpa/inet.h> // inet_addr()
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h> // bzero()
#include <sys/socket.h>
#include <unistd.h> // read(), write(), close()
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}

int main()
{
int sockfd, connfd;
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));

// assign IP, PORT


servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);

// connect the client socket to server socket


if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr))
!= 0) {
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");

// function for chat


func(sockfd);

// close the socket


close(sockfd);
}

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;

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");

}
output
Enter the number of nodes : 3

Enter the cost matrix :


027
201
710

For router 1

node 1 via 1 Distance 0


node 2 via 2 Distance 2
node 3 via 2 Distance 3

For router 2

node 1 via 1 Distance 2


node 2 via 2 Distance 0
node 3 via 3 Distance 1

For router 3

node 1 via 2 Distance 3


node 2 via 2 Distance 1
node 3 via 3 Distance 0
Program4:
Write a C program to implement Dijkstras algorithm to compute the shortest Routing Path.
#include<stdio.h>
#include<conio.h>

#define infinity 999

void dij(int n, int v, int cost[10][10],int dist[])

Int i, u, count, w, flag[10],min;

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++)

if(dist[w]<min && !flag[w])

min=dist[w],u=w; flag[u]=1;

count++;

for(w=1;w<=n;w++)

if((dist[u]+cost[u][w]<dist[w]) && !flag[w]) dist[w]=dist[u]+cost[u][w];

void main ()

int n,v,i,j,cost[10][10],dist[10];

printf("n Enter the number of nodes:");


scanf("%d",&n);

printf("n Enter the cost matrix:\n");

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;

printf("n Enter the source matrix:");

scanf("%d",&v);

dij(n,v,cost,dist);

printf("n Shortestpath:n");

for(i=1;i<=n;i++)

if(i!=v)

printf(" %d->%d, cost=%dn",v,i,dist[i]);

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

Enter the source matrix:


1
n Shortestpath:n
1->2, cost=3
1->3, cost=7
1->4, cost=5
1->5, cost=9
Program5:

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)
{

printf("\nERROR in Received Data\n");

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

Enter the Message


110

Data to be transmitted
1100110000011000110

Enter the Reveived Data


1100110000011000110

Data Recived is ERROR FREE

2)Enter the length of Data Frame:


3

Enter the Message


110

Data to be transmitted
1100110000011000110

Enter the Reveived Data


1100110000011000111

ERROR in Recived Data


Program6:
Implementation of Stop and Wait Protocol and Sliding Window Protocol Stop and wait

#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
{

printf("\n RECEIVER: Frame %d received,ACK sent\n",i);


printf("-------------------------------");
ACK = YES;

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..

RECEIVER: Frame 1 received,ACK sent


-------------------------------
SENDER:ACK for frame 1 Received

SENDER:FRAME 2 sent, Waiting for ACK..

RECEIVER: Frame 2 received,ACK sent


-------------------------------
SENDER:ACK for frame 2 Received

SENDER:FRAME 3 sent, Waiting for ACK..


SENDER:ACK not received for frame 3 => TIMEOUT Resending frame...
SENDER:FRAME 3 sent, Waiting for ACK..

RECEIVER: Frame 3 received,ACK sent


-------------------------------
SENDER:ACK for frame 3 Received

SENDER:FRAME 4 sent, Waiting for ACK..


SENDER:ACK not received for frame 4 => TIMEOUT Resending frame...
SENDER:FRAME 4 sent, Waiting for ACK..

RECEIVER: Frame 4 received,ACK sent


-------------------------------
SENDER:ACK for frame 4 Received

SENDER:FRAME 5 sent, Waiting for ACK..

RECEIVER: Frame 5 received,ACK sent


-------------------------------
SENDER:ACK for frame 5 Received

SENDER:FRAME 6 sent, Waiting for ACK..

RECEIVER: Frame 6 received,ACK sent


-------------------------------
SENDER:ACK for frame 6 Received

SENDER:FRAME 7 sent, Waiting for ACK..

RECEIVER: Frame 7 received,ACK sent


Program7:
Slinding Window program
#include<stdio.h>
#include<stdlib.h>
#define RTT 5
int main()
{
int window_size, i, f,frames[50];
printf("Enter window size:\n");
scanf("%d", &window_size);
printf("\n Enter number of frames to transmit:");
scanf("%d",&f);
printf("\n Enter %d frames:",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\n After sending %d frmaes at each stage sender
waits for ACK",window_size);
printf("\n sending frames in the following manner.n\n");
for(i=1;i<=f;i++)
{
if(i%window_size != 0)
{
printf("%d\t",frames[i]);
}
else
{
printf("%d\n", frames[i]);
printf("SENDER: waiting for ACk\n\n");
//sleep(RTT/2);
printf("Receiver: FramesR eceived, ACK sent\n");
printf("\n");
//sleep(RTT/2);
printf("SENDER:ACK received, sending next frames\n");
}
}
if(f%window_size != 0)
{
printf ("\n SENDER:waiting for ACK...\n");
//sleep(RTT/2);
printf("\n RECEIVER: Frames Received, ACK SENT\n");
printf(" ");
//sleep(RTT/2);
printf("SENDER : ACKreceived");
}
return 0;
}
output:
Enter window size:
2
Enter number of frames to transmit:6
Enter 6 frames:1 2 3 4 5 6
After sending 2 frmaes at each stage sender waits for ACK
sending frames in the following manner.
12
SENDER: waiting for ACk
Receiver: FramesR eceived, ACK sent
SENDER:ACK received, sending next frames
34
SENDER: waiting for ACk
Receiver: FramesR eceived, ACK sent
SENDER:ACK received, sending next frames
56
SENDER: waiting for ACk

Receiver: FramesR eceived, ACK sent


SENDER:ACK received, sending next frames
Program8:
#include<stdio.h>
int main()
{
int incoming, outgoing, buck_size, n, store = 0;
printf("Enter bucket size: ");
scanf("%d", &buck_size);
printf("\n Enter Outgoing rate:");
scanf("%d",&outgoing);
printf("\n number of inputs:");
scanf("%d",&n);
while (n != 0){
printf("Enter the incoming packet size : ");
scanf("%d", &incoming);
printf("\n In coming packet size %d\n", incoming);
if (incoming <= (buck_size - store))
{
store += incoming;
printf("\nBucket buffer size %d out of %d\n", store, buck_size);
}
else
{
printf("\nDropped %d no of packets\n", incoming - (buck_size - store));
printf("\nBucket buffer size %d out of %d\n", store, buck_size);
store = buck_size;
}
store = store - outgoing;
printf("\n After outgoing %d packets left out of %d in buffer\n", store, buck_size);
n--;
}
}
OUTPUT:
Enter bucket size: 15

Enter outgoing rate: 3

Number of inputs: 3

Enter the incoming packet size: 20

Incoming packet size is 20

Dropped 5 number of packets

Buckets buffers size 0 out of 15

After outgoing 12 packets, left out of 15 in buffer

Enter the incoming packet size:9

Incoming packet size is 9

Dropped 6 number of packets

Bucket buffer size 12 out of 15

After outgoing 12 packets, left out of 15 in buffer

Enter the incoming packet size: 5

Incoming packet size is 5

Dropped 2 number of packets

Bucket buffer size 12 out of 15

After outgoing 12 packets, left out of 15 in buffer


Program9:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef pair<int,int>iPair;
class Graph {
int V;
list<pair<int,int> >*adj;
public:Graph(int V);
void addEdge(int u,int v,int w);
void shortestPath(int s);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<iPair>[V];
}
void Graph::addEdge(int u,int v,int w)
{
adj[u].push_back(make_pair(v,w));
adj[v].push_back(make_pair(u,w));
}
void Graph::shortestPath(int src)
{
priority_queue<iPair, vector<iPair>,greater<iPair> >pq;
vector<int> dist(V,INF);
pq.push(make_pair(0,src));
dist[src] = 0;
while(!pq.empty())
{
int u = pq.top().second;
pq.pop();
list<pair<int, int> >::iterator i;
for (i = adj[u].begin();
i != adj[u].end(); ++i)
{
int v = (*i).first;
int weight = (*i).second;
if (dist[v] > dist[u] + weight)
{
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; ++i)
printf("%d \t\t %d\n", i, dist[i]);
}
int main()
{
int V = 7;
Graph g(V);
g.addEdge(0, 1, 2);
g.addEdge(0, 2, 6);
g.addEdge(1, 3, 5);
g.addEdge(2, 3, 8);
g.addEdge(3, 4, 10);
g.addEdge(3, 5, 15);
g.addEdge(4, 6, 2);
g.addEdge(5, 6, 6);
g.shortestPath(0);
return 0;
}
Execution Steps:
gedit p9.cpp (editor)
g++ p9.cpp (compilation)
./a.out (run)

Output:
Vertex Distance from Source
0 0
1 2
2 6
3 7
4 17
5 22
6 19

You might also like