Computer Networks Lab KCS 653
Computer Networks Lab KCS 653
Manual
Objective: Implementation of Stop and Wait Protocol and Sliding Window Protocol.
Program: C programs to simulate data transfer operations using the Stop and Wait Protocol and the Sliding
Window Protocol.
Here, sender sends one frame and waits for an acknowledgment before sending the next frame. When the
receiver successfully receives the frame, sends an acknowledgment.
Program Code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define FRAME_COUNT 5
int main() {
sender(i);
return 0;
} else {
return frame;
} else {
Output
The sender send the multiple frames before getting any acknowledgments for the first frames. The window
size is set to 4.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define WINDOW_SIZE 4
#define FRAME_COUNT 10
void sender();
int sender_base = 0;
int next_seq_num = 0;
int main() {
sender();
return 0;
void sender() {
receiver(next_seq_num);
next_seq_num++;
} else {
process_ack(frame);
} else {
acknowledged[ack] = 1;
while (acknowledged[sender_base]) {
sender_base++;
Output
Socket Programming
The process of creating software that can communicate over a network using sockets is known as
socket programming. A socket is used to sending or receiving data across the computer network.
Sockets can be used for communication on the same machine or on different machines.
Client-Server Model
The client-server model is a design pattern used in network applications where the server provides
resources or services, and the client accesses them.
Server: A program that waits for client requests and provides services. It usually runs on a specific
port number.
Client: A program that sends requests to the server and processes the server's responses.
Server.java
import java.net.*;
import java.io.*;
public class ChatServer
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
ServerSocket ss=new ServerSocket(2345);
Socket skt=ss.accept( );
BufferedReader skt_in=new BufferedReader(new
InputStreamReader(skt.getInputStream( )));
PrintStream skt_out=new PrintStream(skt.getOutputStream( ));
while(true)
{
System.out.println(skt_in.readLine( ));
skt_out.println(“What is your name”);
yourname=skt_in.readLine( );
Client.java
import java.net.*;
import java.io.*;
public class ChatClient
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Socket skt=new Socket(“rungtaibm”,2345);
BufferedReader skt_in=new BufferedReader(new
InputStreamReader(skt.getInputStream( )));
PrintStream skt_out=new PrintStream(skt.getOutputStream( ));
while(true)
{
skt_out.println(“hello can I connect”);
skt_out.println(skt_in.readLine( ));
String myname=br.readLine( );
skt_out.println(myname);
skt_out.println(“What is yours”);
String yourname=skt_in.readLine( );
System.out.println(yourname);
break;
}
while(true)
{
String send=br.readLine( );
skt_out.println(send);
Output
ARP Simulation (Address Resolution Protocol):- ARP is used to resolve IP addresses to MAC addresses on a
local network.
Program Code
#include <stdio.h>
#include <string.h>
struct ARPEntry {
};
{"192.168.1.1", "00:11:22:33:44:55"},
{"192.168.1.2", "11:22:33:44:55:66"},
{"192.168.1.3", "22:33:44:55:66:77"},
};
int i;
int found = 0;
if (strcmp(arp_table[i].ip_address, ip_address) == 0) {
found = 1;
break;
if (!found) {
int main() {
arp_resolve("192.168.1.1");
arp_resolve("192.168.1.2");
return 0;
Output
Program Code
#include <stdio.h>
#include <string.h>
struct RARPEntry {
};
// RARP table
{"00:11:22:33:44:55", "192.168.1.1"},
{"11:22:33:44:55:66", "192.168.1.2"},
{"22:33:44:55:66:77", "192.168.1.3"},
};
int i;
int found = 0;
if (strcmp(rarp_table[i].mac_address, mac_address) == 0) {
found = 1;
if (!found) {
int main() {
// RARP requests
rarp_resolve("00:11:22:33:44:55");
rarp_resolve("11:22:33:44:55:66");
return 0;
Output
import java.io.*;
import java.net.*;
class pinger
try
String str;
InputStreamReader(System.in));
String ip=br1.readLine();
Runtime H=Runtime.getRuntime();
InputStream ins=p.getInputStream();
InputStreamReader(ins));
while((str=br2.readLine())!=null)
System.out.println(e.getMessage());
Output
import java.io.BufferedReader;
import java.io.InputStreamReader;
try
Process p = Runtime.getRuntime().exec(command);
String st = "";
System.out.println(st);
catch (Exception e)
String ip = "www.google.co.in";
Output
Objective: Create a socket for HTTP for web page upload and download
//HttpClient.java
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;
soc=new Socket("localhost",4000);
try {
baos.flush();
baos.close();
dos.writeInt(bytes.length);
dos.write(bytes, 0, bytes.length);
out.close();
catch (Exception e)
soc.close();
soc.close();
HttpServer.java
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
class HttpServer {
ServerSocket server=null;
Socket socket;
server=new ServerSocket(4000);
InputStream in = socket.getInputStream();
dis.readFully(data);
in.close();
l.setIcon(icon);
f.add(l);
f.pack();
f.setVisible(true);
Output
Advantages of RPC
Transparency: Makes remote procedure calls look like local ones, simplifying distributed application
development.
Abstraction: Abstracts the communication layer, allowing developers to focus on application logic.
//serverPC.java
import java.io.*;
import java.net.*;
import java.util.*;
class serverPC
try
while(true)
Socket obj2=obj1.accept();
String str=dins.readLine();
Process p=Runtime.getRuntime().exec(str);
System.out.println(e);
clientPC.java
import java.io.*;
import java.net.*;
import java.util.*;
class clientPC
try
String st = brin.readLine();
dout.writeBytes(st+'\n');
soc.close();
System.out.println(e1);
//Subnetting.java
import java.util.Scanner;
scanner.close();
return String.format("%d.%d.%d.%d",(mask >>> 24) & 0xff,(mask >>> 16) & 0xff,(mask >>> 8) & 0xff,mask &
0xff);
startParts[i] = Integer.parseInt(networkParts[i]);
endParts[i] = Integer.parseInt(broadcastParts[i]);
Output
Objective: Applications using TCP Sockets like (Echo client and echo server)
Program: Write a java program for echo client and echo server using TCP sockets.
// EchoServer.java
import java.io.*;
import java.net.*;
while (true) {
String inputLine;
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
// EchoClient.java
import java.io.*;
import java.net.*;
String userInput;
out.println(userInput);
if ("bye".equalsIgnoreCase(userInput)) {
break;
} catch (UnknownHostException e) {
System.exit(1);
} catch (IOException e) {
System.exit(1);
Output
ChatServer.java
import java.io.*;
import java.net.*;
String message;
try {
} catch (IOException e) {
System.out.println("Connection closed.");
}).start();
String message;
out.println(message);
} catch (IOException e) {
e.printStackTrace();
//ChatClient.java
import java.io.*;
import java.net.*;
String message;
try {
} catch (IOException e) {
System.out.println("Connection closed.");
}).start();
String message;
out.println(message);
} catch (IOException e) {
e.printStackTrace();
Output
//FileServer.java
import java.io.*;
import java.net.*;
System.out.println("Client connected...");
fileInputStream.read(fileBytes);
out.writeLong(fileBytes.length);
out.flush();
} catch (IOException e) {
e.printStackTrace();
//FileClient.java
import java.io.*;
import java.net.*;
in.readFully(fileBytes);
fileOutputStream.write(fileBytes);
} catch (IOException e) {
e.printStackTrace();
Output
//DNSServer.java
import java.io.*;
import java.net.*;
import java.util.*;
while (true) {
System.out.println("Client connected...");
out.println(ipAddress);
} catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
try {
return inetAddress.getHostAddress();
} catch (UnknownHostException e) {
//DNSClient.java
import java.io.*;
import java.net.*;
public class DNSClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int PORT = 1248;
Output
Objective: Applications using TCP and UDP Sockets (like file transfer)
Program: Write a java program for file transfer using UDP sockets.
//UDPFileServer.java
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
try {
while (receiving) {
socket.receive(packet);
fileOutputStream.write(packet.getData(), 0, packet.getLength());
receiving = false;
System.out.println("File received.");
fileOutputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
//UDPFileClient.java
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
try {
int bytesRead;
socket.send(packet);
System.out.println("File sent.");
fileInputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
Output
Assignment: Explain Network simulator (NS) and Simulation of Congestion Control Algorithms using NS.
Testing Protocols: Try out different rules for how data moves through the network, like TCP and UDP.
Making Traffic: Create fake data to see how it affects the network.
Performance Checks: Measure things like how fast data moves, how long it takes, and if any data gets lost.
Customizable: You can add your own ideas and test new network protocols.
Congestion control helps prevent networks from getting too crowded, which can slow down data. Using NS
to test these control methods helps researchers find the best ways to manage network traffic.
Add Congestion Control Methods: Put in different methods to control network traffic, like TCP Reno or TCP
Vegas.
Create Network Traffic: Make pretend data flows to see how the network handles different amounts of
traffic.
Run the Simulation: Start the virtual network and watch how it performs with the traffic you created.
Gather Results: Collect data on how well the network did, such as speed and data loss.
Compare Methods: Look at the results to see which congestion control method worked best.
Link State Routing is a method used in computer networks to figure out the best paths for data to travel.
Unlike another method called distance-vector routing, where routers share their whole list of paths with
their neighbours, Link State Routing has each router keeping a full map of the network. This helps routers
make better decisions about where to send data. One of the most well-known protocols that use Link State
Routing is called Open Shortest Path First, or OSPF.
Network Topology Awareness: Each router has a full view of the network topology.
Shortest Path First (SPF) Algorithm: Uses algorithms like Dijkstra’s to compute the shortest path.
Fast Convergence: Quickly adapt network changes and recalculates optimal paths.
2. Flooding
Flooding is a simple routing algorithm where every incoming packet is sent out on every outgoing line,
except the one it arrived on. This ensures that packets reach every node in the network, but can lead to
excessive redundancy and bandwidth usage.
Economy: Highly inefficient due to excessive redundancy. Every node may receive multiple copies of the
same packet, leading to high bandwidth consumption and potential network congestion.
Distance Vector Routing is a routing protocol used in computer networks to determine the best path for
data packets to travel from a source to a destination. It is based on the Bellman-Ford algorithm and is one
of the simplest types of routing protocols.
Each router maintains a table (or vector) that holds the distance (or cost) to every other router in the
network. This table is updated periodically based on information received from neighbouring routers.
Bellman-Ford Algorithm calculates the shortest path from a single source node to all other nodes in a
weighted graph. It helps to determine the shortest path to each destination.
1. Initialization: Each router initializes its routing table with the direct distances between its immediate
neighbours. For all other destinations, the distance is set to infinity (unknown).
2. Periodic Updates: Routers periodically exchange routing tables with their immediate neighbours. Each
router updates own routing table based on the received information. If a router learns of a shorter path to
a destination through a neighbour, it updates its table with the new distance and next hop.
3. Bellman-Ford Update Rule: The routing table is updated using the Bellman-Ford equation:
4. Convergence: Over time, the routing tables across the network converge, meaning they become stable
and reflect the shortest paths to all destinations.
Performance Results
Objective: To learn handling and configuration of networking hardware like RJ-45 connector, CAT-
6 cable, crimping tool, etc.
Learning how to use and install networking hardware including RJ-45 connectors, CAT-6 cables, and
crimping tools. These devices are necessary for establishing and maintaining network infrastructure. Here's
an easy guide to learn:
1. Measure and Cut the Cable: Determine the length of cable you need and cut it to size using a cable
cutter.
2. Strip the Outer Jacket: Use the cable stripper to remove about 1-2 inches of the outer jacket from both
ends of the cable, exposing the twisted pairs inside.Be careful not to nick the wires inside while stripping
the jacket.
3. Untwist and Arrange the Wires: Untwist the pairs of wires and arrange them according to the T568B
wiring standard (most commonly used):
1) White/Orange
2) Orange
3) White/Green
4) Blue
5) White/Blue
6) Green
7) White/Brown
8) Brown
4. Cut the Wires Evenly: Ensure all the wires are flat and in the correct order, then cut them evenly to
about 0.5 inches in length from the jacket.
5. Insert Wires into the RJ-45 Connector: Holding the RJ-45 connector with the clip facing down, carefully
insert the wires into the connector, ensuring each wire goes into its correct slot. The wires should reach the
end of the connector.
7. Test the Cable: Use a cable tester to verify that each wire is correctly connected and there are no shorts
or miss wires. Insert both ends of the cable into the tester and check the results.
Objective: Running and using services/commands like ping, traceroute, nslookup, arp,
telnet, ftp, etc.
Using various network commands you can diagnose and troubleshoot the network issues. Here are some
common networking commands with explanation:
1. Ping
Ping is used to test the reachability of a host on an IP network and to measure the round-trip time from the
originating host to the destination computer.
Syntax:
Example:
ping google.com
This command sends ICMP Echo Request messages to google.com and displays the time it takes for each
message to return.
2. Traceroute
Traceroute tracks the path packets take from one network host to another, showing each hop along the
way.
Syntax:
Example:
traceroute google.com
This command will list all the routers the packets pass through to reach google.com.
3. Nslookup
Nslookup queries the Domain Name System (DNS) to obtain domain name or IP address mapping
information.
nslookup <hostname>
Example:
nslookup google.com
4. ARP
ARP (Address Resolution Protocol) is used to map IP addresses to MAC addresses, allowing devices to find
each other on a local network.
Syntax:
arp -a
Example:
arp –a
This command will display the ARP table, showing the IP addresses and their corresponding MAC addresses.
5. Telnet
Telnet is a protocol used to remotely access another computer over a network. It is often used for
managing network devices.
Syntax:
Example:
telnet 192.168.1.1 23
This command will attempt to connect to the device at 192.168.1.1 on port 23.
6. FTP
FTP (File Transfer Protocol) is used to transfer files between computers on a network.
Example:
ftp ftp.example.com
This command connects to an FTP server at ftp.example.com. Once connected, you can use commands like
put to upload files and get to download files.
7. SSH
SSH (Secure Shell) is used to securely access a remote device over a network.
Syntax:
Example:
ssh user@192.168.1.1
This command will connect to the device at 192.168.1.1 using the username user.