0% found this document useful (0 votes)
103 views50 pages

CNS LAB Manual Print

This document contains the practical lab manual for the subject "Computer Networks and Security" for semester 5 students. It outlines 6 practical experiments to be performed related to computer networks including setting up a wired LAN, implementing error detection and correction using Hamming codes and CRC, simulating sliding window protocols, demonstrating subnetting, implementing a basic TCP socket program, and writing a program for DNS lookup. It provides the format for students to fill in their details for each experiment along with space to include the program code and output.

Uploaded by

Pranesh Khond
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)
103 views50 pages

CNS LAB Manual Print

This document contains the practical lab manual for the subject "Computer Networks and Security" for semester 5 students. It outlines 6 practical experiments to be performed related to computer networks including setting up a wired LAN, implementing error detection and correction using Hamming codes and CRC, simulating sliding window protocols, demonstrating subnetting, implementing a basic TCP socket program, and writing a program for DNS lookup. It provides the format for students to fill in their details for each experiment along with space to include the program code and output.

Uploaded by

Pranesh Khond
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/ 50

JCEI’s JAIHIND COLLEGE ENGINEERING, KURAN

“PRACTICAL LAB MANUAL”

UNDER THE SUBJECT


“COMPUTER NETWORKS AND SECURITY”
(310247)
TE-COMPUTER ENGINEERING

DEPARTMENT OF COMPUTER ENGINEERING

SEMESTER-V

Name of Student: ____________________________________

Exam Seat Number: __________________________________

Name of Faculty: Prof S. K. Said.

TEACHING SCHEME EXAMINATION SCHEME


Practical: 2Hrs/Week Oral: 25 Marks
Teaching: 3Hrs/Week Term Work: 25 Marks
JCEI’s Jaihind College of Engineering, Kuran 2021-2022
INDEX

Sr. No. Date TITLE Sign Remark

1. 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.
2. Write a program for error detection and
correction for 7/8 bits ASCII codes using
Hamming Codes or CRC.
3. Write a program to simulate Go back N and
Selective Repeat Modes of Sliding Window
Protocol in Peer-to-Peer mode.
4. Write a program to demonstrate Sub-netting
and find subnet masks.
5. Write a program using TCP socket for wired
network for following
a. Say Hello to Each other
b. File transfer
c. Calculator
6. Write a program for DNS lookup. Given an IP
address as input, it should return URL and vice-
versa.

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Practical No.1. (Group A)

Setup a wired LAN using Layer 2 Switch. It includes preparation


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

Subject Computer Networks and Security

Name

Class Third Year. (Computer Engineering.)

Roll No.

Exam Seat No.

Date

Signature

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Practical No.2. (Group A)

Write a program for error detection and correction for 7/8 bits
Title ASCII codes using Hamming Codes or CRC.

Subject Computer Networks and Security

Name

Class Third Year. (Computer Engineering.)

Roll No.

Exam Seat No.

Date

Signature

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: CRC
#include <stdio.h>
#include <string.h>
void main()
{
int i,j,keylen,msglen;
char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
printf("Enter Data: ");
scanf("%s",input);
printf("Enter Key: ");
scanf("%s",key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);
for (i=0;i<keylen-1;i++) {
input[msglen+i]='0';
}
for (i=0;i<keylen;i++)
temp[i]=input[i];

for (i=0;i<msglen;i++) {
quot[i]=temp[0];
if(quot[i]=='0')
for (j=0;j<keylen;j++)
key[j]='0';
else
for (j=0;j<keylen;j++)
key[j]=key1[j];
for (j=keylen-1;j>0;j--) {
if(temp[j]==key[j])
rem[j-1]='0'; else
rem[j-1]='1';
}
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("\nQuotient is ");
for (i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("\nRemainder is ");
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\nFinal data is: ");
for (i=0;i<msglen;i++)
printf("%c",input[i]);
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\n");
}

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output: CRC

ubuntu@linux:~$ g++ hamming.c && ./a.out


Enter 4 bits of data one by one
1
0
1
1
Encoded data is
1010101
Enter received data bits one by one
1
0
1
0
1
0
1
No error while transmission of data

ubuntu@linux:~$ g++ hamming.c && ./a.out


Enter 4 bits of data one by one
1
0
1
1
Encoded data is
1010101
Enter received data bits one by one
1
0
1
0
1
1
1
Error on position 2
Data sent : 1010101
Data received : 1010111
Correct message is
1
0
1
0
1
0
1

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: Hamming
#include<stdio.h>
int main()
{
int data[10];
int dataatrec[10],c,c1,c2,c3,i;
printf("Enter 4 bits of data one by one\n");
scanf("%d",&data[0]);
scanf("%d",&data[1]);
scanf("%d",&data[2]);
scanf("%d",&data[4]);
//Calculation of even parity
data[6]=data[0]^data[2]^data[4];
data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
printf("\nEncoded data is\n");
for(i=0;i<7;i++)
printf("%d",data[i]);
printf("\n\nEnter received data bits one by one\n");
for(i=0;i<7;i++)
scanf("%d",&dataatrec[i]);
c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
c=c3*4+c2*2+c1 ;
if(c==0)
{
printf("\nNo error while transmission of data\n");
}
else
{
printf("\nError on position %d",c);
printf("\nData sent : ");
for(i=0;i<7;i++)

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


printf("%d",data[i]);
printf("\nData received : ");
for(i=0;i<7;i++)
printf("%d",dataatrec[i]);
printf("\nCorrect message is\n");
//if errorneous bit is 0 we complement it else vice versa
if(dataatrec[7-c]==0)
dataatrec[7-c]=1;
else
dataatrec[7-c]=0;
for (i=0;i<7;i++)
{
printf("%d",dataatrec[i]);
printf("\n");
}
}
return 0;
}

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output: Hamming
ubuntu@linux:~$ g++ hamming.c && ./a.out
Enter 4 bits of data one by one
1
0
1
1
Encoded data is
1010101
Enter received data bits one by one
1
0
1
0
1
0
1
No error while transmission of data
ubuntu@linux:~$ g++ hamming.c && ./a.out
Enter 4 bits of data one by one
1
0
1
1
Encoded data is
1010101
Enter received data bits one by one
1
0
1
0
1
1
1

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Error on position 2
Data sent : 1010101
Data received : 1010111
Correct message is
1
0
1
0
1
0
1

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Practical No.3. (Group A)

Write a program to simulate Go back N and Selective Repeat


Title Modes of Sliding Window Protocol in Peer-to-Peer mode.

Subject Computer Networks and Security

Name

Class Third Year. (Computer Engineering.)

Roll No.

Exam Seat No.

Date

Signature

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: Test Server
import java.io.*;
import java.net.*;
import java.util.*;
class testserver
{
public static void main(String args[])throws IOException
{
System.out.println("server Waiting for connection....");
InetAddress addr=InetAddress.getByName("Localhost");
ServerSocket ss=new ServerSocket(5000);

Socket client=new Socket();


client=ss.accept();

BufferedInputStream in=new BufferedInputStream(client.getInputStream());


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

System.out.println("Received request for sending frames");


int p=in.read(); //read no of frames sent by client

boolean f[]=new boolean[p];

int pc=in.read(); //read choice sent by client


System.out.println("Sending....");

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

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


System.out.println("sending frame number "+i);
out.write(i); //send frame on server socket
out.flush();
System.out.println("Waiting for acknowledgement");
try
{
Thread.sleep(7000);
}
catch(Exception e){}

int a=in.read(); //read ack on servers socket from client


System.out.println("received acknowledgement for frame "+i+" as "+a);
}
out.flush();
}
else
{
for(int i=0;i<p;++i)
{
if(i==2)
{
System.out.println("sending frame no "+i); //sent frame 2
}
else
{
System.out.println("sending frame no "+i);
out.write(i); //write 0 and 1 and 3 frame
out.flush();
System.out.println("Waiting for acknowledgement ");
try

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


{
Thread.sleep(7000);
}
catch(Exception e){}

int a=in.read(); //Read NACK

if(a!=255)
{
System.out.println("received ack for frame no: "+i+" as "+a);
f[i]=true;
}
}// end of inner else
}// end of for

// check which frames have not been ack

for(int a=0;a<p;++a)
{
if(f[a]==false)
{
System.out.println("Resending frame "+a);
out.write(a);
out.flush();
System.out.println("Waiting for ack ");
try
{
Thread.sleep(5000);
}
catch(Exception e){}

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


int b=in.read();
System.out.println("received ack for frame no: "+a+" as "+b);
f[a]=true;
}
}
out.flush();
}// end of else which is for error

in.close();
out.close();
client.close();
ss.close();
System.out.println("Quiting");

}// end main method


}// end main class

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output: Test Server
ubuntu@linux:~$ javac testserver.java && java testserver
server Waiting for connection....
Received request for sending frames
Sending....
sending frame no 0
Waiting for acknowledgement
received ack for frame no: 0 as 0
sending frame no 1
Waiting for acknowledgement
received ack for frame no: 1 as 1
sending frame no 2
sending frame no 3
Waiting for acknowledgement
sending frame no 4
Waiting for acknowledgement
sending frame no 5
Waiting for acknowledgement
sending frame no 6
Waiting for acknowledgement
sending frame no 7
Waiting for acknowledgement
Resending frame 2
Waiting for ack
received ack for frame no: 2 as 2
Resending frame 3
Waiting for ack
received ack for frame no: 3 as 3
Resending frame 4
Waiting for ack

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


received ack for frame no: 4 as 4
Resending frame 5
Waiting for ack
received ack for frame no: 5 as 5
Resending frame 6
Waiting for ack
received ack for frame no: 6 as 6
Resending frame 7
Waiting for ack
received ack for frame no: 7 as 7
Quiting
ubuntu@linux:~$ javac testserver.java && java testserver
server Waiting for connection....
Received request for sending frames
Sending....
sending frame number 0
Waiting for acknowledgement
received acknowledgement for frame 0 as 0
sending frame number 1
Waiting for acknowledgement
received acknowledgement for frame 1 as 1
Quiting
ubuntu@linux:~$

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: Test Client
import java.io.*;
import java.net.*;
import java.math.*;
import java.util.*;

class testclient
{

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


{
InetAddress addr=InetAddress.getByName("Localhost");
System.out.println(addr);

Socket connection=new Socket(addr,5000);

BufferedInputStream in=new BufferedInputStream(connection.getInputStream());


DataOutputStream out=new DataOutputStream(connection.getOutputStream());
Scanner scr=new Scanner(System.in);// this will be used to accept i/p from console

System.out.println(" client is Connected to server" + addr);


System.out.println("Enter the number of frames to be requested to the server");
int c=scr.nextInt();

out.write(c); // write no of frames on client socket


out.flush();

System.out.println("Enter the type of trans. Error=1 ; No Error=0");


int choice=scr.nextInt();
out.write(choice); //write choice on socket

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


int check=0;
int i=0;
int j=0;

if(choice==0)
{
for(j=0;j<c;++j)
{
i=in.read(); //read all frames one by one from server
System.out.println("received frame no: "+i);
System.out.println("Sending acknowledgement for frame no: "+i);
out.write(i); //write ack to socket
out.flush();
}
out.flush();
}
else
{
for(j=0;j<c;++j)
{
i=in.read(); //read 0,1,2,3 frame
if(i==check)
{
System.out.println("received frame no: "+i);
System.out.println("Sending acknowledgement for frame no: "+i);
out.write(i); //sent ack of frame 0,1
++check;
}
else
{

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


--j;
System.out.println("Discarded frame no: "+i);
System.out.println("Sending NEGATIVE ack");
out.write(-1);
}
out.flush();
}
}//end of else for error

in.close();
out.close();
System.out.println("Quiting");

}// end of main method


}// end of main class

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output: Test Client
ubuntu@linux:~$ javac testclient.java && java testclient
Localhost/127.0.0.1
client is Connected to serverLocalhost/127.0.0.1
Enter the number of frames to be requested to the server
8
Enter the type of trans. Error=1 ; No Error=0
1
received frame no: 0
Sending acknowledgement for frame no: 0
received frame no: 1
Sending acknowledgement for frame no: 1
Discarded frame no: 3
Sending NEGATIVE ack
Discarded frame no: 4
Sending NEGATIVE ack
Discarded frame no: 5
Sending NEGATIVE ack
Discarded frame no: 6
Sending NEGATIVE ack
Discarded frame no: 7
Sending NEGATIVE ack
received frame no: 2
Sending acknowledgement for frame no: 2
received frame no: 3
Sending acknowledgement for frame no: 3
received frame no: 4
Sending acknowledgement for frame no: 4
received frame no: 5
Sending acknowledgement for frame no: 5

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


