0% found this document useful (0 votes)
31 views52 pages

CNS Lab

The document contains 6 assignments related to computer networks and security laboratory. Assignment 1 involves setting up a wired LAN using a layer 2 switch, testing cables and demonstrating ping packets. Assignment 2 demonstrates different network topologies and transmission media using packet tracer. Assignment 3 implements Hamming codes for error detection and correction of 7/8-bit ASCII codes. Assignment 4 simulates Go back N and selective repeat sliding window protocols. Assignment 5 involves subnetting and finding subnet masks. Assignment 6 implements the distance vector routing protocol to find suitable transmission paths.

Uploaded by

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

CNS Lab

The document contains 6 assignments related to computer networks and security laboratory. Assignment 1 involves setting up a wired LAN using a layer 2 switch, testing cables and demonstrating ping packets. Assignment 2 demonstrates different network topologies and transmission media using packet tracer. Assignment 3 implements Hamming codes for error detection and correction of 7/8-bit ASCII codes. Assignment 4 simulates Go back N and selective repeat sliding window protocols. Assignment 5 involves subnetting and finding subnet masks. Assignment 6 implements the distance vector routing protocol to find suitable transmission paths.

Uploaded by

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

COMPUTER NETWORKS & SECURITY LABORATORY

Name: SIDDHESH MAHENDRA MENGADE


Class: TE COMP ENGG
Roll no.: B-72
ASSIGNMENT NO. 01

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

Demonstrate the different types of topologies and types of transmission media by


using a packet tracer tool.
ASSIGNMENT NO. 03

Write a program for error detection and correction for 7/8 bits ASCII codes using

Hamming Codes.

Program Source Code:

#include <iostream>

int main()

int data[20]={0},c1,c2,c4,rdata[20]={0};

std::cout<<"Enter data bits for code(d7,d6,d5,d3):\n";

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::cout<<"encoded Code word: ";

for (int i = 7; i > 0; --i) {

std::cout << data[i] << " "; }

std::cout<<"\nEnter received data bit wise:\n";

for(int j=7; j>0; --j)

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

for (int k = 7; k > 0; --k) {

std::cout << rdata[k] << " "; }

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

std::cout<<"\nError Corrected data:\n";

for (int k = 7; k > 0; --k) {

std::cout << rdata[k] << " "; }

Program Output:

Enter data bits for code(d7,d6,d5,d3):

encoded Code word: 1 0 1 0 0 1 0

Enter received data bit wise:

Received data:

1 0 1 1 0 1 0

ERROR in message!

Error Corrected data:

1 0 1 0 0 1 0
ASSIGNMENT NO. 04

Write a program to simulate Go back N and Selective Repeat Modes of Sliding

Window Protocol in Peer-to-Peer mode.

Program Source Code at Client Side:

import java.lang.System;

import java.net.*;

import java.io.*;

public class Client {

static Socket connection;

public static void main(String a[]) throws SocketException {

try {

int v[] = new int[9];

//int g[] = new int[8];

int n = 0;

InetAddress addr = InetAddress.getByName("Localhost");

System.out.println(addr);

connection = new Socket(addr, 8011);

DataOutputStream out = new DataOutputStream(

connection.getOutputStream());

DataInputStream in = new DataInputStream(

connection.getInputStream());

int p = in.read();

System.out.println("No of frame is:" + p);

for (int i = 0; i < p; i++) {

v[i] = in.read();

System.out.println(v[i]);
//g[i] = v[i];

v[5] = -1;

for (int i = 0; i < p; i++)

System.out.println("Received frame is: " + v[i]);

for (int i = 0; i < p; i++)

if (v[i] == -1) {

System.out.println("Request to retransmit packet no "

+ (i+1) + " again!!");

n = i;

out.write(n);

out.flush();

System.out.println();

v[n] = in.read();

System.out.println("Received frame is: " + v[n]);

System.out.println("quiting");

} catch (Exception e) {

System.out.println(e);

}
}

Output at Client Side:

No of frame is:9

30

40

50

60

70

80

90

100

110

Received frame is: 30

Received frame is: 40

Received frame is: 50

Received frame is: 60

Received frame is: 70

Received frame is: -1

Received frame is: 90

Received frame is: 100

Received frame is: 110

Request to retransmit packet no 6 again!!

Received frame is: 80

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;

public class Server {

static ServerSocket Serversocket;

static DataInputStream dis;

static DataOutputStream dos;

public static void main(String[] args) throws SocketException {

try {

int a[] = { 30, 40, 50, 60, 70, 80, 90, 100, 110 };

Serversocket = new ServerSocket(8011);

System.out.println("waiting for connection");

Socket client = Serversocket.accept();

dis = new DataInputStream(client.getInputStream());

dos = new DataOutputStream(client.getOutputStream());

System.out.println("The number of packets sent is:" + a.length);

int y = a.length;

dos.write(y);

dos.flush();

for (int i = 0; i < a.length; i++) {

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

// TODO Auto-generated catch block

e.printStackTrace();

Program Output at Server Side:

waiting for connection

The number of packets sent is:9


ASSIGNMENT NO.05

Write a program to demonstrate Sub-netting and find subnet masks.

Program Source Code:

import java.io.*;

import java.net.InetAddress;

public class Subnet1 {

public static void main(String[] args) throws IOException {

System.out.println("ENTER IP:");

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

String ip = br.readLine();

String checkclass = ip.substring(0, 3);

int cc = Integer.parseInt(checkclass);

String mask = null;

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

if(cc>=128 && cc<=191)

mask = "255.255.0.0";

System.out.println("Class B IP Address");

System.out.println("SUBNET MASK:\n"+mask);
}

if(cc>=192 && cc<=223)

mask = "255.255.255.0";

System.out.println("Class C IP Address");

System.out.println("SUBNET MASK:\n"+mask);

if(cc>=224 && cc<=239)

mask = "255.0.0.0";

System.out.println("Class D IP Address Used for multicasting");

if(cc>=240 && cc<=254)

mask = "255.0.0.0";

System.out.println("Class E IP Address Experimental Use");

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+".";

System.out.println("First IP of block: "+networkAddr);

System.out.println("Last IP of block: "+lastAddr);

Program Output:

ENTER IP:

226.35.65.23

Class D IP Address Used for multicasting

First IP of block: 226.0.0.0.

Last IP of block: 226.255.255.255.

iotlab@iotlab-Veriton-M200-B360:~$ java Subnet1

ENTER IP:

192.168.100.5

Class C IP Address

SUBNET MASK:

255.255.255.0

First IP of block: 192.168.100.0.

Last IP of block: 192.168.100.255.


ASSIGNMENT NO. 06

Write a program to implement Distance vector routing protocol to find suitable path
for transmission.

Program Source Code:

#include<stdlib.h>

#define nul 1000

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

printf("\nEnter dist from the node %d to other nodes",r);

printf("\nPls enter 999 if there is no direct route\n",r);


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

if(i!=r)

printf("\nEnter dist to the node %d:",i);

scanf("%d",&router[r].a[i][2]);

router[r].a[i][3]=i;

void display(int r)

int i,j;

printf("\n\nThe routing table for node %d is as follows:",r);

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

if(router[r].a[i][2]>=999)

printf("\n\t\t\t %d \t no link \t no hop",router[r].a[i][1]);

else

printf("\n\t\t\t %d \t %d \t\t d",router[r].a[i][1],router[r].a[i][2],router[r].a[i][3]);

void dv_algo(int r)

int i,j,z;

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

if(router[r].a[i][2]!=999 && router[r].a[i][2]!=0)

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

printf("Enter the no. of nodes required (less than 10 pls):");

scanf("%d",&no);

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

init(i);

inp(i);

printf("\nThe configuration of the nodes after initialization is as follows:");

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

display(i);

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

dv_algo(i);

printf("\nThe configuration of the nodes after computation of paths is as follows:");


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

display(i);

while(1)

printf("\n\nWanna continue (y/n):");

scanf("%c",&choice);

if(choice=='n')

break;

printf("\nEnter the nodes btn which shortest path is to be found:\n");

scanf("%d %d",&x,&y);

printf("\nThe length of the shortest path is %d",router[x].a[y][2]);

Program Output:

Enter the no. of nodes required (less than 10 pls):4

Enter dist from the node 1 to other nodes

Pls enter 999 if there is no direct route

Enter dist to the node 2:5

Enter dist to the node 3:3

Enter dist to the node 4:7

Enter dist from the node 2 to other nodes

Pls enter 999 if there is no direct route

Enter dist to the node 1:5

Enter dist to the node 3:999

Enter dist to the node 4:6

Enter dist from the node 3 to other nodes

Pls enter 999 if there is no direct route

Enter dist to the node 1:3\

Enter dist to the node 2:


Enter dist to the node 4:

Enter dist from the node 4 to other nodes

Pls enter 999 if there is no direct route

Enter dist to the node 1:

Enter dist to the node 2:

Enter dist to the node 3:

The configuration of the nodes after initialization is as follows:

The routing table for node 1 is as follows:

101

252

333

474

The routing table for node 2 is as follows:

151

202

3 no link no hop

464

The routing table for node 3 is as follows:

131

2 no link no hop

303

4 no link no hop

The routing table for node 4 is as follows:

1 no link no hop

2 no link no hop

3 no link no hop

404

The configuration of the nodes after computation of paths is as follows:

The routing table for node 1 is as follows:


101

252

333

474

The routing table for node 2 is as follows:

151

202

381

464

The routing table for node 3 is as follows:

131

281

303

4 10 1

The routing table for node 4 is as follows:

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

a. Say Hello to each other


b. File transfer
c. Calculator

Program: ‘Say Hello to each other’

Client.c

#include <stdio.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <string.h>

int main()

int clientSocket;

char buffer[1024];

struct sockaddr_in serverAddr;

socklen_t addr_size;

/*—- Create the socket. The three arguments are: —-*/

/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */

clientSocket = socket(PF_INET, SOCK_STREAM, 0);

/*—- Configure settings of the server address struct —-*/

/* Address family = Internet */

serverAddr.sin_family = AF_INET;

/* Set port number, using htons function to use proper byte order */

serverAddr.sin_port = htons(7891);

/* Set IP address to localhost */

serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */

memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

/*—- Connect the socket to the server using the address struct —-*/

addr_size = sizeof serverAddr;

connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

/*—- Read the message from the server into the buffer —-*/

recv(clientSocket, buffer, 1024, 0);

/*—- Print the received message —-*/

printf("Data received: %s",buffer);

return 0;

Server.c

#include <stdio.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <string.h>

int main()

int welcomeSocket, newSocket;

char buffer[1024];

struct sockaddr_in serverAddr;

struct sockaddr_storage serverStorage;

socklen_t addr_size;
/*—- Create the socket. The three arguments are: —-*/

/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */

welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

/*—- Configure settings of the server address struct —-*/

/* Address family = Internet */

serverAddr.sin_family = AF_INET;

/* Set port number, using htons function to use proper byte order */

serverAddr.sin_port = htons(7891);

/* Set IP address to localhost */

serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

/* Set all bits of the padding field to 0 */

memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

/*—- Bind the address struct to the socket —-*/

bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

/*—- 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 —-*/

addr_size = sizeof serverStorage;

newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage,


&addr_size);
/*—- Send message to the socket of the incoming connection —-*/

strcpy(buffer,"Hello World\n");

send(newSocket,buffer,13,0);

return 0;

Program Output: ‘Say Hello to each other’

Client:

Data received: Hello World

Server:

Listening

Program: ‘File Transfer’

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

struct sockaddr_in serv_addr;

/* Create a socket first */

if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0)

printf("\n Error : Could not create socket \n");

return 1;

/* Initialize sockaddr_in data structure */

serv_addr.sin_family = AF_INET;

serv_addr.sin_port = htons(5000); // port

serv_addr.sin_addr.s_addr = inet_addr("172.16.6.168");

/* Attempt a connection */

if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)

printf("\n Error : Connect Failed \n");

return 1;

/* Create file where data will be stored */

FILE *fp;

fp = fopen("sample_file.txt", "ab");

if(NULL == fp)

printf("Error opening file");

return 1;
}

/* Receive data in chunks of 256 bytes */

while((bytesReceived = read(sockfd, recvBuff, 256)) > 0)

printf("Bytes received %d\n",bytesReceived);

// recvBuff[n] = 0;

fwrite(recvBuff, 1,bytesReceived,fp);

// printf("%s \n", recvBuff);

if(bytesReceived < 0)

printf("\n Read Error \n");

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;

struct sockaddr_in serv_addr;

char sendBuff[1024];

int numrv;

listenfd = socket(AF_INET, SOCK_STREAM, 0);

printf("Socket retrieve success\n");

memset(&serv_addr, '0', sizeof(serv_addr));

memset(sendBuff, '0', sizeof(sendBuff));

serv_addr.sin_family = AF_INET;

serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

serv_addr.sin_port = htons(5000);

bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));

if(listen(listenfd, 10) == -1)

printf("Failed to listen\n");

return -1;

}
while(1)

connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);

