Comnet
Comnet
Comnet
SCIENCE
BONAFIDE CERTIFICATE
Register No.
This is to certify that this is a Bonafide record of the work done by the above student with
Date:
PAGE MARK
EX.NO DATE NAME OF THE EXPERIMENT SIGN
NO S
1 Learn to use commands like tcpdump,
netstat, ifconfig, nslookup and traceroute.
Capture ping and trace route PDUs using a
network protocol analyzer and examine
2 Write a HTTP web client program to
download a web page using TCP sockets.
3 Applications using TCP sockets like:
a) Echo client and echo server
b) Chat
4 Simulation of DNS using UDP sockets.
5 Use a tool like Wireshark to capture packets
and examine the packets
6 Write a code simulating ARP /RARP
protocols.
7 Study of Network simulator (NS) and
Simulation of Congestion Control
Algorithms using NS.
8 Study of TCP/UDP performance using
Simulation tool.
9 Simulation of Distance Vector/ Link State
Routing algorithm.
10 Simulation of an error correction code (like
CRC)
EXP NO: 1
Learn to use commands like tcpdump, netstat, ifconfig,
DATE
nslookup and traceroute ping.
AIM:
To Learn to use commands like tcpdump, netstat, ifconfig, nslookup and traceroute ping.
Tcpdump:
The tcpdump utility allows you to capture packets that flow within your network to assist in
network troubleshooting. The following are several examples of using tcpdump with different
options. Traffic is captured based on a specified filter.
Netstat
Netstat is a common command line TCP/IP networking available in most versions of
Windows, Linux, UNIX and other operating systems.
Netstat provides information and statistics about protocols in use and current TCP/IP network
connections.
ipconfig
ipconfig is a console application designed to run from the Windows command prompt.
This utility allows you to get the IP address information of a Windows computer.
From the command prompt, type ipconfig to run the utility with default options. The output of the
default command contains the IP address, network mask, and gateway for all physical and virtual
network adapter.
nslookup
The nslookup (which stands for name server lookup) command is a network utility program used
to obtain information about internet servers. It finds name server information for domains by
querying the Domain Name System.
Trace route:
Traceroute is a network diagnostic tool used to track the pathway taken by a packet on an IP
network from source to destination. Traceroute also records the time taken for each hop the packet
makes during its route to the destination
Commands:
Tcpdump:
Display traffic between 2 hosts:
To display all traffic between two hosts (represented by variables host1 and host2): # tcpdump
host host1 and host2
Display traffic from a source or destination host only:
To display traffic from only a source (src) or destination (dst)
host: # tcpdump src host
# tcpdump dst host
Display traffic for a specific protocol
Provide the protocol as an argument to display only traffic for a specific protocol, for example tcp,
udp, icmp, arp
# tcpdump protocol
For example to display traffic only for the tcp traffic :
# tcpdump tcp
Filtering based on source or destination port
To filter based on a source or destination port:
# tcpdump src port ftp
# tcpdump dst port
http
2.Netstat
Netstat is a common command line TCP/IP networking available in most versions of
Windows, Linux, UNIX and other operating systems.
Netstat provides information and statistics about protocols in use and current TCP/IP
network connections. The Windows help screen (analogous to a Linux or UNIX for netstat reads as
follows: displays protocol statistics and current TCP/IP network connections.
#netstat
3. ipconfig
In Windows, ipconfig is a console application designed to run from the Windows command
prompt. This utility allows you to get the IP address information of a Windows computer.
Using ipconfig
From the command prompt, type ipconfig to run the utility with default options. The output of the
default command contains the IP address, network mask, and gateway for all physical and virtual
network adapter.
#ipconfig
4.nslookup
The nslookup (which stands for name server lookup) command is a network utility program used
to obtain information about internet servers. It finds name server information for domains by
querying the Domain Name System.
The nslookup command is a powerful tool for diagnosing DNS problems. You know you're
experiencing a DNS problem when you can access a resource by specifying its IP address but not its
DNS name.
#nslookup
5.Trace route:
Traceroute uses Internet Control Message Protocol (ICMP) echo packets with variable time to live
(TTL) values. The response time of each hop is calculated. To guarantee accuracy, each hop is queried
multiple times (usually three times) to better measure the response of that particular hop.
Traceroute is a network diagnostic tool used to track the pathway taken by a packet on an IP network
from source to destination. Traceroute also records the time taken for each hop the packet makes
during its route to the destination. Traceroute uses Internet Control Message Protocol (ICMP) echo
packets with variable time to live (TTL) values.
The response time of each hop is calculated. To guarantee accuracy, each hop is queried
multiple times (usually three times) to better measure the response of that particular hop. Traceroute
sends packets with TTL values that gradually increase from packet to packet, starting with TTL value
of one. Routers decrement TTL values of packets by one when routing and discard packets whose
TTL value has reached zero, returning the ICMP error message ICMP Time Exceeded.
For the first set of packets, the first router receives the packet, decrements the TTL value and
drops the packet because it then has TTL value zero. The router sends an ICMP Time Exceeded
message back to the source. The next set of packets are given a TTL value of two, so the first router
forwards the packets, but the second router drops them and replies with ICMP Time Exceeded.
Proceeding in this way, traceroute uses the returned ICMP Time Exceeded messages to build a list of
routers that packets traverse, until the destination is reached and returns an ICMP Echo Reply
message.
With the tracert command shown above, we're asking tracert to show us the path from the local
computer all the way to the network device with the hostname
www.google.com.
#tracert
google.com
6. Ping:
The ping command sends an echo request to a host available on the network. Using this
command, you can check if your remote host is responding well or not. Tracking and isolating
hardware and software problems. Determining the status of the network and various foreign hosts.
The ping command is usually used as a simple way to verify that a computer can communicate over
the network with another computer or network device. The ping command operates by sending
Internet Control Message Protocol (ICMP) Echo Request messages to the destination computer and
waiting for a response
# ping172.16.6.2
RESULT:
Thus the various networks commands like tcpdump, netstat, ifconfig, nslookup and traceroute
ping are executed successfully.
EXP NO: 2
DATE
Write a HTTP web client program to download a
web page using TCP sockets.
AIM:
To write a HTTP web client program to download a web page using TCP sockets.
PROCEDURE:
4. HTTP GET Request: The request is sent with the required headers (Host and Connection).
5. Response Handling: The response is read line by line and printed to the console.
6. Cleanup: The input and output streams and the socket are closed
PROGRAM:
import java.io.*;
import java.net.Socket;
public class HttpClient
{
public static void main(String[] args)
{
String host = "www.example.com";
String path = "/";
try
{
// Create a TCP socket
Socket socket = new Socket(host, 80);
// Create output stream to send request
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// Create input stream to read response
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Send HTTP GET request
out.println("GET " + path + " HTTP/1.1");
out.println("Host: " + host);
out.println("Connection: close");
out.println();
// Read and print the response
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
// Close the streams and socket
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:
Result :
Thus the web page is downloaded using TCP sockets in java.
EXP NO: 3 a
Applications using TCP sockets like:
DATE
a) Echo client and echo server
AIM:
To implement the application of Echo client and server using TCP sockets.
PROCEDURE:
● In the terminal, start the server by executing the compiled EchoServer.class file:
● Once the client and server are running, you can type messages in the client terminal.
● The server will receive these messages, echo them back, and you'll see the echoed messages in the
client terminal.
● Type "exit" in the client terminal to quit the client.
✔ Shutdown:
● To stop the server, press Ctrl + C in the terminal where it's running.
● The client can be stopped by typing "exit" and pressing Enter.
PROGRAM:
ECHO SERVER:
import java.io.*;
import java.net.*;
public class EchoServer {
public static void main(String[] args) {
final int PORT = 12345;
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress());
String message;
while ((message = reader.readLine()) != null) {
System.out.println("Received from client: " + message);
writer.println("Server echo: " + message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
ECHO CLIENT:
import java.io.*;
import java.net.*;
public class EchoClient
{
public static void main(String[] args)
{
final String SERVER_ADDRESS = "localhost";
final int SERVER_PORT = 12345;
try
{
Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
BufferedReader serverReader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
System.out.println("Connected to server. Type 'exit' to quit.");
String message;
while (true)
{
System.out.print("Client: ");
message = reader.readLine();
if (message.equalsIgnoreCase("exit"))
{
break;
}
writer.println(message);
String response = serverReader.readLine();
System.out.println("Server: " + response);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
OUTPUT:
RESULT:
Thus the Echo Server and Echo Client application is implemented using TCP sockets.
EXP NO: 3 b
Applications using TCP sockets like:
DATE
b) Chat
AIM:
To implement the application of Chat using TCP sockets.
PROCEDURE:
● In the terminal, start the server by executing the compiled EchoServer.class file:
● Once the client and server are running, you can type messages in the client terminal.
● The server will receive these messages, echo them back, and you'll see the echoed messages in the
client terminal.
● Type "exit" in the client terminal to quit the client.
✔ Shutdown:
● To stop the server, press Ctrl + C in the terminal where it's running.
● The client can be stopped by typing "exit" and pressing Enter.
PROGRAM:
ECHO SERVER:
import java.io.*;
import java.net.*;
public class chatServer {
public static void main(String[] args) {
final int PORT = 12345;
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress());
String message;
while ((message = reader.readLine()) != null) {
System.out.println("Received from client: " + message);
writer.println("Server echo: " + message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
CHAT CHATLIENT:
import java.io.*;
import java.net.*;
public class chatClient
{
public static void main(String[] args)
{
final String SERVER_ADDRESS = "localhost";
final int SERVER_PORT = 12345;
try
{
Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
BufferedReader serverReader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
System.out.println("Connected to server. Type 'exit' to quit.");
String message;
while (true)
{
System.out.print("Client: ");
message = reader.readLine();
if (message.equalsIgnoreCase("exit"))
{
break;
}
writer.println(message);
String response = serverReader.readLine();
System.out.println("Server: " + response);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
OUTPUT:
RESULT:
AIM:
To implement and simulate a basic DNS (Domain Name System) server using UDP (User
Datagram Protocol) sockets.
PROCEDURE:
● Start the DNS server by running the DNSServer class: java DNSServer
● In a separate command prompt or terminal window, run the DNSClient class: java DNSClient
✔ Observe Output:
● After running the client, you should see the resolved IP address printed on the client's console.
PROGRAM:
DNS Server:
import java.net.*;
while (true) {
// Create a datagram packet to receive data from the client
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
DNS Client:
import java.net.*;
AIM:
To create a simple Java client-server program using UDP sockets and examine the packets using
Wireshark.
PROCEDURE:
✔ Open Wireshark:
● In Wireshark, select the network interface that you want to capture packets on. This is typically your
Wi-Fi or Ethernet interface.
● In the client program, send data to the server by running the UDPClient.
● The client will send a message to the server over UDP.
● Apply a display filter in Wireshark to focus on UDP traffic on the port used by your server (e.g.,
udp.port == 9876).
✔ Examine Packets:
● Wireshark provides detailed information about each packet, including the contents of the packet
payload.
● Examine the packet details to understand the sequence of packets exchanged between the client and
server, and the contents of the messages sent.
● Once you've captured enough packets and examined them, you can stop the packet capture in
Wireshark.
PROGRAM:
UDP server:
import java.net.*;
try {
while (true) {
serverSocket.receive(receivePacket);
// Extract the received data
serverSocket.send(sendPacket);
} catch (Exception e) {
e.printStackTrace();
UDP client:
import java.net.*;
// Message to send
clientSocket.send(sendPacket);
clientSocket.receive(receivePacket);
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
OUTPUT
AIM:
TO implementation of ARP (Address Resolution Protocol) and RARP (Reverse Address Resolution
Protocol).
PROCEDURE:
● Initialize the ARP table with known mappings of IP addresses to MAC addresses.
● This step simulates the initial setup of the network's ARP cache.
● Simulate receiving an ARP reply containing the MAC address corresponding to the destination IP
address.
● Retrieve the MAC address from the ARP table using the destination IP address.
● Print a message indicating the received ARP reply, showing the resolved MAC address.
PROGRAM:
import java.util.HashMap;
import java.util.Map;
class ARP {
private static Map<String, String> arpTable = new HashMap<>();
sendARPRequest("192.168.1.2", "192.168.1.3");
}
}
OUTPUT:
PROCEDURE:
Ensure you have JDK installed on your system. You can download it from the Oracle website or
use OpenJDK.
Use an IDE such as IntelliJ IDEA, Eclipse, or NetBeans to write and run your Java programs.
IDEs provide a convenient environment for Java development.
o The Node class represents a network node with input and output queues for message passing.
o Define attributes and methods for sending and receiving messages.
o Implement Runnable interface for concurrent execution using threads.
● Set up nodes (Node A and Node B) and simulate a direct link between them.
● Start threads for each node to simulate concurrent execution.
● Send messages between nodes and observe the communication.
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
String message = inputQueue.take();
System.out.println(name + " received message: " + message);
Thread.sleep(1000); // Simulate processing delay
sendMessage(message); // Echo back the message
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
NetworkSimulation.java:
public class NetworkSimulation {
public static void main(String[] args) {
Node nodeA = new Node("Node A");
Node nodeB = new Node("Node B");
threadA.start();
threadB.start();
// Send messages
nodeA.sendMessage("Hello from Node A");
try {
Thread.sleep(3000); // Allow time for messages to propagate
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Output:
RESULT:
AIM:
To write a program for TCP/UDP performance using Simulation tool.
Procedure:
✔ Server.java:
✔ Client.java:
Server.java:
import java.io.*;
import java.net.*;
try {
// UDP Server
udpSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
// TCP Server
tcpSocket = new ServerSocket(6789);
Socket clientSocket = tcpSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while (true) {
// UDP Server
udpSocket.receive(receivePacket);
String udpMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("UDP Client: " + udpMessage);
// TCP Server
String tcpMessage = in.readLine();
System.out.println("TCP Client: " + tcpMessage);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (udpSocket != null) udpSocket.close();
if (tcpSocket != null) {
try {
tcpSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Client.java:
import java.io.*;
import java.net.*;
try {
// UDP Client
udpSocket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost");
byte[] sendData = "Hello UDP Server".getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress,
9876);
udpSocket.send(sendPacket);
// TCP Client
tcpSocket = new Socket("localhost", 6789);
PrintWriter out = new PrintWriter(tcpSocket.getOutputStream(), true);
out.println("Hello TCP Server");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (udpSocket != null) udpSocket.close();
if (tcpSocket != null) {
try {
tcpSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Output:
Aim:
To implement the Distance Vector/ Link State Routing algorithm
Procedure:
✔ Router.java:
● Represents a router with its ID, distance vector (mapping of destination router ID to cost), and
forwarding table (mapping of destination router ID to next hop router ID).
● Initializes with initial distance vectors and updates based on received distance vectors.
✔ NetworkSimulation.java:
Router.java:
import java.util.*;
import java.util.*;
public NetworkSimulation() {
this.routers = new HashMap<>();
}
Output:
Iteration 1:
Router 1 Distance Vector: {1=0, 2=1, 3=2}
Router 1 Forwarding Table: {2=2, 3=3}
Router 2 Distance Vector: {1=1, 2=0, 3=1}
Router 2 Forwarding Table: {1=1, 3=3}
Router 3 Distance Vector: {1=2, 2=1, 3=0}
Router 3 Forwarding Table: {1=1, 2=2}
Result:
Thus the Distance Vector/ Link State Routing is implemented
EXP NO: 10
Simulation of an error correction code (like CRC)
DATE
Aim:
To implement the Simulation of an error correction code (like CRC)
Procedure:
.
Program:
import java.util.*;
return crc;
}
if (receivedCRC == 0) {
System.out.println("CRC check passed: Data is intact.");
} else {
System.out.println("CRC check failed: Data is corrupted.");
}
}
}
Output:
Original Data: [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
Data with CRC: [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, -96, -38]
Received Data: [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, -96, -38]
Received CRC: 0
CRC check passed: Data is intact.
Result: