You are on page 1of 26

Practical File

Of

COMPUTER NETWORKS LAB

(KCS-653)

(2022-2023)

Submitted to: Submitted by:


Vaibhav Sharma Name: Ravi Kumar Sharma
Assistant Professor, Roll No.: 2001500100081
Department of CSE Year/Branch: 3rd Year/CSE
INDEX
S. No. Date Contents Page no.

1. To learn handling and configuration


of networking hardware like RJ-45
connector, CAT-6 cable and
crimping tool etc.

2.
Configuration of the router, hub,
switch etc. (using real devices or
simulators)

3. To implement the stop-and-wait


protocol using Java programming
language.

4. Study of Socket Programming and


Client-Server Model

5. To write a Java program for the


socket for HTTP for web page
upload and download.
Experiment-1
Objective: To learn handling and configuration of networking hardware like RJ-45 connector, CAT-6
cable and crimping tool etc.

Apparatus (Components): RJ-45 connector, Crimping Tool, Twisted pair Cable.

Brief Theory:
RJ 45- Registered Jack 45 (RJ45) is a standard type of physical connector for network cables. RJ45
connectors are most commonly seen with Ethernet cables and networks. Modern Ethernet cables feature
small plastic plugs on each end that are inserted into the RJ45 jacks of Ethernet devices.
CAT-6 Cable- Category 6 Cable (Cat 6), is a standardized twisted pair cable for Ethernet and other
network physical layers.
Crimping Tool- A crimping tool is a device used to conjoin two pieces of metal by deforming one or
both of them in a way that causes them to hold each other.

Procedure:
To do these practical following steps should be done:
Step 1- Start by stripping off about 2 inches of the plastic jacket off the end of the cable. Be very careful
at this point, as to not nick or cut into the wires, which are inside. Doing so could alter the characteristics
of your cable or even worse render it useless. Check the wires, one more time for nicks or cuts. If there
are any, just whack the whole end off, and start over.
Step 2- Spread the wires apart, but be sure to hold onto the base of the jacket with your other hand. You
do not want the wires to become untwisted down inside the jacket. Category 5 cable must only have 1/2
of an inch of 'untwisted' wire at the end; otherwise, it will be 'out of spec'. At this point, you obviously
have ALOT more than 1/2 of an inch of un-twisted wire.
Step 3- You have 2 end jacks, which must be installed on your cable. If you are using a pre-made cable,
with one of the ends whacked off, you only have one end to install - the crossed-over end. Below are two
diagrams, which show how you need to arrange the cables for each type of cable end. Decide at this point
which end you are making and examine the associated picture below.

Diagrams for preparing Cross wired connection

Figure1.1: Crossover connection


Figure 1.2: Color coding sequence for RJ45 connectors at both ends in cross-wired connection

Figure1.3: Straight through wired connection


Diagrams for preparing Cross wired connection

Figure 1.4: Color coding sequence for RJ45 connectors at both ends in straight-through wired
connection

Result- Configuration of networking hardware like RJ-45 connector, CAT-6 cable, crimping
tool, etc. is learned.
Experiment-2
Objective: Configuration of the router, hub, switch etc. (using real devices or simulators)

Apparatus: Cisco Packet Tracer

Procedure:

Configuration of Hub using Star Topology

Step 1- Open Cisco packet Tracer Software and choose Generic Hub on the workspace.

Figure 2.1: Generic Hub


Step 2- Now choose the end device as Generic. Connect end devices with Hub (Choose automatic
connection type).

Figure 2.2: End devices connected with Hub


Step 3- Now click on each end device and enter an IP address such as 10.0.0.1 and label the device
with the corresponding IP address using the text tool available in the Cisco packet tracer.

Figure 2.3: IP configuration of devices


Step 4- Now select Simple Message(PDU) from the right side of the window and click the other

sender node and the receiver node.

Figure 2.4: Selection of PDU