/* Open the file that we wish to transfer */

FILE *fp = fopen("sample_file.txt","rb");

if(fp==NULL)

printf("File opern error");

return 1;

/* Read data from file and send it */

while(1)

/* First read file in chunks of 256 bytes */

unsigned char buff[256]={0};

int nread = fread(buff,1,256,fp);

printf("Bytes read %d \n", nread);

/* If read was success, send data. */

if(nread > 0)

printf("Sending \n");

write(connfd, buff, nread);

if (nread < 256)

if (feof(fp))
printf("End of file\n");

if (ferror(fp))

printf("Error reading\n");

break;

close(connfd);

sleep(1);

return 0;

Program Output: ‘File Transfer’

Server:

Socket retrieve success

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>

//#define buffsize 150

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)

printf("socket created sucessfully\n");

struct sockaddr_in servaddr;

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");

servaddr.sin_port=6006;

sin_size = sizeof(struct sockaddr_in);

if((con=connect(sockfd,(struct sockaddr *) &servaddr, sin_size))==0); //initiate a


connection on a socket

printf("connect sucessful\n");

printf("Enter operation:\n +:Addition \n -: Subtraction \n /: Division \


n*:Multiplication \n");

scanf("%c",&operator);

printf("Enter operands:\n");

scanf("%d %d", &op1, &op2);

write(sockfd,&operator,10);

write(sockfd,&op1,sizeof(op1));

write(sockfd,&op2,sizeof(op2));

read(sockfd,&result,sizeof(result));

printf("Operation result from server=%d\n",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)

printf("socket created sucessfully\n"); //socket creation

struct sockaddr_in servaddr;

struct sockaddr_in clientaddr;

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");

servaddr.sin_port=6006;

if((bind(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr)))==0)