received frame no: 6
Sending acknowledgement for frame no: 6
received frame no: 7
Sending acknowledgement for frame no: 7
Quiting
ubuntu@linux:~$ javac testclient.java && java testclient
Localhost/127.0.0.1
client is Connected to serverLocalhost/127.0.0.1
Enter the number of frames to be requested to the server
2
Enter the type of trans. Error=1 ; No Error=0
0
received frame no: 0
Sending acknowledgement for frame no: 0
received frame no: 1
Sending acknowledgement for frame no: 1
Quiting
ubuntu@linux:~$

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: Server
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 };
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();

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


}

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

}
}
}

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output: Server

ubuntu@linux:~$ javac Server.java && java Server

waiting for connection

The number of packets sent is:8

ubuntu@linux:~$

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: Client

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[8];

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

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


}

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

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output: Client

ubuntu@linux:~$ javac Client.java && java Client

Localhost/127.0.0.1

No of frame is:8

30

40

50

60

70

80

90

100

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

Request to retransmit from packet no 6 again!!

Received frame is: 80

quitingt's

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Practical No.4 (Group B)

Write a program to demonstrate Sub-netting and find subnet


Title masks.

Subject Computer Networks and Security

Name

Class Third Year. (Computer Engineering.)

Roll No.

Exam Seat No.

Date

Signature

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program:

import java.util.Scanner;
class Subnet{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the ip address: ");
String ip = sc.nextLine();
String split_ip[] = ip.split("\\."); //SPlit the string after every .
String split_bip[] = new String[4]; //split binary ip
String bip = "";
String mask1="";

int firstoctet = Integer.parseInt(split_ip[0]);


if(firstoctet<=127)
{
mask1 = "255.0.0.0";
System.out.println("Class A IP Address");
System.out.println("Default mask is: "+mask1);
}
else if(firstoctet>=128 && firstoctet<=191)
{
mask1 = "255.255.0.0";
System.out.println("Class B IP Address");
System.out.println("Default mask is: "+mask1);
}
else if(firstoctet>=192 && firstoctet<=223)
{
mask1 = "255.255.255.0";
System.out.println("Class C IP Address");
System.out.println("Defaultt mask is: "+mask1);
}
for(int i=0;i<4;i++)
{
split_bip[i] = appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i]))); // "18" => 18 => 10010
=> 00010010
bip += split_bip[i];
}
System.out.println("IP in binary is "+bip);
System.out.print("Enter the number of subnets: ");
int n = sc.nextInt();

//Calculation of mask
double bits = (int)Math.ceil(Math.log(n)/Math.log(2)); /*eg if address = 120, log 120/log 2 gives log to
the base 2 => 6.9068, ceil gives us upper integer */
int y=(int) bits;
System.out.println("Number of bits borrowd from host to network are = "+y);
int z=8-y;
System.out.println("Number of host bits are = "+z);

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


//int mask = 32-bits;
double mask=256-(Math.pow(2.0,z));
int x = (int) mask;
System.out.println("New subnet mask is = "+x);
int sub_size=256-x;
System.out.println("Subnet Size = "+sub_size);

//Calculation of first address and last address


int fbip[] = new int[32];
for(int i=0; i<32;i++)
fbip[i] = (int)bip.charAt(i)-48; //convert cahracter 0,1 to integer 0,1
for(int i=31;i>31-z;i--)//Get first address by ANDing last n bits with 0
fbip[i] &= 0;
String fip[] ={"","","",""};
for(int i=0;i<32;i++)
fip[i/8] = new String(fip[i/8]+fbip[i]);
System.out.println("first Subnet Details");
System.out.print("first N/W address is = ");
for(int i=0;i<4;i++){
System.out.print(Integer.parseInt(fip[i],2));
if(i!=3) System.out.print(".");
}
System.out.println();

