CS8581 NETWORKS LABORATORY 2017
CS8581 NETWORKS LABORATORY 2017
LIST OF EXPERIMENTS
1. Learn to use commands like tcpdump, netstat, ifconfig, nslookup and traceroute.
Capture ping and traceroute PDUs using a network protocol analyzer and examine.
2. Write a HTTP web client program to download a web page using TCP sockets.
b) Chat
c) File Transfer
using NS.
HARDWARE:
SOFTWARE:
AIM:
To write a java program for socket for HTTP for web page upload and download .
ALGORITHM:
import java.io.*;
import java.net.*;
public class SocketHTTPClient
{
public static void main(String[] args)
{
String hostName = "www.sunnetwork.in";
int portNumber = 80;
try
{
Socket socket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in =new BufferedReader(new
InputStreamReader(socket.getInputStream()));
out.println("GET / HTTP/1.1\nHost: www.sunnetwork.in\n\n");
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
}
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host " + hostName);
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to " + hostName);
System.exit(1);
}
}
}
OUTPUT:
RESULT:
Thus the program for creating sockets for HTTP web page to download was implemented.
EX.NO 3(A) APPLICATIONS USING TCP SOCKETS LIKE ECHO CLIENT
DATE: AND ECHO SERVER
AIM
To write a java program for applications using TCP sockets like Echo client and Echo server
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:
EchoServer.Java:
import java.io.*;
import java.net.*;
public class EchoServer
{
public EchoServer(int portnum)
{
try
{
server = new ServerSocket(portnum);
}
catch (Exception err)
{
System.out.println(err);
}
}
public void serve()
{
try
{
while (true)
{
Socket client = server.accept();
BufferedReader r = new BufferedReader(new
InputStreamReader(client.getInputStream()));
PrintWriter w = new
PrintWriter(client.getOutputStream(),
true);
w.println("Welcome to the Java EchoServer. Type 'bye'
to
close.");
String line;
do
{
line = r.readLine();
if ( line != null )
w.println("Got: "+ line);
System.out.println (line);
}
while ( !line.trim().equals("bye") );
client.close();
}
}
catch (Exception err)
{
System.err.println(err);
}
}
public static void main(String[] args)
{
EchoServer s = new EchoServer(9999);
s.serve();
}
private ServerSocket server;
}
EchoClient.java :
import java.io.*;
import java.net.*;
public class EchoClient
{
public static void main(String[] args)
{
try
{
Socket s = new Socket("127.0.0.1", 9999);
BufferedReader r = new BufferedReader(new
InputStreamReader(s.getInputStream()));
PrintWriter w = new PrintWriter(s.getOutputStream(), true);
BufferedReader con = new BufferedReader(new
InputStreamReader(System.in));
String line;
do
{
line = r.readLine();
if ( line != null )
System.out.println(line);
line = con.readLine();
w.println(line);
}
while ( !line.trim().equals("bye") );
}
catch (Exception err)
{
System.err.println(err);
}
}
}
OUTPUT:
RESULT:
Thus the java program to concurrently communicate using TCP Sockets was executed
successfully.
B.CHAT
AIM:
To implement a CHAT application using TCP Socket.
CONCEPT:
1. It uses TCP socket communication .We have a server as well as a client.
2. Both can be run in the same machine or different machines. If both are running in
the machine, the address to be given at the client side is local host address.
3. If both are running in different machines, then in the client side we need to specify
the ip address of machine in which server application is running.
PROGRAM:
Chatserver.java:
import java.net.*;
import java.io.*;
RESULT:
CHAT application using TCP Socket is implemented successfully.
C.FILE TRANSFER
AIM:
ALGORITHM:
Server
Step1: Import java packages and create class file server.
Step2: Create a new server socket and bind it to the port.
Step3: Accept the client connection
Step4: Get the file name and stored into the BufferedReader.
Step5: Create a new object class file and realine.
Step6: If file is exists then FileReader read the content until EOF is reached.
Step7: Stop the program.
Client
Step1: Import java packages and create class file server.
Step2: Create a new server socket and bind it to the port.
Step3: Now connection is established.
Step4: The object of a BufferReader class is used for storing data content which has been
retrieved from socket object.
Step5 The connection is closed.
Step6: Stop the program.
PROGRAM:
Serverfile:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverfile
{ public static void main(String args[])
{
try
{
ServerSocket obj=new ServerSocket(139);
while(true)
{
Socket obj1=obj.accept();
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
FileReader f=new FileReader(str);
BufferedReader b=new BufferedReader(f);
String s;
while((s=b.readLine())!=null)
{ System.out.println(s);
dout.writeBytes(s+'\n');
}
f.close();
dout.writeBytes("-1\n");
}}
catch(Exception e)
{ System.out.println(e);}
}
}
Clientfile.java
import java.io.*;
import java.net.*;
import java.util.*;
class Clientfile
{ public static void main(String args[])
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Socket clsct=new Socket("127.0.0.1",139);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the file name:");
String str=in.readLine();
dout.writeBytes(str+'\n');
System.out.println("Enter the new file name:");
String str2=in.readLine();
String str1,ss;
FileWriter f=new FileWriter(str2);
char buffer[];
while(true)
{ str1=din.readLine();
if(str1.equals("-1")) break;
System.out.println(str1);
buffer=new char[str1.length()];
str1.getChars(0,str1.length(),buffer,0);
f.write(buffer);
}
f.close();
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
D:\cn>java Clientfile
D:\cn>java Clientfile
D:\cn>javac Serverfile.java
D:\cn>java Serverfile
File content
good morning
have a nice day
Computer networks
jhfcgsauf
jbsdava
jbvuesagv
client
RESULT:
FILE TRANSFER application using TCP Socket is implemented successfully.
EX.NO 4. SIMULATION OF DNS USING UDP SOCKETS.
DATE:
AIM :
Write java program Simulation of DNS using UDP Sockets
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:
Server
E:\nwlab>java dnsserver
Press Ctrl + C to Quit
Request for host google.com
Request for host flipkart.com
Client
E:\nwlab>java dnsclient
Enter the hostname : google.com
IP Address: 172.217.11.14
E:\nwlab>java dnsclient
Enter the hostname : flipkart.com
IP Address: Host Not Found
E:\nwlab>
RESULT:
Thus the DNS application program was executed.
EX.NO 5A PROGRAM FOR ADDRESS RESOLUTION PROTOCOL (ARP) USING TCP
DATE:
AIM:
To write a java program for simulating ARP protocols using TCP
ALGORITHM:
Client
1. Start the program
2. Using socket connection is established between client and server.
3. Get the IP address to be converted into MAC address.
4. Send this IP address to server.
5. Server returns the MAC address to client.
Server
1. Start the program
2. Accept the socket which is created by the client.
3. Server maintains the table in which IP and corresponding MAC addresses are stored.
4. Read the IP address which is send by the client.
5. Map the IP address with its MAC address and return the MAC address to client.
PROGRAM:
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientarp
{
public static void main(String args[])
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Socket clsct=new Socket("127.0.0.1",5604);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the Logical address(IP):");
String str1=in.readLine();
dout.writeBytes(str1+'\n');
String str=din.readLine();
System.out.println("The Physical Address is: "+str);
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Server:
Import java.io.*;
Import java.net.*;
Import java.util.*;
class Serverarp
{
public static void main(String args[])
{
try
{
ServerSocket obj=new
ServerSocket(5604);
Socket obj1=obj.accept();
while(true)
{
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new DataOutputStream(obj1.getOutputStream()); String str=din.readLine();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
{
if(str.equals(ip[i]))
{
dout.writeBytes(mac[i]+'\n');
break;
}
}
obj.close();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
E:\networks>java Serverarp
E:\networks>java Clientarp
Enter the Logical address(IP):
165.165.80.80
The Physical Address is: 6A:08:AA:C2
RESULT:
Thus the ARP protocol using TCP Sockets program was executed.
AIM:
To write a java program for simulating RARP protocols using UDP
ALGORITHM:
Client
PROGRAM:
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientrarp
{
public static void main(String args[])
{
try
{
DatagramSocket client=new DatagramSocket();
InetAddress addr=InetAddress.getByName("127.0.0.1");
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Physical address (MAC):");
String str=in.readLine(); sendbyte=str.getBytes();
DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,1309);
client.send(sender);
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
client.receive(receiver);
String s=new String(receiver.getData());
System.out.println("The Logical Address is(IP): "+s.trim());
client.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverrarp
{
public static void main(String args[])
{
try
{
DatagramSocket server=new DatagramSocket(1309);
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
server.receive(receiver);
String str=new String(receiver.getData());
String s=str.trim();
InetAddress addr=receiver.getAddress();
int port=receiver.getPort();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
{
if(s.equals(mac[i]))
{
sendbyte=ip[i].getBytes();
DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
server.send(sender);
break;
}
}
break;
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
I:\ex>java Serverrarp12
I:\ex>java Clientrarp12
Enter the Physical address
(MAC): 6A:08:AA:C2
The Logical Address is(IP): 165.165.80.80
RESULT :
Thus the RARP program using UDP was executed.
EX.NO. SIMULATION OF LINK STATE ROUTING ALGORITHM
6
DATE:
AIM:
To simulate and study the link state routing algorithm using simulation.
SOFTWARE REQUIRED:
NS-2
THEORY:
In link state routing, each router shares its knowledge of its neighborhood with every other
router in the internet work. (i) Knowledge about Neighborhood: Instead of sending its entire routing
table a router sends info about its neighborhood only. (ii) To all Routers: each router sends this
information to every other router on the internet work not just to its neighbor .It does so by a process
called flooding. (iii)Information sharing when there is a change: Each router sends out information about
the neighbors when there is change.
PROCEDURE:
The Dijkstra algorithm follows four steps to discover what is called the shortest path
tree(routing table) for each router: The algorithm begins to build the tree by identifying its roots. The
root router’s trees the router itself. The algorithm then attaches all nodes that can be reached from the
root. The algorithm compares the tree’s temporary arcs and identifies the arc with the lowest
cumulative cost. This arc and the node to which it connects are now a permanent part of the shortest
path tree. The algorithm examines the database and identifies every node that can be reached from its
chosen node. These nodes and their arcs are added temporarily to the tree.
The last two steps are repeated until every node in the network has become a permanent part of the
tree.
ALGORITHM:
$ns rtproto LS
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)
$ns at 45 "finish"
$ns run
OUTPUT:
RESULT:
Thus the Link State Routing Algorithm was Simulated and studied.
AIM:
To simulate and study the Distance Vector routing algorithm using simulation.
SOFTWARE REQUIRED:
NS-2
THEORY:
Distance Vector Routing is one of the routing algorithm in a Wide Area Network for
computing shortest path between source and destination. The Router is one main devices used in
a wide area network. The main task of the router is Routing. It forms the routing table and
delivers the packets depending upon the routes in the table-either directly or via an intermediate
devices.
Each router initially has information about its all neighbors. Then this information will be shared
among nodes.
ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and
execute nam on trace file.
4. Create n number of nodes using for loop
5. Create duplex links between the nodes
6. Setup UDP Connection between n(0) and n(5)
7. Setup another UDP connection between n(1) and n(5)
8. Apply CBR Traffic over both UDP connections
9. Choose distance vector routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
PROGRAM:
$ns rtproto DV
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)
$udp0 set fid_ 1
$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green
$ns at 45 "finish"
$ns run
OUTPUT:
RESULT:
Thus the Distance vector Routing Algorithm was Simulated and studied.
EX.NO.8 SIMULATION OF CONGESTION CONTROL ALGORITHM
DATE :
AIM:
Write a program for congestion control using leaky bucket algorithm
THEORY:
The main concept of the leaky bucket algorithm is that the output data flow remains
constant despite the variant input traffic, such as the water flow in a bucket with a small hole at
the bottom. In case the bucket contains water (or packets) then the output flow follows a constant
rate, while if the bucket is full any additional load will be lost because of spillover. In a similar
way if the bucket is empty the output will be zero. From network perspective, leaky bucket
consists of a finite queue (bucket) where all the incoming packets are stored in case there is
space in the queue, otherwise the packets are discarded. In order to regulate the output flow,
leaky bucket transmits one packet from the queue in a fixed time (e.g. at every clock tick). In the
following figure we can notice the main rationale of leaky bucket algorithm, for both the two
approaches (e.g. leaky bucket with water (a) and with packets (b))
While leaky bucket eliminates bursty traffic by regulating the incoming data flow its main
drawback is that it drops packets if the bucket is full. Also, it doesn‘t take into account the idle
process of the sender which means that if the host doesn‘t transmit data for some time the bucket
becomes empty without permitting the transmission of any packet.
PROGRAM:
import java.io.*;
import java.util.*;
class Queue
{
int q[],f=0,r=0,size;
void insert(int n)
{
Scanner in = new Scanner(System.in);
q=new int[10];
for(int i=0;i<n;i++)
{
System.out.print("\nEnter " + i + " element: ");
int ele=in.nextInt(); if(r+1>10)
{ System.out.println("\nQueue is full \nLost Packet: "+ele);
break; }
else { r++; q[i]=ele;
}
}
}
void delete()
{
Scanner in = new Scanner(System.in);
Thread t=new Thread();
if(r==0)
System.out.print("\nQueue empty ");
else
{
for(int i=f;i<r;i++)
{
try
{
t.sleep(1000);
}
catch(Exception e)
{}
System.out.print("\nLeaked Packet: "+q[i]);
f++;
}
}
System.out.println();
}
}
class Leaky extends Thread
{
public static void main(String ar[]) throws Exception
{
Queue q=new Queue();
Scanner src=new Scanner(System.in);
System.out.println("\nEnter the packets to be sent:");
int size=src.nextInt();
q. insert(size);
q.delete();
}
}
OUTPUT:
E:\nwlab>java Leaky
Enter the packets to be sent:
12
Enter 0 element: 23
Enter 1 element: 34
Enter 2 element: 34
Enter 3 element: 334
Enter 4 element: 334
Enter 5 element: 34
Enter 6 element: 34
Enter 7 element: 34
Enter 8 element: 34
Enter 9 element: 34
Enter 10 element: 4
Queue is full
Lost Packet: 4
Leaked Packet: 23
Leaked Packet: 34
Leaked Packet: 34
Leaked Packet: 334
Leaked Packet: 334
Leaked Packet: 34
Leaked Packet: 34
Leaked Packet: 34
Leaked Packet: 34
Leaked Packet: 34
E:\nwlab>
RESULT:
Thus the program for congestion control using leaky bucket algorithm is implemented
and executed successfully.