printf("bind sucessful\n"); //bind() assigns the

// address specified by addr to the socket referred to by the file

// descriptor sockfd. addrlen specifies the size, in bytes, of the

// address structure pointed to by addr. Traditionally, this operation is

// called “assigning a name to a socket”.

if((listen(sockfd,5))==0) //listen for connections on a socket

printf("listen sucessful\n");

sin_size = sizeof(struct sockaddr_in);

if((connfd=accept(sockfd,(struct sockaddr *)&clientaddr,&sin_size))>0);


printf("accept sucessful\n");

read(connfd, &operator,10);

read(connfd,&op1,sizeof(op1));

read(connfd,&op2,sizeof(op2));

switch(operator)

case '+':

result=op1 + op2;

printf("Result is: %d + %d = %d\n",op1, op2, result);

break;

case '-':

result=op1 - op2;

printf("Result is: %d - %d = %d\n",op1, op2, result);

break;

case '*':

result=op1 * op2;

printf("Result is: %d * %d = %d\n",op1, op2, result);

break;

case '/':

result=op1 / op2;

printf("Result is: %d / %d = %d\n",op1, op2, result);

break;

default:

printf("ERROR: Unsupported Operation");

write(connfd,&result,sizeof(result));

close(sockfd);
}

Program Output: ‘Calculator’

