CN Lab Manual-2-60
CN Lab Manual-2-60
CN Lab Manual-2-60
AIM:
To writea java program for simulating ping commands
ALGORITHM:
1. Start theprogram.
2. Get the frame size from theuser
3. To create the frame based on the user request.
4.To send frames to server from the clientside.
5. If your frames reach the server it will send ACK signal to client otherwise it will
send NACK signal toclient.
6. Stop theprogram
PROGRAM:
//pingclient.java
import java.io.*;
import java.net.*;
importjava.util.Calendar;
class pingclient
{
public static void main(String args[])throws Exception
{
String str;
int c=0;
long t1,t2;
Socket s=new Socket("127.0.0.1",5555);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream out=new PrintStream(s.getOutputStream());
while(c<4)
{
t1=System.currentTimeMillis();
str="Welcome to network programming world";
out.println(str);
System.out.println(dis.readLine());
t2=System.currentTimeMillis();
System.out.println(";TTL="+(t2-t1)+"ms");
c++;
}
s.close();
Output:
SERVER
RESULT:
Thus the program was implementing to simulating ping and traceroute commands
AIM:
A. To learn to use commands like tcpdump, netstat, ifconfig, nslookup and
traceroute
B. Capture ping and trace route PDUs using a network protocol analyzer and
examine.
PROCEDURE:
1. Tcpdump
1. tcpdump -i eth1
2. tcpdump -i eth1 -nn-s0-vport 80
-i : Select interface that the capture is to take place on, this will often be an ethernet card or
wireless adapter.
-nn : A single (n) will not resolve hostnames. A double (nn) will not resolve hostnames or
ports. This is handy for not only viewing the IP / port numbers but also when capturing a
large amount of data, as the name resolution will slow down the capture.
-s0 : Snap length, is the size of the packet to capture.
o -s0 will set the size to unlimited - use this if you want to capture all the traffic.
-v : Verbose, using (-v) or (-vv) increases the amount of detail shown in the output, often
showing more protocol specific information.
port 80 : this is a common port filter to capture only traffic on port 80
Combine Filters
9. tcpdump -nn -A -s1500 -l | grep "User-Agent:"
2. ifconfig
3. nslookup
A. Ping Command
1. Observe the traffic captured in the top Wireshark packet list pane. Look for traffic with
ICMP listed as the protocol. To view only ICMP traffic, type icmp (lower case) in the
Filter box and press Enter.
2. Select the first ICMP packet, labeled Echo (ping) request.
3. Observe the packet details in the middle Wireshark packet details pane. Notice that it is
an Ethernet II / Internet Protocol Version 4 / Internet Control Message Protocol frame.
4. Expand Internet Control Message Protocol to view ICMP details.
5. Observe the Type. Notice that the type is 8 (Echo (ping) request).
6. Select Data in the middle Wireshark packet details pane to highlight the data portion of
the frame.
7. Observe the packet contents in the bottom Wireshark packet bytes pane.
1. In the top Wireshark packet list pane, select the second ICMP packet, labeled Echo
(ping) reply.
2. Observe the packet details in the middle Wireshark packet details pane. Notice that it is
an Ethernet II / Internet Protocol Version 4 / Internet Control Message Protocol frame.
3. Expand Internet Control Message Protocol to view ICMP details.
4. Observe the Type. Notice that the type is 0 (Echo (ping) reply).
5. Select Data in the middle Wireshark packet details pane to highlight the data portion of
the frame.
6. Observe the packet contents in the bottom Wireshark packet bytes pane. Notice that the
reply echoes the request sequence.
7. Close Wireshark to complete this activity. Quit without saving to discard the captured
traffic.
B.traceroute
Trace route
RESULT:
Thus the network commands are studied and ping and traceroute PDUs are captured
using a network protocol analyzer. Finally packets are examined.
AIM:
To write a java program using sockets and TCP to download the web page.\
PROCEDURE:
1. Open the Start -> All Programs -> Accessories -> Notepad
2. Save the file name with URLdemo.java
3. Import the necessary statement to the URLdemo.java
4. Create a class called URLdemo with main function
5. In the main function declare the variables and web object of URL with url in constructor
6. Create a con object for URLConnection
7. Display the date, content type, last modification, content length and finally write a code
to display the content of the file.
8. Save and compile the file
Javac URLdemo.java
9. Run the program using
Java URLdemo
10. Finally verify the output.
Code:
URLdemo.java
import java.net.*;
import java.io.*;
importjava.util.Date;
classURLdemo
{
public static void main(String[] arg) throws Exception
{
int c;
URL web = new URL("http://www.mzcet.in/default.aspx");
URLConnection con = web.openConnection();
System.out.println("Date: "+ new Date(con.getDate()));
System.out.println("Content-Type: " + con.getContentType());
System.out.println("Expires: "+ con.getExpiration());
System.out.println("Last-Modified: " + new Date(con.getLastModified()));
intlen = con.getContentLength();
Thus the web page is downloaded using TCP and output is verified.
AIM:
To write a program to implement ECHO.
PROCEDURE:
1. Open the Start -> All Programs -> Accessories -> Notepad
2. Save the file name with echoclient.java and echoserver.java
3. Import the necessary statement to the echoclient.java and echoserver.java
4. Create classes called echoclient.java and echoserver.java with main function.
5. Open the Socket in the server and listen for the client request
6. Create a socket and request the connection in client.
7. Server accepts and establishes the connection.
8. Client receive the message and echo the message to server
9. Compile and execute the echoclient.java&echoserver.java
PROGRAM:
echoclient.java
//echo client.java
import java.io.*;
import java.net.*;
importjava.util.*;
public class echoclient
{
public static void main(String args[])throws Exception
{
Socket c=null;
DataInputStreamusr_inp=null;
DataInputStream din=new DataInputStream(System.in);
DataOutputStreamdout=null;
try
{
c=new Socket("127.0.0.1",5678);
usr_inp=new DataInputStream(c.getInputStream());
dout=new DataOutputStream(c.getOutputStream());
}
catch(IOException e)
{
}
echoserver.java
//echoserver.java
import java.io.*;
import java.net.*;
public class echoserver
{
public static void main(String args[])throws Exception
{
ServerSocket m=null;
Socket c=null;
DataInputStreamusr_inp=null;
DataInputStream din=new DataInputStream(System.in);
DataOutputStreamdout=null;
try
{
m=new ServerSocket(5678);
c=m.accept();
usr_inp=new DataInputStream(c.getInputStream());
dout=new DataOutputStream(c.getOutputStream());
}
catch(IOException e)
{}
if(c!=null || usr_inp!=null)
{
String unip;
while(true)
{
System.out.println("\nMessage from Client...");
String m1=(usr_inp.readLine());
System.out.println(m1);
dout.writeBytes(""+m1);
OUTPUT:
SERVER
CLIENT
RESULT:
AIM:
To write a program to implement TCP Chat.
PROCEDURE:
1. Open the Start -> All Programs -> Accessories -> Notepad
2. Save the file name with tcpcli.java and tcpser.java
3. Import the necessary statement to the tcpcli.java and tcpser.java
4. Create classes called tcpcli.java and tcpser.java with main function.
SERVER:
1. Establish the connection of socket.
2. Assign the local Protocol address 3535 to the socket.
3. Move the socket from closed to listener state and provide maximum no. of Connections.
4. Create a new socket connection using client address.
5. Read the data from the socket and display
6. Get the message from the user &Write the data into socket.
7. Repeat steps 4 to 6 till exit is typed
8. Close the socket.
CLIENT:
1. Open the socket.
2. Get the host name and port number 3535 from client.
3. Write a request to the buffer that contain the request number as a byte to the output stream.
4. Get the message from the user.
5. Write to the socket.
6. Set the write operation for success.
7. Read the contents from the socket and print
8. Repeat steps 4 to 7 till exit is typed
9. Close the socket
Program:
Server:
import java.io.*;
import java.net.*;
class tcpser
{
static finalint port = 3535;
public static void main(String args[ ])
{
try
{
ServerSocketss=new ServerSocket(port);
OUTPUT:
SERVER
CLIENT
RESULT:
Thus the chat application is implemented using TCP using sockets in java
AIM:
To implement the file transfer using TCP in java.
PROCEDURE:
1. Open the Start -> All Programs -> Accessories -> Notepad
2. Save the file name with ft2server.java and ft2client.java
3. Import the necessary statement to the ft2server.java and ft2client.java
4. Create classes ft2server.java and ft2client.java with main function.
CLIENT:
1. Start, import all the necessary packages.
2. Create a client socket and connect to server using port & IP address.
3. Send a file request to server.
4. Then server responses are retrieved using input & output streams.
5. Close the socket.
SERVER:
1. Import all the necessary packages.
2. Create a client and server socket and accept client connection.
3. Transfer files that user requested to transfer using output stream.
4. Close the socket and trace the output.
PROGRAM:
Server:
import java.io.*;
import java.net.*;
importjava.util.*;
public class ft2server
{
public static void main(String args[])throws IOException
{
ServerSocketss=null;
try
{
ss=new ServerSocket(8081);
}
catch(IOException e)
{
System.out.println("couldn't listen");
System.exit(0);
}
Socket cs=null;
try
{
}
fs.close();
System.out.println("File received");
s.close();
}
}
CLIENT:
SERVER:
RESULT:
Thus file transfer is implemented using TCP and Sockets in java program.
AIM:
To write a java program to simulate DNS.
PROCEDURE:
1. Open the Start -> All Programs -> Accessories -> Notepad
2. Save the file name with Udpdnsserver.java and Udpdnsclient.java
3. Import the necessary statement to the Udpdnsserver.java and Udpdnsclient.java
4. Create classes Udpdnsserver.java and Udpdnsclient.java with main function.
Server:
1. Import all the necessary packages.
2. Create a client and server socket and accept client connection.
3. Create a string array with host and ip with its value.
4. Listen to the request from client
5. If you receive request with hostname then respond with the respective ip address.
6. Close the socket and trace the output.
Client:
1. Start, import all the necessary packages.
2. Create a client socket and connect to server using port & IP address of the server.
3. Get the users input a hostname and parse it to the server.
4. Then server responses are retrieved using output streams.
5. Close the socket.
PROGRAM:
Udpdnsserver.java
// UDP DNS Server
import java.io.*;
import java.net.*;
public class udpdnsserver
{
private static intindexOf(String[] array, String str)
{
str = str.trim();
for (int i=0; i <array.length; i++)
{
if (array[i].equals(str)) return i;
}
return -1;
}
public static void main(String arg[])throws IOException
{
String[] hosts = {"yahoo.com", "gmail.com","cricinfo.com", "facebook.com"};
OUTPUT
CLIENT
SERVER
RESULT:
Thus the function of DNS is simulated using java program.
AIM:
To write a java program for simulating ARP protocols
PROCEDURE:
Server
1. Create a server socket and bind it to port.
2. Listen for new connection and when a connection arrives, accept it.
3. Receive the logical address.
4. Find and reply with the MAC address that matches with the IP address.
5. Close all streams.
6. Close the server socket.
7. Stop.
Client
1. Create a client socket and connect it to the server‟s port number.
2. Retrieve its own IP address using built-in function or get IP address as user input.
3. Send its address to the server.
4. Receive the MAC address from the server and display.
5. Close the input and output streams.
6. Close the client socket.
7. Stop.
Program
Serverarp.java
import java.io.*;
import java.net.*;
importjava.util.*;
classServerarp
{
public static void main(String args[])
{
try
{
ServerSocketobj=new ServerSocket(139);
Socket obj1=obj.accept();
while(true)
{
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStreamdout=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]))
{
RESULT:
Thus the Simulation of the ARP protocol is done using java program.
AIM:
To write a java program for simulating RARP protocols
PROCEDURE:
Server
1. Create a server socket and bind it to port.
2. Listen for new connection and when a connection arrives, accept it.
3. Receive the physical address.
4. Find and reply with the IP address that matches with the MAC address.
5. Close all streams.
6. Close the server socket.
7. Stop.
Client
1. Create a client socket and connect it to the server‟s port number.
2. Retrieve its own MAC address using built-in function or get MAC address as user input.
3. Send its address to the server.
4. Receive the MAC address from the server and display.
5. Close the input and output streams.
6. Close the client socket.
7. Stop.
Program
Serverrarp12.java
import java.io.*;
import java.net.*;
importjava.util.*;
class Serverrarp12
{
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();
//System.out.println(s);
InetAddressaddr=receiver.getAddress();
int port=receiver.getPort();
Result:
Thus the Simulation of the RARP protocol is done using java program.
The above figure shows the general architecture of NS and a general user (not an NS
developer) can be thought of standing at the left bottom corner, designing and running-
simulations in Tcl using the simulator objects in the OTcl library. The event schedulers and most
of the network components are implemented in C++ and available to OTcl through an OTcl
linkage that is implemented using Tcl. The whole thing together makes NS, which is an object
oriented extended Tcl interpreter with network simulator libraries.
Obtaining Simulation Results:
When a simulation is finished, NS produces one or more text-based output files that
contain detailed simulation data, if specified to do so in the input Tel (or more specifically, OTcl)
script. The data can be used for simulation analysis or as an input to a graphical simulation
display tool called Network Animator (NAM) that is developed as a part of VINT project. AM
has a nice graphical user interface similar to that of a CD player (play, fast forward, rewind,
pause and so on), and also has a display speed controller. Furthermore, it can graphically present
information such as throughput and number of packet drops at each link, although the graphical
Network Construction:
a) This network. Consists of 4 nodes (n0. n1, n2, n3) as shown in above figure.
b) The duplex links between n0 and n2, and n1 and n2 have 2 Mbps of bandwidth and 10 ms
of delay.
c) The duplex link between n2 and n3 has 1.7 Mbps of bandwidth and 20 ms of delay.
d) Each node uses a Drop Tail queue, of which the maximum size is 10.
e) A “tcp” agent is attached to n0, and a connection is established to a tcp “sink” age
attached to n3.
f) As default, the maximum size of a packet that a “tcp” agent can generate is 1Kbyte.
g) A tcp “sink” agent generates and sends ACK packets to he sender (tcp agent) and frees
the received packets.
k) The “cbr” is set to start at 0.1 sec and stop at 4.5 sec.
l) The “ftp” is set to start at 1.0 sec and stop at 4.0 Sec.
Sending Data:
Once nodes constructed and the link established we can have process of transmitting the
following diagram shows the flow of data from node 0 to node 1.
Creating A Topology:
Once the basic components like node construction, link establishment, setting the type of
source, queue monitors we can go for the construction of required topology, mostly all the
networking simulations are concentrated on the visualization of the effect produced by a
topology as a whole. The following diagram shows the initial basic topology.
Multicast Routing:
$ns duplex-link Sn1 $n2 1.5Mb 10ms Drop Tail // Establish link for node 1 and node 2
$ns duplex-link Sn1 $n3 1.5Mb 10ms Drop Tail //Establish link for node 1 and node 3
At The Prompt:
After editing the code for multicast routing save the file with any file name but with a .tcl
extension and at the UNIX prompt, perform the following.
ns<filename>.tclif (example) ns mcasttd
An output animator file would then be created, the name of the animator file would be
out.nam. Then run the generated output animator file using namavailable in the shell.
Now, the network animation for muhicast programming can be viewed graphically.
RESULT:
Thus the study about network simulator is done.
AIM:
To study about the TCP/UDP performance using Simulation tool
PROCEDURE:
1. Create a network simulator object.
2. Define different colors for data flow.
3. Write a finish Procedure to close nam.
4. Create four nodes for the simulation purpose.
5. Then Create link between nodes.
6. Setup nodes position.
7. Setup the UDP connection with the node.
8. Setup the CBR for UDP.
9. Setup the CBR for TCP.
10. Measure the performance.
PROGRAM:
OUTPUT:
RESULT:
Thus the performance study of TCP/UDP is done successfully using NS2 simulation tool.
AIM:
To simulate and study of link state routing.
PROCEDURE:
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 Link state routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
PROGRAM:
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
setnf [open thro.nam w]
$ns namtrace-all $nf
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
execnamthro.nam&
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }
$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
RESULT:
AIM:
To simulate and study the distance vector routing algorithm using simulation.
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:
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
setnf [open thro.nam w]
$ns namtrace-all $nf
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
execnamthro.nam&
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }
$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
RESULT:
Thus the distance vector routing algorithm was simulated and studied.
AIM:
To study the link state routing
PROCEDURE:
i.Link State routing:
Routing is the process of selecting best paths in a network. In the past, the term routing
was also used to mean forwarding network traffic among networks. However this latter function
is much better described as simply forwarding. Routing is performed for many kinds of
networks, including the telephone network (circuit switching), electronic data networks (such as
the Internet), and transportation networks. This article is concerned primarily with routing in
electronic data networks using packet switching technology.
In packet switching networks, routing directs packet forwarding (the transit of logically
addressed network packets from their source toward their ultimate destination) through
intermediate nodes. Intermediate nodes are typically network hardware devices such as routers,
bridges, gateways, firewalls, or switches. General-purpose computers can also forward packets
and perform routing, though they are not specialized hardware and may suffer from limited
performance. The routing process usually directs forwarding on the basis of routing tables which
maintain a record of the routes to various network destinations. Thus, constructing routing tables,
which are held in the router's memory, is very important for efficient routing. Most routing
algorithms use only one network path at a time. Multipath routing techniques enable the use of
multiple alternative paths.
In case of overlapping/equal routes, the following elements are considered in order to decide
which routes get installed into the routing table (sorted by priority):
1. Prefix-Length: where longer subnet masks are preferred (independent of whether it is within a
routing protocol or over different routing protocol)
2. Metric: where a lower metric/cost is preferred (only valid within one and the same routing
protocol)
3. Administrative distance: where a lower distance is preferred (only valid between different
routing protocols)
Routing, in a more narrow sense of the term, is often contrasted with bridging in its
assumption that network addresses are structured and that similar addresses imply proximity
within the network. Structured addresses allow a single routing table entry to represent the route
to a group of devices. In large networks, structured addressing (routing, in the narrow sense)
outperforms unstructured addressing (bridging). Routing has become the dominant form of
addressing on the Internet. Bridging is still widely used within localized environments.
Disadvantages:
Flooding can be costly in terms of wasted bandwidth. While a message may only have
one destination it has to be sent to every host. In the case of a ping flood or a denial of
service attack, it can be harmful to the reliability of a computer network.
RESULT:
Thus the case study is done with different protocols
AIM:
To write a java program to simulate the CRC technique
PROCEDURE:
1. Open the notepad
2. Import necessary header files.
3. Create a class called CRC with the main function
4. Declare needed variable
5. Implement the CRC code
6. Display the code bit added
PROGRAM:
CRC.java
importjava.util.Scanner;
class CRC
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
intm,g[],n,d[],z[],r[],msb,i,j,k;
System.out.print("Enter no. of data bits : ");
n=sc.nextInt();
System.out.print("Enter no. of generator bits : ");
m=sc.nextInt();
d=new int[n+m];
g=new int[m];
System.out.print("Enter data bits : ");
for(i=0;i<n;i++)
d[i]=sc.nextInt();
System.out.print("Enter generator bits : ");
for(j=0;j<m;j++)
g[j]=sc.nextInt();
for(i=0;i<m-1;i++)
d[n+i]=0;
r=new int[m+n];
for(i=0;i<m;i++)
r[i]=d[i];
z=new int[m];
for(i=0;i<m;i++)
z[i]=0;
for(i=0;i<n;i++)
{
k=0;
msb=r[i];
RESULT:
Thus the CRC is simulated using java program.