Step 5- Now click on simulation and click on AutoPlay to see the effect.

Figure 2.4: Simulation results

Configuration of Switch using Star Topology

Step 1- Open Cisco packet Tracer Software and choose Generic Switch on the workspace.

Figure 2.5:Generic Switch


Step 2- Now choose the end device as Generic. Connect end devices with switch (Choose
automatic connection type.

Figure 2.6: Connection of end devices with Switch

Figure 2.7: IP configuration of end devices


Figure 2.8: Topology diagram
Step 5- Now click on simulation and click on AutoPlay to see the effect.

Configuration of Router

Figure 2.9: Router as connecting device

Modes of Router

1. User mode: Router>


2. Privilege Mode: Router#
3. Global Configuration Mode: Router (config)#
4. Fast Ethernet Interface Mode: Router (Config-if)#

Basic Router Commands

1. User Mode to Privilege Mode: Router > enable

2. Privilege Mode to Global Configuration Mode: Router# config terminal

3. Global Configuration Mode to Fast Ethernet Interface Mode: Router (config)# interface
FastEthernet 0/0

4. Active and Synchronize IP with interface: Router(config-if) no shutdown

5. Exit from Mode : Router(Config-if)#Exit

Figure 2.10:Topology diagram for Router configuration


Step 1: Open Cisco Packet Tracer software and then click on Router and then over Generic, then
drag it over the workspace.

Step 2: Design topology as shown in the diagram above.

Step 3: Now click on each end device and enter an IP address such as 10.0.0.1 and label the device
with the corresponding IP address using the text tool available in the cisco packet tracer. Ensure
that the router should connect two different networks.
Figure 2.11:IP configuration in end devices
Step 4: Double click router and type router commands to configure the router at interface fa0/0
and fa0/1

Router>enable
Router#configure terminal
Router(config)#hostname R1
Router(config)#interface fastethernet 0/0
Router(config-if )# 10.0.0.1 255.0.0.0
Router(config-if )# no shut down

Figure 2.12: configuration in CLI mode


Step 5- Now select Simple Message from the right side of the window and click over the sender
node and on the receiver node.

Step 6- Now click on simulation and click on AutoPlay to see the effect.

Result:
Configuration of the router, hub, and switch is performed.
Experiment-3
Objective: To implement the stop-and-wait protocol using Java programming language.

Procedure:
Sender
Step 1: sequence 0
Step 2: Accept new packet and assign a sequence to it.
Step 3: Send packet sequence with sequence number sequence.
Step 4: Set timer for recently sent packets.
Step 5: If error-free acknowledgment from the receiver and NextFrameExpected -> sequence then
sequence NextFrameExpected.
Step 6: If time out then go to step3.
Step 7: Stop.
Receiver
Step 1: Start.
Step 2: NextFrameExpected 0, repeat steps 3 forever.
Step 3: If error-free frame received and sequence= NextFrameExpected, then pass packet to higher
layer and NextFrameExpected NextFrameExpected+1(modulo 2).
Step 4: Stop.

Code:
Sender.java
import java.io.*;
import java.net.*;
public class Sender{
Socket sender;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,str, msg;
int n,i=0,sequence=0;
Sender(){}
public void run(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Waiting for Connection....");
sender = new Socket("localhost",2005);
sequence=0;
out=new ObjectOutputStream(sender.getOutputStream());
out.flush();
in=new ObjectInputStream(sender.getInputStream());
str=(String)in.readObject();
System.out.println("reciver > "+str);
System.out.println("Enter the data to send....");
packet=br.readLine();
n=packet.length();
do{
try{

if(i<n){
msg=String.valueOf(sequence);
msg=msg.concat(packet.substring(i,i+1));
}else if(i==n){
msg="end";out.writeObject(msg);break;
}out.writeObject(msg);
sequence=(sequence==0)?1:0;
out.flush();
System.out.println("data sent>"+msg);
ack=(String)in.readObject();
System.out.println("waiting for ack.....\n\n");
if(ack.equals(String.valueOf(sequence))){
i++;
System.out.println("receiver > "+" packet recieved\n\n");
}else{
System.out.println("Time out resending data....\n\n");
sequence=(sequence==0)?1:0;
}}catch(Exception e){}
}while(i<n+1);
System.out.println("All data sent. exiting.");
}catch(Exception e){}
finally{
try{
in.close();
out.close();
sender.close();
}
catch(Exception e){}
}}
public static void main(String args[]){
Sender s=new Sender();
s.run();
}}