Client:

socket created sucessfully

connect sucessful

Enter operation:

+:Addition

-: Subtraction

/: Division

*:Multiplication

Enter operands:

10

15

Operation result from server=25

Server:

socket created sucessfully

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.

Program Source Code:

client:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#define SIZE 1024

void send_file_data(FILE* fp, int sockfd, struct sockaddr_in addr)

int n;

char buffer[SIZE];

// Sending the data

while (fgets(buffer, SIZE, fp) != NULL)

printf("[SENDING] Data: %s", buffer);

n = sendto(sockfd, buffer, SIZE, 0, (struct sockaddr*)&addr, sizeof(addr));

if (n == -1)

perror("[ERROR] sending data to the server.");

exit(1);

bzero(buffer, SIZE);
}

// Sending the 'END'

strcpy(buffer, "END");

sendto(sockfd, buffer, SIZE, 0, (struct sockaddr*)&addr, sizeof(addr));

fclose(fp);

int main(void)

// Defining the IP and Port

char *ip = "127.0.0.1";

const int port = 8080;

// Defining variables

int server_sockfd;

struct sockaddr_in server_addr;

char *filename = "client.txt";

FILE *fp = fopen(filename, "r");

// Creating a UDP socket

server_sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if (server_sockfd < 0)

perror("[ERROR] socket error");

exit(1);

}
server_addr.sin_family = AF_INET;

server_addr.sin_port = port;

server_addr.sin_addr.s_addr = inet_addr(ip);

// Reading the text file

if (fp == NULL)

perror("[ERROR] reading the file");

exit(1);

// Sending the file data to the server

send_file_data(fp, server_sockfd, server_addr);

printf("[SUCCESS] Data transfer complete.\n");

printf("[CLOSING] Disconnecting from the server.\n");

close(server_sockfd);

return 0;

server.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#define SIZE 1024


void write_file(int sockfd, struct sockaddr_in addr)

char* filename = "server.txt";

int n;

char buffer[SIZE];

socklen_t addr_size;

// Creating a file.

FILE* fp = fp = fopen(filename, "w");

// Receiving the data and writing it into the file.

while (1)

addr_size = sizeof(addr);

n = recvfrom(sockfd, buffer, SIZE, 0, (struct sockaddr*)&addr, &addr_size);

if (strcmp(buffer, "END") == 0)

break;

printf("[RECEVING] Data: %s", buffer);

fprintf(fp, "%s", buffer);

bzero(buffer, SIZE);

fclose(fp);
}

int main()

// Defining the IP and Port

char* ip = "127.0.0.1";

const int port = 8080;

// Defining variables

int server_sockfd;

struct sockaddr_in server_addr, client_addr;

char buffer[SIZE];

int e;

// Creating a UDP socket

server_sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if (server_sockfd < 0)

perror("[ERROR] socket error");

exit(1);

server_addr.sin_family = AF_INET;

server_addr.sin_port = port;

server_addr.sin_addr.s_addr = inet_addr(ip);

e = bind(server_sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));

