0% found this document useful (0 votes)
47 views26 pages

Compter Network

Uploaded by

Saurabh Tiwary
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)
47 views26 pages

Compter Network

Uploaded by

Saurabh Tiwary
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/ 26

Practical File

Of

COMPUTER NETWORK LAB

(KCS-653)

(2022-23)

Submitted to: Submitted by:


Vaibhav Sharma Name: Shivam Madheshiya
Roll no: 2001500100105
B.Tech CSE 3RD YEAR
TABLE OF CONTENT

S.NO Name of experiment Date of Submission Page Sign.


exp. no.
1. To learn handling and
configuration of
networking hardware like
RJ-45 connector, CAT-6
cable, crimping tool, etc.
2. Configuration of router,
hub, switch etc. (using
real device or simulators)

3. Implementing stop &


wait protocol.

4.
Study of Socket
Programming and Client-
Server model

Create a socket for


5. HTTP for web page
upload and download.
Q1. To learn handling and configuration of networking hardware like
RJ-45 connector, CAT-6 cable, crimping tool, etc.
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 Ethernetdevices.
CAT-6 Cable-Category 6 Cable (Cat 6), is a standardized twisted pair cable for Ethernet and other
network physical layers.
Crimping Tool-Crimping Tool is a device used to conjoin two pieces of metal by deforming one or
both of them in a way that causes themto 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 is 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

Diagram for preparing straight through wired connection


Figure1.3: Straight through 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.
Question 2 : Configuration of router, hub, switch etc. (using real device 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 workspace.

Figure 2.1: Generic Hub


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

Figure 1: End devices connected with Hub


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

Figure 2.2: IP configuration of devices

Step 4:- Now select Simple Message(PDU) from right side of window and click over sender

node and on receiver node.

Figure 2.3: Selection of PDU


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

Figure 2.4: Simulation results

Configuration of Switch using Star Topology

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

Figure 2.5:Generic Switch


Step 2 :- Now choose 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 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 workspace.

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

Step 3: Now click on each end device and enter IP address such as 10.0.0.1 and label the device
with corresponding IP address using text tool available in cisco packet tracer. Be ensure that 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 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 right side of window and click over sender node
and on receiver node.

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

Result: Configuration of router, hub, switch is performed .


Question 3. IMPLEMENTING STOP & WAIT PROTOCOL

AIM
To implement the stop and wait protocol using java programming language.

PROCEDURE
Sender
Step1: sequence 0
Step2: Accept new packet and assign sequence to it.
Step3: Send packet sequence with sequence number sequence.
Step4: Set timer for recently sent packets.
Step5: If error free acknowledgment from receiver and NextFrameExpected -> sequence then
sequence NextFrameExpected.
Step6: If time out then go to step3.
Step7: Stop.
Receiver
Step1: Start.
Step2: NextFrameExpected 0, repeat steps 3 forever.
Step3: If error-free frame received and sequence= NextFrameExpected, then pass packet to higher layer and
NextFrameExpected NextFrameExpected+1(modulo 2).
Step4: 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 Coding:

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
Question 4. Study of Socket Programming and Client-Server model
Socket Programming

Java Socket 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 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, client sends a message to
the server, 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 message. The
ServerSocket class is used at server-side. The accept() method of ServerSocket class blocks the console until the client
is connected. After the successful connection of client, it returns the instance of Socket at 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 6666 port
number for the communication between the client and server. You may also choose any other port number. The
accept() method waits for the client. If clients connects with the given port number, it returns an instance of Socket.

1. ServerSocket ss=new ServerSocket(6666);


2. Socket s=ss.accept();//establishes 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.
1. Socket s=new Socket("localhost",6666);

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

File: MyServer.java

1. import java.io.*;
2. import java.net.*;
3. public class MyServer {
4. public static void main(String[] args){
5. try{
6. ServerSocket ss=new ServerSocket(6666);
7. Socket s=ss.accept();//establishes connection
8. DataInputStream dis=new DataInputStream(s.getInputStream());
9. String str=(String)dis.readUTF();
10. System.out.println("message= "+str);
11. ss.close();
12. }catch(Exception e){System.out.println(e);}
13. }
14. }

File: MyClient.java

1. import java.io.*;
2. import java.net.*;
3. public class MyClient {
4. public static void main(String[] args) {
5. try{
6. Socket s=new Socket("localhost",6666);
7. DataOutputStream dout=new DataOutputStream(s.getOutputStream());
8. dout.writeUTF("Hello Server");
9. dout.flush();
10. dout.close();
11. s.close();
12. }catch(Exception e){System.out.println(e);}
13. }
14. }
Question 5. Create a socket for HTTP for web page upload and download.

Aim:
To write a java program for 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 request.


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

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

6.Stop the program

Program :

Client

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

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

When you run the client code, following output screen would appear on client side.

You might also like