int lbip[] = new int[32];


for(int i=0; i<32;i++)
lbip[i] = (int)bip.charAt(i)-48; //convert cahracter 0,1 to integer 0,1
for(int i=31;i>31-z;i--)//Get last address by ORing last n bits with 1
lbip[i] |= 1;
String lip[] = {"","","",""};
for(int i=0;i<32;i++)
lip[i/8] = new String(lip[i/8]+lbip[i]);
System.out.print("Broadcast address is = ");
for(int i=0;i<4;i++){
System.out.print(Integer.parseInt(lip[i],2));
if(i!=3) System.out.print(".");
}
System.out.println();
}
static String appendZeros(String s)
{
String temp = new String("00000000");
return temp.substring(s.length())+ s;
}
}

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output:

ubuntu@linux:~$ javac Subnet.java && java Subnet


Enter the ip address: 127.0.0.1
Class A IP Address
Default mask is: 255.0.0.0
IP in binary is 01111111000000000000000000000001
Enter the number of subnets: 2
Number of bits borrowd from host to network are = 1
Number of host bits are = 7
New subnet mask is = 128
Subnet Size = 128
first Subnet Details
first N/W address is = 127.0.0.0
Broadcast address is = 127.0.0.127
ubuntu@linux:~$ javac Subnet.java && java Subnet
Enter the ip address: 63.0.0.1
Class A IP Address
Default mask is: 255.0.0.0
IP in binary is 00111111000000000000000000000001
Enter the number of subnets: 1
Number of bits borrowd from host to network are = 0
Number of host bits are = 8
New subnet mask is = 0
Subnet Size = 256
first Subnet Details
first N/W address is = 63.0.0.0
Broadcast address is = 63.0.0.255
ubuntu@linux:~$ javac Subnet.java && java Subnet
Enter the ip address: 191.0.0.1
Class B IP Address
Default mask is: 255.255.0.0
IP in binary is 10111111000000000000000000000001
Enter the number of subnets: 3
Number of bits borrowd from host to network are = 2
Number of host bits are = 6
New subnet mask is = 192
Subnet Size = 64
first Subnet Details
first N/W address is = 191.0.0.0
Broadcast address is = 191.0.0.63
ubuntu@linux:~$ javac Subnet.java && java Subnet
Enter the ip address: 255.0.0.1
IP in binary is 11111111000000000000000000000001
Enter the number of subnets: 4
Number of bits borrowd from host to network are = 2
Number of host bits are = 6
New subnet mask is = 192
Subnet Size = 64
first Subnet Details
first N/W address is = 255.0.0.0
Broadcast address is = 255.0.0.63
ubuntu@linux:~$

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Practical No.5 (Group B)

Write a program using TCP socket for wired network for


Title following:
a. Say Hello to Each other
b. File transfer
c. Calculator

Subject Computer Networks and Security

Name

Class Third Year. (Computer Engineering.)

Roll No.

Exam Seat No.

Date

Signature

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: a. Say Hello (Server)

#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>
#include<netdb.h>
void main()
{
int b,sockfd,connfd,sin_size,l,n,len;
char buff[256];
char buff1[256];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>=0) //socket creation
printf("socket created sucessfully \n"); //on success 0 otherwise -1

struct sockaddr_in servaddr;


struct sockaddr_in clientaddr;

//Assign server object parameters


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

//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((b=bind(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr)))==0)
printf("bind Sucessful \n");

//listen for connections on a socket


l=listen(sockfd,5);
printf("listen Sucessful \n");

//accept call accepts connection from client


sin_size = sizeof(clientaddr);
if((connfd=accept(sockfd,(struct sockaddr *)&clientaddr,&sin_size))>0);
printf("accept sucessful \n");

bzero(buff,256);
//bzero(buff1,256);
//len=strlen(buff);
//reads msg from socket
n=read(connfd, buff, 256);
printf("msg from client:%s\n",buff);

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


//writes msg on socket
printf("Enter Your Message:");
fgets(buff1, 256,stdin);
len=strlen(buff1);

write(connfd, buff1, len);


//printf("msg to client:%s \n",buff1);
//close call closes socket created and free assigned parameters
close(sockfd);
}

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: a.Say Hello (Client)

#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>
#include <stdlib.h>

void main()
{
int b,sockfd,sin_size,con,n,len;
char buff[256];
char buff1[256];
char ip[50];
char port[10];
struct sockaddr_in servaddr;

// Socket call for socket creation: socket on sucess returns smallest integer value (3) otherwise -1
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>=0)
printf("Socket Created Sucessfully \n");

//Initialize object parameters of server: family,ip,port


servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("10.15.9.218");
servaddr.sin_port=6000;

//initiate a connection on a socket from client to server using connect call


// connect call on sucess return 0 otnerwise -1
sin_size = sizeof(servaddr);
if((con=connect(sockfd,(struct sockaddr *) &servaddr, sin_size))==0)
printf("connect sucessful \n");

bzero(buff,256); //null 256 bytes of buff


bzero(buff1,256); //null 256 bytes of buff1

//fgets call reads msg from user


printf("Enter Your Message:");
fgets(buff, 256,stdin);
len=strlen(buff);

//write call writes message on socket


write(sockfd,buff,len);
//printf("\n msg to server:%s",buff);

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


//read call reads message from socket
read(sockfd, buff1, 256);
printf("msg from server:%s \n",buff1);

//close call closes socket created and free assigned parameters


close(sockfd);
}

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: b. File Transfer (Server)
#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>
#include <stdlib.h>
#define MAXBUFLEN 1000000
void main()
{
int b,sockfd,connfd,sin_size,l,n,len;

if((sockfd=socket(AF_INET,SOCK_STREAM,0))>=0) //socket creation


printf("Socket Created Sucessfully \n"); //on success 0 otherwise -1

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=6007;

if((bind(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr)))==0) //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”.

printf("Bind Sucessful \n");

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");
char buffer[100];
char c[10000] = "this is file transfer program";
//char source[MAXBUFLEN + 1];
//bzero(buffer,10000);

FILE *fp;
fp= fopen("/home/ubuntu/Downloads/CNL/Assg2/Assg2_b/send.txt", "w+");
//fp = fopen("file.txt", "w+");

/* Write data to the file */

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


fwrite(c, 1, strlen(c) + 1, fp);

/* Seek to the beginning of the file */


fseek(fp, 0, SEEK_SET);

/* Read and display data */


fread(buffer, 1,strlen(c)+1, fp);

// fclose(fp);

write(connfd, buffer, strlen(buffer));


printf("Sent File Contents: %s\n", buffer);
fclose(fp);
close(sockfd);
}

Input: receive.txt
this is file transfer program\00

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: b. File Transfter (Client)

#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>
#include <stdlib.h>
#define MAXBUFLEN 1000000
//#define buffsize 150
void main()
{
int b,sockfd,sin_size,con,n,len;
//char buff[256];

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=6007;

sin_size = sizeof(struct sockaddr_in);


if((connect(sockfd,(struct sockaddr *) &servaddr, sin_size))==0) //initiate a connection on a socket

printf("Connect Sucessful \n");

char buffer[10000];
char c[10000];
FILE *fp;
//bzero(buffer,10000);
//bzero(c,10000);
read(sockfd, buffer, 10000);

fp= fopen("/home/ubuntu/Downloads/CNL/Assg2/Assg2_b/receive.txt", "w+");

/* Read and display data */


fwrite(buffer, 1,strlen(buffer) + 1, fp);
//fseek(fp, 0, SEEK_SET);
//fread(c, strlen(buffer)+1, 1, fp);
printf("Received File Contents :%s \n", buffer);
fclose(fp);
close(sockfd);
}
Input: send.txt

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


this is file transfer program\00
Program: c. Calculator (Client)
#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 buff[256];
char operator;
int op1,op2,result;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>0)
printf("socket created sucessfully\n");
//printf("%d\n", sockfd);
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);
}

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output: c. Calculator (Client)

ubuntu@linux:~$ gcc ccalculator.c && ./a.out