if (e < 0)

{
perror("[ERROR] bind error");

exit(1);

printf("[STARTING] UDP File Server started. \n");

write_file(server_sockfd, client_addr);

printf("[SUCCESS] Data transfer complete.\n");

printf("[CLOSING] Closing the server.\n");

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.

Program Source Code:

import java.net.*;

import java.util.*;

public class IPDemo

public static void main(String[] args){

String host;

Scanner ch = new Scanner(System.in);

System.out.print("1.Enter Host Name \n2.Enter IP address \nChoice=");

int choice = ch.nextInt();

if(choice==1)

Scanner input = new Scanner(System.in);

System.out.print("\n Enter host name: ");

host = input.nextLine();

try {

InetAddress address = InetAddress.getByName(host);

System.out.println("IP address: " + address.getHostAddress());

System.out.println("Host name : " + address.getHostName());

System.out.println("Host name and IP address: " + address.toString());

catch (UnknownHostException ex) {

System.out.println("Could not find " + host);

}
else

Scanner input = new Scanner(System.in);

System.out.print("\n Enter IP address: ");

host = input.nextLine();

try {

InetAddress address = InetAddress.getByName(host);

System.out.println("Host name : " + address.getHostName());

System.out.println("IP address: " + address.getHostAddress());

System.out.println("Host name and IP address: " + address.toString());

catch (UnknownHostException ex) {

System.out.println("Could not find " + host);

Program Output:

1.Enter Host Name

2.Enter IP address

Choice=1

Enter host name: www.google.com

IP address: 172.217.160.196

Host name : www.google.com

Host name and IP address: www.google.com/172.217.160.196


ASSIGNMENT NO. 11

Installing and configure DHCP server and write a program to install the software on

remote machine.

Program Source Code:

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>

int main(int argc, char* argv[])

/*Variables*/

int sock,i=0;

struct sockaddr_in server;

int mysock;

char buffer[1024],command[1000];

int rval;

/*Create Sockets*/

sock = socket(AF_INET, SOCK_STREAM, 0);

if(sock<0)

perror("Failed to create Socket");

exit(1);

server.sin_family = AF_INET;

server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(5000);

/*Call Bind*/

if(bind(sock, (struct sockaddr *)&server, sizeof(server)))

perror("Bind Failed");

exit(1);

/*Listen*/

listen(sock, 5);

/*Accept*/

mysock = accept(sock, (struct sockaddr *) 0, 0);

if(mysock == -1)

perror("Accept Failed");

else

do

memset(buffer, 0, sizeof(buffer));

//Receiving command character by character from the


client

if((rval = recv(mysock, buffer, sizeof(buffer), 0))<0)

perror("Reading Stream Message error");

else if(rval == 0)

printf("Ending Connection\n");
//command[i] = '\0';

printf("\nCommand==%s\n",command);

//Executing the received command on the server

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

struct termios oldt,newt;

int ch;

tcgetattr( STDIN_FILENO, &oldt );

newt = oldt;

newt.c_lflag &= ~( ICANON | ECHO );

tcsetattr( STDIN_FILENO, TCSANOW, &newt );

ch = getchar();

tcsetattr( STDIN_FILENO, TCSANOW, &oldt );

return ch;

int main(int argc, char *argv[]){

int sock;

struct sockaddr_in server;

struct hostent *hp;

char buffer[1024], cbuff[10];

//Creating Socket

sock= socket(AF_INET, SOCK_STREAM, 0);

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

memcpy(&server.sin_addr, hp->h_addr, hp->h_length);

server.sin_port = htons(5000);

if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)

perror("connect failed. Error");

return 1;

puts("Connected\n");

//Accepting command

printf("Enter command: \t");

cbuff[0] = 1;

do {

cbuff[0] = mygetch();

cbuff[1] = '\0';

if( send(sock , cbuff , strlen(cbuff) , 0) < 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.

You might also like