CNS Lab
CNS Lab
Setup a wired LAN using Layer 2 Switch. It includes preparation of cable, testing of
cable using line tester, configuration machine using IP addresses, testing using
PING utility and demonstrating the PING packets captured traces using Wireshark
Packet Analyzer Tool.
ASSIGNMENT NO. 02
Write a program for error detection and correction for 7/8 bits ASCII codes using
Hamming Codes.
#include <iostream>
int main()
int data[20]={0},c1,c2,c4,rdata[20]={0};
std::cin>>data[7];
std::cin>>data[6];
std::cin>>data[5];
std::cin>>data[3];
data[1]=data[3]^data[5]^data[7];
data[2]=data[3]^data[6]^data[7];
data[4]=data[5]^data[6]^data[7];
{ std::cin>>rdata[j]; }
c1=rdata[3]^rdata[5]^rdata[7];
c2=rdata[3]^rdata[6]^rdata[7];
c4=rdata[5]^rdata[6]^rdata[7];
std::cout<<"\nReceived data:\n";
if(rdata[1]!=data[1])
std::cout<<"\nERROR in message!";
if(rdata[1]==0)
rdata[1]=1;
else
rdata[1]=0;
else if(rdata[2]!=data[2])
std::cout<<"\nERROR in message!";
if(rdata[2]==0)
rdata[2]=1;
else
rdata[2]=0;
else if(rdata[4]!=data[4])
std::cout<<"\nERROR in message!";
if(rdata[4]==0)
rdata[4]=1;
else
rdata[4]=0;
}
else
std::cout<<"NO ERROR!\n";
Program Output:
Received data:
1 0 1 1 0 1 0
ERROR in message!
1 0 1 0 0 1 0
ASSIGNMENT NO. 04
import java.lang.System;
import java.net.*;
import java.io.*;
try {
int n = 0;
System.out.println(addr);
connection.getOutputStream());
connection.getInputStream());
int p = in.read();
v[i] = in.read();
System.out.println(v[i]);
//g[i] = v[i];
v[5] = -1;
if (v[i] == -1) {
n = i;
out.write(n);
out.flush();
System.out.println();
v[n] = in.read();
System.out.println("quiting");
} catch (Exception e) {
System.out.println(e);
}
}
No of frame is:9
30
40
50
60
70
80
90
100
110
quiting
Program Source Code at Server Side:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
try {
int a[] = { 30, 40, 50, 60, 70, 80, 90, 100, 110 };
int y = a.length;
dos.write(y);
dos.flush();
dos.write(a[i]);
dos.flush();
int k = dis.read();
dos.write(a[k]);
dos.flush();
} catch (IOException e) {
System.out.println(e);
} finally {
try {
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
import java.io.*;
import java.net.InetAddress;
System.out.println("ENTER IP:");
String ip = br.readLine();
int cc = Integer.parseInt(checkclass);
if(cc>0)
if(cc<=127)
mask = "255.0.0.0";
System.out.println("Class A IP Address");
System.out.println("SUBNET MASK:\n"+mask);
mask = "255.255.0.0";
System.out.println("Class B IP Address");
System.out.println("SUBNET MASK:\n"+mask);
}
mask = "255.255.255.0";
System.out.println("Class C IP Address");
System.out.println("SUBNET MASK:\n"+mask);
mask = "255.0.0.0";
mask = "255.0.0.0";
String networkAddr="";
String lastAddr="";
String[] ipAddrParts=ip.split("\\.");
String[] maskParts=mask.split("\\.");
for(int i=0;i<4;i++){
int x=Integer.parseInt(ipAddrParts[i]);
int y=Integer.parseInt(maskParts[i]);
int z=x&y;
networkAddr+=z+".";
int w=z|(y^255);
lastAddr+=w+".";
Program Output:
ENTER IP:
226.35.65.23
ENTER IP:
192.168.100.5
Class C IP Address
SUBNET MASK:
255.255.255.0
Write a program to implement Distance vector routing protocol to find suitable path
for transmission.
#include<stdlib.h>
#define nodes 10
int no;
struct node
int a[nodes][4];
}router[nodes];
void init(int r)
int i;
for(i=1;i<=no;i++)
router[r].a[i][1]=i;
router[r].a[i][2]=999;
router[r].a[i][3]=nul;
router[r].a[r][2]=0;
router[r].a[r][3]=r;
void inp(int r)
int i;
if(i!=r)
scanf("%d",&router[r].a[i][2]);
router[r].a[i][3]=i;
void display(int r)
int i,j;
for(i=1;i<=no;i++)
if(router[r].a[i][2]>=999)
else
void dv_algo(int r)
int i,j,z;
for(i=1;i<=no;i++)
{
for(j=1;j<=no;j++)
z=router[r].a[i][2]+router[i].a[j][2];
if(router[r].a[j][2]>z)
router[r].a[j][2]=z;
router[r].a[j][3]=i;
int main()
int i,j,x,y;
char choice;
scanf("%d",&no);
for(i=1;i<=no;i++)
init(i);
inp(i);
for(i=1;i<=no;i++)
display(i);
for(i=1;i<=no;i++)
dv_algo(i);
display(i);
while(1)
scanf("%c",&choice);
if(choice=='n')
break;
scanf("%d %d",&x,&y);
Program Output:
101
252
333
474
151
202
3 no link no hop
464
131
2 no link no hop
303
4 no link no hop
1 no link no hop
2 no link no hop
3 no link no hop
404
252
333
474
151
202
381
464
131
281
303
4 10 1
1 no link no hop
2 no link no hop
3 no link no hop
404
ASSIGNMENT NO. 07
Use packet Tracer tool for configuration of 3 router network using RIP protocol.
ASSIGNMENT NO. 08
Write a program using TCP socket for wired network for following
Client.c
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
int clientSocket;
char buffer[1024];
socklen_t addr_size;
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
/*—- Connect the socket to the server using the address struct —-*/
/*—- Read the message from the server into the buffer —-*/
return 0;
Server.c
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
char buffer[1024];
socklen_t addr_size;
/*—- Create the socket. The three arguments are: —-*/
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/*—- Listen on the socket, with 5 max connection requests queued —-*/
if(listen(welcomeSocket,5)==0)
printf("Listening\n");
else
printf("Error\n");
/*—- Accept call creates a new socket for the incoming connection —-*/
strcpy(buffer,"Hello World\n");
send(newSocket,buffer,13,0);
return 0;
Client:
Server:
Listening
Client.c
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(void)
int sockfd = 0;
int bytesReceived = 0;
char recvBuff[256];
memset(recvBuff, '0', sizeof(recvBuff));
return 1;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("172.16.6.168");
/* Attempt a connection */
return 1;
FILE *fp;
fp = fopen("sample_file.txt", "ab");
if(NULL == fp)
return 1;
}
// recvBuff[n] = 0;
fwrite(recvBuff, 1,bytesReceived,fp);
if(bytesReceived < 0)
return 0;
Server.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int main(void)
int listenfd = 0;
int connfd = 0;
char sendBuff[1024];
int numrv;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
printf("Failed to listen\n");
return -1;
}
while(1)
if(fp==NULL)
return 1;
while(1)
if(nread > 0)
printf("Sending \n");
if (feof(fp))
printf("End of file\n");
if (ferror(fp))
printf("Error reading\n");
break;
close(connfd);
sleep(1);
return 0;
Server:
Bytes read 0
End of file
Program: ‘Calculator’
Client.c
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include <unistd.h>
#include<string.h>
#include<strings.h>
#include <arpa/inet.h>
void main()
{
int b,sockfd,sin_size,con,n,len;
char operator;
int op1,op2,result;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>0)
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=6006;
printf("connect sucessful\n");
scanf("%c",&operator);
printf("Enter operands:\n");
write(sockfd,&operator,10);
write(sockfd,&op1,sizeof(op1));
write(sockfd,&op2,sizeof(op2));
read(sockfd,&result,sizeof(result));
close(sockfd);
Server.c
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include <unistd.h>
#include<string.h>
#include <arpa/inet.h>
void main()
int b,sockfd,connfd,sin_size,l,n,len;
char operator;
int op1,op2,result;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>0)
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=6006;
printf("listen sucessful\n");
read(connfd, &operator,10);
read(connfd,&op1,sizeof(op1));
read(connfd,&op2,sizeof(op2));
switch(operator)
case '+':
result=op1 + op2;
break;
case '-':
result=op1 - op2;
break;
case '*':
result=op1 * op2;
break;
case '/':
result=op1 / op2;
break;
default:
write(connfd,&result,sizeof(result));
close(sockfd);
}
Client:
connect sucessful
Enter operation:
+:Addition
-: Subtraction
/: Division
*:Multiplication
Enter operands:
10
15
Server:
bind sucessful
listen sucessful
accept sucessful
Result is: 10 + 15 = 25
ASSIGNMENT NO. 09
Write a program using UDP Sockets to enable file transfer (Script) between two
machines.
client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int n;
char buffer[SIZE];
if (n == -1)
exit(1);
bzero(buffer, SIZE);
}
strcpy(buffer, "END");
fclose(fp);
int main(void)
// Defining variables
int server_sockfd;
if (server_sockfd < 0)
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
if (fp == NULL)
exit(1);
close(server_sockfd);
return 0;
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int n;
char buffer[SIZE];
socklen_t addr_size;
// Creating a file.
while (1)
addr_size = sizeof(addr);
if (strcmp(buffer, "END") == 0)
break;
bzero(buffer, SIZE);
fclose(fp);
}
int main()
char* ip = "127.0.0.1";
// Defining variables
int server_sockfd;
char buffer[SIZE];
int e;
if (server_sockfd < 0)
exit(1);
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
if (e < 0)
{
perror("[ERROR] bind error");
exit(1);
write_file(server_sockfd, client_addr);
close(server_sockfd);
return 0;
}
ASSIGNMENT NO. 10
Write a program for DNS lookup. Given an IP address as input, it should return URL
and vice-versa.
import java.net.*;
import java.util.*;
String host;
if(choice==1)
host = input.nextLine();
try {
}
else
host = input.nextLine();
try {
Program Output:
2.Enter IP address
Choice=1
IP address: 172.217.160.196
Installing and configure DHCP server and write a program to install the software on
remote machine.
Server.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<string.h>
#include<stdlib.h>
/*Variables*/
int sock,i=0;
int mysock;
char buffer[1024],command[1000];
int rval;
/*Create Sockets*/
if(sock<0)
exit(1);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(5000);
/*Call Bind*/
perror("Bind Failed");
exit(1);
/*Listen*/
listen(sock, 5);
/*Accept*/
if(mysock == -1)
perror("Accept Failed");
else
do
memset(buffer, 0, sizeof(buffer));
else if(rval == 0)
printf("Ending Connection\n");
//command[i] = '\0';
printf("\nCommand==%s\n",command);
system(command);
break;
else
system("clear");
command[i] = buffer[0];
command[i+1] = '\0';
printf("%s\n",command);
i++;
}while(1);
close(mysock);
return 0;
Client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
int mygetch( ) {
int ch;
newt = oldt;
ch = getchar();
return ch;
int sock;
//Creating Socket
if(sock<0)
perror("Socket Failed");
close(sock);
exit(1);
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if(hp==0)
{
perror("gethostbynme Failed");
close(sock);
exit(1);
server.sin_port = htons(5000);
return 1;
puts("Connected\n");
//Accepting command
cbuff[0] = 1;
do {
cbuff[0] = mygetch();
cbuff[1] = '\0';
puts("Send failed");
return 1;
printf("%s",cbuff);
}while(cbuff[0] != '\n');
return 0;
Program Output:
Client:
Connected
Enter command: ls
ASSIGNMENT NO. 12
Capture packets using Wireshark, write the exact packet capture filter expressions
to accomplish the following and save the output in file:
1. Capture all TCP traffic to/from Facebook, during the time when you log in to
your Facebook account.
2. Capture all HTTP traffic to/from Facebook, when you log in to your Facebook
account.
3. Write a DISPLAY filter expression to count all TCP packets (captured under
item #1) that have the flags SYN, PSH, and RST set. Show the fraction of
packets that had each flag set.
4. Count how many TCP packets you received from / sent to Face book, and
how many of each were also HTTP packets.
Solution
To capture packets using Wireshark and apply specific filters, you can use the
following expressions:
To capture all TCP traffic to/from Facebook during the time you log in to your
Facebook account, use the following packet capture filter expression:
host facebook.com and tcp
To save the output to a file, you can go to "File" -> "Save As" and choose the
desired file format.
To capture all HTTP traffic to/from Facebook when you log in to your account, use
the following packet capture filter expression:
host facebook.com and http
Again, you can save the output to a file using the "Save As" option.
To write a display filter expression to count all TCP packets (captured under item
#1) that have the flags SYN, PSH, and RST set, use the following display filter
expression:
tcp.flags.syn == 1 and tcp.flags.psh == 1 and tcp.flags.rst == 1
This will count all TCP packets with the SYN, PSH, and RST flags set. To show the
fraction of packets that had each flag set, you can use the "Statistics" ->
"Conversations" option in Wireshark and select the desired columns to display the
statistics.
To count how many TCP packets you received from/sent to Facebook and how many
of each were also HTTP packets, you can use the following display filter expression:
ip.addr == <your IP address> and tcp.port == <your Facebook port> and
http
Replace <your IP address> with your actual IP address and <your Facebook port>
with the port number used for Facebook communication. This filter will count the
TCP packets exchanged with Facebook and also filter for HTTP packets.
Remember to adapt the expressions to your specific network setup and replace any
placeholders with the appropriate values.
ASSIGNMENT NO. 13
To study the SSL protocol by capturing the packets using Wireshark tool while
visiting any SSL secured website (e-commerce).
ASSIGNMENT NO. 14
To study the IPsec (ESP and AH) protocol by capturing the packets using Wireshark
tool.