Professional Documents
Culture Documents
06CSL77 - Networks Manual
06CSL77 - Networks Manual
NETWORKS LABORATORY
Sub Code : 06CSL77
Note : Student is required to solve one problem from PART-A and one problem
from PART-B. Both the parts have equal marks.
PART B
Implement the following in C/C++.
KJ
Department Of Computer Science, YDIT, Bangalore. Page: 1/15
06CSL77: Networks Laboratory Manual C/C++ Programs
1) Write a program for error detecting code using CRC-CCITT (16-bits).
//crc.c
#include<stdio.h>
#include<string.h>
char data[50], crc[50];
char gen[] = "10001000000100001"; //G(x): x^16+x^12+x^5+1 for CRC-CCITT 16bit
int len, i, j;
void calc_crc() {
for (i = 0; i < strlen(gen); i++) crc[i] = data[i];
do {
if (crc[0] == '1') {
for (j = 1; j < strlen(gen); j++)
crc[j] = ((crc[j] == gen[j]) ? '0' : '1');
}
for (j = 0; j < strlen(gen) - 1; j++)
crc[j] = crc[j + 1];
crc[j] = data[i++];
} while (i <= len + strlen(gen) - 1);
}
int main() {
printf("Enter Bit string\t: ");
scanf("%s", data);
len = strlen(data);
printf("Generating Polynomial\t: %s\n", gen);
for (i = len; i < len + strlen(gen) - 1; i++) data[i] = '0';
printf("Modified Data is\t: %s\n", data);
calc_crc();
printf("Checksum is\t\t: %s\n", crc);
for (i = len; i < len + strlen(gen) - 1; i++) data[i] = crc[i - len];
printf("Final Codeword is\t: %s\n", data);
printf("Test Error detection\n1(Yes) / 0(No)? : ");
scanf("%d", &i);
if (i == 1) {
printf("Enter position to insert an error : ");
scanf("%d", &i);
data[i] = (data[i] == '0') ? '1' : '0';
printf("Errorneous data\t\t: %s\n", data);
}
calc_crc();
for (i = 0; (i < strlen(gen) - 1) && (crc[i] != '1'); i++);
if (i < strlen(gen) - 1) printf("Error detected.\n");
else printf("No Error Detected.\n");
return 0;
}
Output:
$ gcc crc.c
$ ./a.out
Enter Bit string: 10100010101110101010010010100010110
Generating Polynomial : 10001000000100001
Modified t[u] is: 101000101011101010100100101000101100000000000000000
Checksum is : 1000000100110101
Final Codeword is : 101000101011101010100100101000101101000000100110101
Test Error detection
1(Yes) / 0(No)? : 1
Enter position to insert an error : 24
Errorneous data : 101000101011101010100100001000101101000000100110101
Error detected. KJ
Department Of Computer Science, YDIT, Bangalore. Page: 2/15
06CSL77: Networks Laboratory Manual C/C++ Programs
2) Write a program for frame sorting technique used in buffers.
//Framesort.c
#include<stdio.h>
struct frame { //structure maintained to hold frames
char fdata[20];
int fno;
} fr[20];
int n;
void sort() {
int i, j;
struct frame temp;
for (i = 0; i < n; i++)
for (j = 0; j < n - 1 - i; j++)
if (fr[j].fno > fr[j + 1].fno) {
temp = fr[j];
fr[j] = fr[j + 1];
fr[j + 1] = temp;
}
}
int main() {
int i;
printf("Enter number of frames: ");
scanf("%d", &n);
for (i = 0; i < n; i++) { //Assign Frames with data and sequence number.
printf("Enter Frame No and data: ");
scanf("%d%s", &fr[i].fno, fr[i].fdata);
}
printf("Before sorting\n");
for (i = 0; i < n; i++)
printf("%2d\t%s\n", fr[i].fno, fr[i].fdata);
sort(); //Sorts the frames
printf("After sorting\n");
for (i = 0; i < n; i++)
printf("%2d\t%s\n", fr[i].fno, fr[i].fdata);
return 0;
}
Output:
$ gcc framesort.c
$ ./a.out
Enter number of frames: 5
Enter Frame No and data: 3 hi
Enter Frame No and data: 1 hello
Enter Frame No and data: 5 Yahoo
Enter Frame No and data: 2 Cool
Enter Frame No and data: 4 bye
Before sorting
3 hi
1 hello
5 Yahoo
2 Cool
4 bye
After sorting
1 hello
2 Cool
3 hi
4 bye
5 Yahoo
KJ
Department Of Computer Science, YDIT, Bangalore. Page: 3/15
06CSL77: Networks Laboratory Manual C/C++ Programs
3) Write a program for distance vector algorithm to find suitable path for
transmission.
//dv.c
#include<stdio.h>
int n, e, s[20][20], graph[20][20];
void initialize() {
int i, j;
for (i = 1; i <= 20; i++)
for (j = 1; j <= 20 && (s[i][j] = graph[i][j] = 0); j++);
}
void getgraph() {
int i, strt, end;
printf("Enter no. of routers & edges in the graph: ");
scanf("%d%d", &n, &e);
while (e-- > 0) {
printf("Enter start router -> end router : ");
scanf("%d%d", &strt, &end);
graph[strt][end] = graph[end][strt] = 1;
}
}
void gettable(int src) {
int i, j;
printf("Enter information for Source router %d.\n", src);
for (i = 1; i <= n; i++)
if (graph[src][i] == 1) {
printf("Enter distance from source router %d to %d :", src, i);
scanf("%d", &s[src][i]);
}
printf("Enter the contents of Echo packet
of Adacentj routers of %d\n",src);
for (i = 1; i <= n; i++)
if (graph[src][i] == 1) {
printf("Enter the contents of Echo packet of router %d.\n", i);
for (j = 1; j <= n; j++) {
if (i == j) continue;
printf("Enter distance from router %d to %d :", i, j);
scanf("%d", &s[i][j]);
}
}
}
void process(int src, int dest) {
int min = 999, i, delay, via;
for (i = 1; i <= n; i++)
if (graph[src][i] == 1) {
delay = s[src][i] + s[i][dest];
if (delay < min) {
min = delay;
via = i;
}
}
printf("Suitable path from router %d to %d is
through router %d with delay %d units\n", src, dest, via, min);
}
int main() {
int src, dest;
initialize();
getgraph();
KJ
Department Of Computer Science, YDIT, Bangalore. Page: 4/15
06CSL77: Networks Laboratory Manual C/C++ Programs
printf("Enter the Source & Destination router\n");
scanf("%d%d", &src, &dest);
gettable(src);
process(src, dest);
return 0;
}
Output:
$ gcc dv.c
$ ./a.out
Enter no. of routers & edges in the graph: 5 12
Enter start router --> end router : 1 2
Enter start router --> end router : 2 3
Enter start router --> end router : 3 4
Enter start router --> end router : 4 1
Enter start router --> end router : 1 5
Enter start router --> end router : 5 3
Enter start router --> end router : 3 5
Enter start router --> end router : 5 1
Enter start router --> end router : 1 4
Enter start router --> end router : 4 3
Enter start router --> end router : 3 2
Enter start router --> end router : 2 1
Enter the Source & Destination router
1 3
Enter information for Source router 1.
Enter distance from source router 1 to 2 :3
Enter distance from source router 1 to 4 :3
Enter distance from source router 1 to 5 :7
Enter the contents of Echo packet of Adjacent routers of 1
Enter the contents of Echo packet of router 2.
Enter distance from router 2 to 1 :8
Enter distance from router 2 to 3 :4
Enter distance from router 2 to 4 :999
Enter distance from router 2 to 5 :999
Enter the contents of Echo packet of router 4.
Enter distance from router 4 to 1 :5
Enter distance from router 4 to 2 :999
Enter distance from router 4 to 3 :3
Enter distance from router 4 to 5 :999
Enter the contents of Echo packet of router 5.
Enter distance from router 5 to 1 :4
Enter distance from router 5 to 2 :999
Enter distance from router 5 to 3 :1
Enter distance from router 5 to 4 :999
Suitable path from router 1 to 3 is through router 4 with delay 6 units
8 3
1 2
5 4 6
7
5
3
3 1 4
4 3
2 3 KJ
Department Of Computer Science, YDIT, Bangalore. Page: 5/15
06CSL77: Networks Laboratory Manual C/C++ Programs
4) Using TCP/IP sockets, write a client-server program to make client sending
the file name and the server to send back the contents of the requested file if
present.
//4-server.c
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#define BUFSIZE 1024
#define SERVER_PORT 15000
int main() {
int count, create_socket, new_socket, addrlen, fd;
char *buffer = malloc(BUFSIZE);
char fname[256];
struct sockaddr_in address;
if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) > 0)
printf("[SERVER] The socket was created\n");
else
perror("[SERVER] Socket creation Failed"), exit(1);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(SERVER_PORT);
if (bind(create_socket, (struct sockaddr *) &address, sizeof (address))==0)
printf("[SERVER] Binding Socket\n");
else
perror("[SERVER] Socket Binding Failed"), exit(1);
listen(create_socket, 5);
addrlen = sizeof (struct sockaddr_in);
new_socket = accept(create_socket, (struct sockaddr *) &address, &addrlen);
if (new_socket > 0)
printf("[SERVER] The Client %s is Connected...\n",
inet_ntoa(address.sin_addr));
else
perror("[SERVER] Client not connected"), exit(1);
recv(new_socket, fname, 255, 0);
printf("[SERVER] A request for filename %s Received..\n", fname);
if ((fd = open(fname, O_RDONLY)) < 0)
send(new_socket, "[SERVER] File Open Failed.\n", 27, 0);
//27 is the length of string "[SERVER] File Open Failed.\n"
while ((count = read(fd, buffer, BUFSIZE)) > 0)
send(new_socket, buffer, count, 0);
printf("[SERVER] Request Completed\n");
close(fd);
close(new_socket);
return close(create_socket);
}
//4-client.c
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h> KJ
Department Of Computer Science, YDIT, Bangalore. Page: 6/15
06CSL77: Networks Laboratory Manual C/C++ Programs
#define BUFSIZE 1024
#define SERV_HOST_ADDR "127.0.0.1"
#define SERVER_PORT 15000
int main() {
int create_socket, count;
char *buffer = malloc(BUFSIZE);
char fname[256];
struct sockaddr_in address;
if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) > 0)
printf("[CLIENT] The Socket was created\n");
else
perror("[CLIENT] Socket creation Failed"), exit(1);
address.sin_family = AF_INET;
address.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERV_HOST_ADDR, &address.sin_addr);
if (connect(create_socket,(struct sockaddr *)&address,sizeof (address))==0)
printf("[CLIENT] The connection was accepted with the server %s\n",
SERV_HOST_ADDR);
else
perror("[CLIENT] Binding creation Failed"), exit(1);
printf("[CLIENT] Enter The Filename to Request : ");
scanf("%s", fname);
send(create_socket, fname, sizeof (fname), 0);
printf("[CLIENT] Request Accepted... Receiving File...\n");
printf("The contents of file are...\n\n");
while ((count = recv(create_socket, buffer, BUFSIZE, 0)) > 0)
write(1, buffer, count);
return close(create_socket);
}
Output:[SERVER]
$ gcc -o server 4-server.c
$ ./server
[SERVER] The socket was created
[SERVER] Binding Socket
[SERVER] The Client 127.0.0.1 is Connected...
[SERVER] A request for filename msg.txt Received..
[SERVER] Request Completed
$
Output:[CLIENT]
$ gcc -o client 4-client.c
$ ./client
[CLIENT] The Socket was created
[CLIENT] The connection was accepted with the server 127.0.0.1
[CLIENT] Enter The Filename to Request : msg.txt
[CLIENT] Request Accepted... Receiving File...
[CLIENT] The contents of file are...
The contents of file are...
Hi this is
sample
FILE :)
$
KJ
Department Of Computer Science, YDIT, Bangalore. Page: 7/15
06CSL77: Networks Laboratory Manual C/C++ Programs
5) Implement the above program using as message queues or FIFOs as IPC
channels.
//5-server.c
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
//#include<string.h>
#define FIFO1 "fifo1"
#define FIFO2 "fifo2"
#define PERM 0666
int main() {
int readfd, writefd, fd;
ssize_t n;
char buff[512], fname[256];
else
while ((n = read(fd, buff, 512)) > 0)
write(writefd, buff, n);
close(fd);
close(readfd);
close(writefd);
unlink(FIFO1);
unlink(FIFO2);
}
KJ
Department Of Computer Science, YDIT, Bangalore. Page: 8/15
06CSL77: Networks Laboratory Manual C/C++ Programs
//5-client.c
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#define FIFO1 "fifo1"
#define FIFO2 "fifo2"
#define PERMS 0666
int main() {
ssize_t n;
char buff[512], fname[256];
int readfd, writefd;
printf("[CLIENT] Trying to Connect to Server..\n");
writefd = open(FIFO1, O_WRONLY, 0);
readfd = open(FIFO2, O_RDONLY, 0);
printf("[CLIENT] Connected..\n");
printf("[CLIENT] Enter the filename to request from server: ");
scanf("%s", fname);
write(writefd, fname, strlen(fname));
printf("[CLIENT] Waiting for Server to reply..\n");
while ((n = read(readfd, buff, 512)) > 0)
write(1, buff, n);
close(readfd);
close(writefd);
return 0;
}
Output: [SERVER]
$ gcc -o s 5-server.c
$ ./s
[SERVER] Waiting for connection Request..
[SERVER] Connection Established..
[SERVER] Client has requested file msg.txt
[SERVER] Requested Complete.
$
Output: [CLIENT]
$ gcc -o c 5-client.c
$ ./c
[CLIENT] Trying to Connect to Server..
[CLIENT] Connected..
[CLIENT] Enter the filename to request from server: msg.txt
[CLIENT] Waiting for Server to reply..
The contents of file are...
Hi this is
sample
FILE :)
$
KJ
Department Of Computer Science, YDIT, Bangalore. Page: 9/15
06CSL77: Networks Laboratory Manual C/C++ Programs
6) Write a program for simple RSA algorithm to encrypt and decrypt the data.
//rsa.c
#include<stdio.h>
typedef unsigned int uint;
int main() {
char msg[256];
uint pt[256], ct[256], n, d, e, p, q, z, i, len;
do {
printf("Enter 2 large prime numbers p & q:");
scanf("%d %d", &p, &q); //Get 2 large Prime numbers
} while (prime(p) || prime(q)); //check for prime
n = p*q;
z = (p - 1)*(q – 1);
do {
printf("Enter prime value of e relative to %d(z):", z);
scanf("%d", &e); //Get Encryption (Public) key
} while (gcd(e, z) != 1 || e > n);
printf("Cipher Text=");
for (i = 0; i < len; i++) { //convert plain to cipher text
ct[i] = multi(pt[i], e, n);
printf("%d ", ct[i]);
}
printf("\nPlain Text=");
for (i = 0; i < len; i++) { //convert cipher to plain text
pt[i] = multi(ct[i], d, n);
printf("%c", pt[i]);
}
printf("\n");
}
Output:
$ gcc rsa.c
$ ./a.out
Enter 2 large prime numbers p & q:23 11
Enter prime value of e relative to 220(z):113
Enter the Message
computer networks
Cipher Text=44 166 175 19 123 139 85 229 131 220 85 139 179 166 229 17 92
Plain Text=computer networks
KJ
Department Of Computer Science, YDIT, Bangalore. Page: 11/15
06CSL77: Networks Laboratory Manual C/C++ Programs
7) Write a program for Hamming Code generation for error detection and
correction.
//hamming.c
#include<stdio.h>
char data[4];
int encoded[7], edata[7], syn[3], i = 0;
int hmatrix[3][7] = {1, 0, 0, 0, 1, 1, 1,
0, 1, 0, 1, 0, 1, 1,
0, 0, 1, 1, 1, 0, 1};
int gmatrix[4][7] = {0, 1, 1, 1, 0, 0, 0,
1, 0, 1, 0, 1, 0, 0,
1, 1, 0, 0, 0, 1, 0,
1, 1, 1, 0, 0, 0, 1};
int main() {
int i, j;
printf("Enter 4 bit data: ");
scanf("%s", data);
printf("Encoded Data: ");
for (i = 0; i < 7; i++) {
for (j = 0; j < 4; j++)
encoded[i] += ((data[j])*(gmatrix[j][i]));
encoded[i] = encoded[i] % 2;
printf("%d ", encoded[i]);
}
printf("\nEnter Encoded bits as received : ");
for (i = 0; i < 7; i++)
scanf("%d", &edata[i]);
for (i = 0; i < 3; i++) { //Generating Syndrome matrix
for (j = 0; j < 7; j++)
syn[i] += (hmatrix[i][j] * edata[j]);
syn[i] = syn[i] % 2;
}
for (i = 0; i < 7; i++) //finding Error Bit
if (syn[0] == hmatrix[0][i]
&& syn[1] == hmatrix[1][i]
&& syn[2] == hmatrix[2][i])
break;
if (i == 7) printf("Data is error free!!\n");
else {
printf("Error in data at bit position: %d\n", i + 1);
edata[i] = !edata[i]; //Complementing the bit value
printf("The Correct data Should be : ");
for (i = 0; i < 7; i++)
printf("%d ", edata[i]);
printf("\n");
}
return 0;
}
Output:
$ gcc hamming.c
$ ./a.out
[ENCODING] Enter 4 bit data: 1011
Encoded Data: 0 1 0 1 0 1 1
[DECODING]Enter Encoded bits as received : 0 1 0 1 0 1 0
Error received at bit number 7 of the data
The Correct data Should be : 0 1 0 1 0 1 1
KJ
Department Of Computer Science, YDIT, Bangalore. Page: 12/15
06CSL77: Networks Laboratory Manual C/C++ Programs
8) Write a program for congestion control using Leaky bucket algorithm.
//LeakyBucket.c
#include <stdio.h>
//IF U want the count of time elapsed remove the comments :)
//int time=0;
void bktInput(int a, int b, int size) {
if (a > size)
printf("\tBucket overflow.\n");
else {
sleep(1); //Generate equal time delay
// time++;
while (a > b) {
// printf("Time: %dseconds\n",time);
printf("\t%d bytes outputted\n", b);
a -= b;
sleep(1); //Generate equal time delay
// time++;
}
// printf("Time: %dseconds\n",time);
if (a > 0) printf("\tLast %d bytes sent\n", a);
printf("\tBucket output successful.\n");
}
}
int main() {
int op, pktSize, i, bktsize, n, delay;
printf("bucket Size : ");
scanf("%d", &bktsize);
printf("Output Rate : ");
scanf("%d", &op);
printf("No of packets : ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
delay = (rand() % 10); //Generate random rate of packet
sleep(delay); //sleep(No_Of_Seconds);
// time+=dealy;
pktSize = rand() % 1000;
// printf("Time: %dseconds\n",time);
printf("Packet no %d\tPacket size = %d\n", i, pktSize);
bktInput(pktSize, op, bktsize);
}
return 0;
}
Output:
$ gcc LeakyBucket.c
$ ./a.out
bucket Size : 512
Output Rate : 128
No of packets : 3
Packet no 0 Packet size = 886
Bucket overflow .
Packet no 1 Packet size = 103
Last 103 bytes sent
Bucket output successful.
Packet no 2 Packet size = 207
128 bytes outputted
Last 79 bytes sent
Bucket output successful. KJ
Department Of Computer Science, YDIT, Bangalore. Page: 13/15
06CSL77: Networks Laboratory Manual C/C++ Programs
Viva Questions
KJ
Department Of Computer Science, YDIT, Bangalore. Page: 15/15