Receiver.java
import java.io.*;
import java.net.*;
public class Receiver{
ServerSocket reciever;
Socket connection=null;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,data="";
int i=0,sequence=0;
Receiver(){}
public void run(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
reciever = new ServerSocket(2005,10);

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


connection=reciever.accept();
sequence=0;
System.out.println("Connection established :");
out=new ObjectOutputStream(connection.getOutputStream());
out.flush();
in=new ObjectInputStream(connection.getInputStream());
out.writeObject("connected .");

do{
try{
packet=(String)in.readObject();
if(Integer.valueOf(packet.substring(0,1))==sequence){
data+=packet.substring(1);
sequence=(sequence==0)?1:0;
System.out.println("\n\nreceiver >"+packet);
}
else
{
System.out.println("\n\nreceiver >"+packet +" duplicate data");
}if(i<3){
out.writeObject(String.valueOf(sequence));i++;
}else{
out.writeObject(String.valueOf((sequence+1)%2));
i=0;
}}
catch(Exception e){}
}while(!packet.equals("end"));
System.out.println("Data recived="+data);
out.writeObject("connection ended .");
}catch(Exception e){}
finally{
try{in.close();
out.close();
reciever.close();
}
catch(Exception e){}
}}

public static void main(String args[]){


Receiver s=new Receiver();
while(true){
s.run();
}
}
}

Output:

client class

server class

Sliding Window Protocol in Java


Sliding window protocol can be implemented in Java using two different classes in the same packages
using a unique id.

Sliding window protocols are used to deliver in-order packets in Transmission Control Protocol (TCP)
and Data link layer and it is also a feature of packet-based data transmission protocols.

Detail:

The socket is an endpoint of the two-way communication link between two programs running on the
same network and the programs communicate with each other using the port running port numbers on
the network. Here, the client and server are the two programs using the socket for transferring data.

Advantage:
• Sliding window protocol is efficient than the Stop and Wait protocol.
• Sliding window protocol sends multiple frames and retransmits damaged frames.

Program:

SERVER:
import java.io.*;
import java.net.*;
public class server {

public static void main(String args[])


{

try
{
ServerSocket ss1;
ss1 = new ServerSocket(9000);
Socket s =ss1.accept();
DataInputStream dd= new DataInputStream(s.getInputStream());
String sss1 = dd.readUTF();
System.out.println(sss1);
}
catch(Exception ex)
{
System.out.println(ex);
}
}

}
--------------------------------
CLIENT:
import java.io.*;
import java.net.*;
public class client {
public static void main(String args[])
{
String h="localhost";
int p=9000;
Try

{
Socket s1;
s1= new Socket(h,p);
DataOutputStream d = new DataOutputStream(s1.getOutputStream());
d.writeUTF("Start server :");
}

catch(Exception ex)
{
System.out.println(ex);
}
}
}

Output:

Server class

Client class
Experiment-4

Objective: Study of Socket Programming and Client-Server model

Brief Theory:
Java programming is used for communication between the applications running on different JRE.

Java Socket programming can be connection-oriented or connection-less.

Socket and ServerSocket classes are used for connection-oriented socket programming and
DatagramSocket and DatagramPacket classes are used for connection-less socket programming.

The client in socket programming must know two pieces of information:

1. IP Address of Server, and