socket created sucessfully
connect sucessful
Enter operation:
+:Addition
-: Subtraction
/: Division
*:Multiplication
+
Enter operands:
5
10
Operation result from server=15
ubuntu@linux:~$ gcc ccalculator.c && ./a.out
socket created sucessfully
connect sucessful
Enter operation:
+:Addition
-: Subtraction
/: Division
*:Multiplication
-
Enter operands:
20
10
Operation result from server=10
ubuntu@linux:~$ gcc ccalculator.c && ./a.out
socket created sucessfully
connect sucessful
Enter operation:
+:Addition
-: Subtraction
/: Division
*:Multiplication
*
Enter operands:
10
30
Operation result from server=300
ubuntu@linux:~$ gcc ccalculator.c && ./a.out
socket created sucessfully
connect sucessful
Enter operation:
+:Addition
-: Subtraction
/: Division

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


*:Multiplication
/
Enter operands:
625
25
Operation result from server=25
ubuntu@linux:~$

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program: c. Calculator (Server)

#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
//printf("%d\n", sockfd); //on success 0 otherwise -1

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

//printf("%d\n",b);

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


printf("listen sucessful\n");
//printf("%d\n",l);

sin_size = sizeof(struct sockaddr_in);


if((connfd=accept(sockfd,(struct sockaddr *)&clientaddr,&sin_size))>0);
printf("accept sucessful\n");
//printf("%d\n",connfd);
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);

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


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

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output: c. Calculator (Server)

ubuntu@linux:~$ gcc scalculator.c && ./a.out


socket created sucessfully
bind sucessful
listen sucessful
accept sucessful
Result is: 5 + 10 = 15
ubuntu@linux:~$ gcc scalculator.c && ./a.out
socket created sucessfully
bind sucessful
listen sucessful
accept sucessful
Result is: 20 - 10 = 10
ubuntu@linux:~$ gcc scalculator.c && ./a.out
socket created sucessfully
bind sucessful
listen sucessful
accept sucessful
Result is: 10 * 30 = 300
ubuntu@linux:~$ gcc scalculator.c && ./a.out
socket created sucessfully
bind sucessful
listen sucessful
accept sucessful
Result is: 625 / 25 = 25
ubuntu@linux:~$

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Practical No.6 (Group C)

Write a program for DNS lookup. Given an IP address as input,


Title it should return URL and vice-versa.

Subject Computer Networks and Security

Name

Class Third Year. (Computer Engineering.)

Roll No.

Exam Seat No.

Date

Signature

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Program:

import socket

print 'Welcome to DNS Lookup'


print 'Enter your option 1. URL to IP 2. IP to URL'
op=raw_input('Enter Option')
var=raw_input('Enter URL/IP')
addr1 = socket.gethostbyname(var)
addr6=socket.gethostbyaddr(var)
if op==1:
print(addr1)

else:
print(addr6)

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


Output:

ubuntu@linux:~$ python2 DNS.py


Welcome to DNS Lookup
Enter your option 1. URL to IP 2. IP to URL
Enter Option1
Enter URL/IP127.0.0.0
('localhost', [], ['127.0.0.0'])
ubuntu@linux:~$

JCEI’s Jaihind College of Engineering, Kuran 2021-2022


JCEI’s JAIHIND COLLEGE ENGINEERING, KURAN

DEPARTMENT OF COMPUTER ENGINEERING

CERTIFICATE
This is to certify that the Practical Lab Manual
under the subject

“COMPUTER NETWOKS AND SECURITY”


(310247)
SUBMITTED BY
Mr. /Miss. ________________________________________

Exam Seat No.________________________________________

Is a Bonafede work carried out by student under the supervision of Prof. S. K. Said.
and it is submitted towards the partial fulfilment of the requirement of Third Year of
Computer Engineering.

Prof. S. K. Said. Prof. A. A. Khatri


Subject Teacher Head of Department
Dept. of Computer Engineering. Dept. of Computer Engineering.
Dr. D. J. Garkal

Principal

JCEI’s JAIHIND COLLEGE OF ENGINEERING, KURAN

JCEI’s Jaihind College of Engineering, Kuran 2021-2022

You might also like