2. Port number.

Here, we are going to make one-way client and server communication. In this application, the client
sends a message to the server, the server reads the message and prints it. Here, two classes are being
used: Socket and ServerSocket. The Socket class is used to communicate client and server. Through
this class, we can read and write messages. The ServerSocket class is used on the server side. The
accept() method of ServerSocket class blocks the console until the client is connected. After the
successful connection of the client, it returns the instance of Socket at the server side.
Socket class

A socket is simply an endpoint for communications between the machines. The Socket class can be
used to create a socket.

Important methods:
Method Description

1) public InputStream getInputStream() returns the InputStream attached with this socket.

2) public OutputStream getOutputStream() returns the OutputStream attached with this socket.

3) public synchronized void close() closes this socket

ServerSocket class

The ServerSocket class can be used to create a server socket. This object is used to establish
communication with the clients.

Important methods:
Method Description

1) public Socket accept() returns the socket and establish a connection between server and
client.

2) public synchronized void close() closes the server socket.

Example of Java Socket Programming

Creating Server:

To create the server application, we need to create the instance of ServerSocket class. Here, we are
using the 6666 port number for communication between the client and server. You may also choose
any other port number. The accept() method waits for the client. If clients connect with the given port
number, it returns an instance of Socket.

ServerSocket ss=new ServerSocket(6666);


Socket s=ss.accept();//establishes the connection and waits for the client
Creating Client:

To create the client application, we need to create the instance of Socket class. Here, we need to pass
the IP address or hostname of the Server and a port number. Here, we are using "localhost" because
our server is running on same system.

Socket s=new Socket("localhost",6666);

Let's see a simple Java socket programming where the client sends a text and the server receives and
prints it.

File: MyServer.java

import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}

File: MyClient.java

import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
Experiment-5

Objective:
To write a Java program for the socket for HTTP for web page upload and download.

Algorithm:
1. Start the program.
2. Get the frame size from the user

3. To create the frame based on the user’s request.


4. To send frames to the server from the client side.

5. If your frames reach the server it will send an ACK signal to the client otherwise it will send
NACK signal to the client.

6. Stop the program.

Program:

Client code

import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File;

import java.io.IOException; import javax.imageio.ImageIO;

public class Client{

public static void main(String args[]) throws Exception{ Socket soc;

BufferedImage img = null;


soc=new Socket("localhost",4000);

System.out.println("Client is running. ");


try {

System.out.println("Reading image from disk. ");

img = ImageIO.read(new File("digital_image_processing.jpg")); ByteArrayOutputStream baos =


new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
baos.flush();

byte[] bytes = baos.toByteArray(); baos.close();

OutputStream out = soc.getOutputStream();


DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(bytes.length);

dos.write(bytes, 0, bytes.length);
System.out.println("Image sent to server. ");
dos.close();
out.close();

}catch (Exception e) { System.out.println("Exception: " + e.getMessage());


soc.close();
}

soc.close();
}
}

Server code

import java.net.*;
import java.io.*;

import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;

class Server {

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


ServerSocket server=null;

Socket socket;

server=new ServerSocket(4000);
System.out.println("Server Waiting for image");
socket=server.accept(); System.out.println("Client connected.");
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt();

System.out.println("Image Size: " + len/1024 + "KB"); byte[] data = new byte[len];


dis.readFully(data);
dis.close();
in.close();
InputStream ian = new ByteArrayInputStream(data);
BufferedImage bImage = ImageIO.read(ian);
JFrame f = new JFrame("Server");
ImageIcon icon = new ImageIcon(bImage);
JLabel l = new JLabel();
l.setIcon(icon);
f.add(l);

f.pack();
f.setVisible(true);
}
}
Output: Client-Side Output

When you execute the client code, the following output appears on the client side −

Server-Side Output

When you execute the server code, the following output appears on server side −

After receiving the image, the server displays the image as shown below −

You might also like