Printout For Record - CS3591 CN Lab
Printout For Record - CS3591 CN Lab
NO:1 Learn to use commands like tcpdump, netstat, ifconfig, nslookup and trace
route.
Capture ping and trace route PDUs using a network protocol analyzer and examine.
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:
Trace route is a network diagnostic tool used to track the pathway taken by a packet on an IP
network from source to destination. Trace route also records the time taken for each hop the
packet makes during its route to the destination
Commands:
1. Tcpdump:
# tcpdump src host # tcpdump
dst host
# tcpdump src port ftp # tcpdump
dst port http
2. Netstat
#netstat
3. ipconfig
#ipconfig
4. nslookup
#nslookup
5. Trace route:
#tracert google.com
6. Ping:
# ping172.16.6.2
OUTPUT:
Ex.No: 2 Write a HTTP web client program to download a web page using TCP sockets
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();
System.out.println("Sending image to server.");
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:
Ex.No: 3 Applications using TCP sockets like: Echo client and echo server,
Chat and File Transfer
PROGRAM:
Client:
import java.net.*;
import java.io.*;
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try{
c=new Socket("172.27.101.243",9000);
catch(Exception e){
System.out.println(e);
try{
System.out.println("input:");
is=new DataInputStream(System.in);
os=new PrintStream(c.getOutputStream());
is1=new DataInputStream(c.getInputStream());
while(true)
System.out.println("client:");
line=is.readLine();
os.println(line);
System.out.println("server:"+is1.readLine());
}}
catch(Exception e){
SERVER:
import java.net.*;
import java.io.*;
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try
s=new ServerSocket(9000);
catch(Exception e)
System.out.println(e);
try
c=s.accept();
is=new DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());
while(true)
line=is.readLine();
ps.println(line);
System.out.println(line);
catch(Exception e)
System.out.println(e);
}
OUTPUT:
CLIENT:
SERVER:
(II) CHAT
PROGRAM
CLIENT:
import java.net.*;
import java.io.*;
class client
String str="",str2="";
while(!str.equals("stop"))
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("server says:"+str2);
dout.close();
s.close();
}}
SERVER:
import java.net.*;
import java.io.*;
class server
Socket s=ss.accept();
String str="",str2="";
while(!str.equals("stop"))
str=din.readUTF();
System.out.println("client says:"+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
din.close();
s.close();
ss.close();
}
OUTPUT:
CLIENT:
SERVER:
(III) FILE TRANSFER
PROGRAM
CLIENT:
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
InputStream is = socket.getInputStream();
int bytesRead = 0;
while((bytesRead=is.read(contents))!=-1)
bos.write(contents, 0, bytesRead);
bos.flush();
socket.close();
SERVER:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class FileServer{
InetAddress IA = InetAddress.getByName("localhost");
OutputStream os = socket.getOutputStream();
byte[] contents;
long current = 0;
while(current!=fileLength){
current += size;
else{
current = fileLength; }
bis.read(contents, 0, size);
os.write(contents);
os.flush();
socket.close();
ssock.close();
}}
OUTPUT:
CLIENT:
SERVER:
Ex.No: 4 Simulation of DNS using UDP Sockets
PROGRAM
import java.net.*;
import java.io.*;
import java.util.*;
int n;
do
n=Integer.parseInt(System.console().readLine());
if(n==1)
try
String hname=in.readLine();
InetAddress address;
address=InetAddress.getByName(hname);
System.out.println("host name:"+address.getHostName());
System.out.println("IP:"+address.getHostAddress());
catch(IOException ioe){
ioe.printStackTrace();
}}
if(n==2)
try
System.out.println("\nenter IP address");
String ipstr=in.readLine();
InetAddress ia=InetAddress.getByName(ipstr);
System.out.println("Hostname:"+ia.getHostName());
catch(IOException ioe)
ioe.printStackTrace();
}}
}while(!(n==3));}}
OUTPUT:
Ex.No: 5 Wireshark to capture packets and examine the packets
Wireshark v3 organizes the output into three vertically stacked window panes. Wireshark v4 uses the same
three panes, but the Packet Details pane is in the lower-left corner -- it was the middle pane in v3 -- and the
Packet Bytes pane is in the lower-right corner.
Select a frame in the Packet List pane
In the new Wireshark interface, the top pane summarizes the capture. It displays one or more frames,
along with the packet number, time, source, destination, protocol, length and info fields.
Use the protocols, source and destination addresses, and ports columns to help you decide which
frame to examine. Click on a frame to select it, and then look at the two lower panes for details.
The sample capture for these screenshots is a simple Whois query to www.iana.org. This example
generates DNS and other traffic that is handy for the explanations below
Frame content
This frame section provides Ethernet information, such as frame size, time of capture and the physical
interface on which the frame was captured.
Use this information:
Administrators can use this information to examine frame size, for example. Certain
devices might have issues accepting frames that exceed the standard size.
Troubleshooters can also verify which interface the data was captured on, ensuring
information flows through the proper connection.
Ethernet content
Next is Ethernet II content, including source and destination MAC addresses.
Depending on the frame's direction of travel, the local MAC address is either the source
or destination address, and the next network device's MAC address is the other.
Next is the IP section, with source and destination IP addresses and port numbers. For
most networks, the address structure is IPv4. Time-to-live information exists here, as
does fragmentation instructions. Finally, a field defines whether the packet uses TCP
or UDP at the transport layer.
Use this information:
Network technicians can verify the IP addresses are valid and expected. Remember, an address
beginning with 169.254.x.x is not valid on the network and indicates a possible Dynamic Host
Configuration Protocol problem. Techs can also confirm the source and destination IP addresses are as
expected to reduce issues of DNS poisoning or incorrect name resolution settings.
Transport content
Next is a section containing transport layer information. You should see either TCP or UDP here,
depending on the type of datagram captured. Remember, TCP uses the three-way handshake to
enumerate the data exchange, ensuring that the source device resends any lost data.
This section displays the source and destination ports, too. If the packet originates from the client
computer, the destination port is the service port number. For example, if the destination system is a
web server, the destination port number is 80 or 443 (HTTP or HTTPS). The client's port number is a
randomly generated number between 1,024 and 65,535 (this range varies by OS). Both port numbers
appear in this header.
The Packet Bytes pane in the lower-right corner of Wireshark displays the payload. This content can
be the end-user data security professionals worry about. Unencrypted protocols, such as HTTP, Telnet,
SMTP and others, don't protect the confidentiality of their payload, so the data is shown in this
window.
ARP PROTOCOL:
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",139)
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(139); 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
RARP PROTOCOL:
PROGRAM:
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientrarp12
{
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=newDatagramPacket(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 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();
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.8
Ex.No: 7 Study of Network simulator (NS) and Simulation of Congestion Control
Algorithms using NS
Program:
include <wifi_lte/wifi_lte_rtable.h>
struct r_hist_entry *elm, *elm2;
int num_later = 1;
elm = STAILQ_FIRST(&r_hist_);
while (elm != NULL && num_later <= num_dup_acks_){
num_later;
elm = STAILQ_NEXT(elm, linfo_);
}
if (elm != NULL){
elm = findDataPacketInRecvHistory(STAILQ_NEXT(elm,linfo_));
if (elm != NULL){
elm2 = STAILQ_NEXT(elm, linfo_);
while(elm2 != NULL){
if (elm2->seq_num_ < seq_num && elm2->t_recv_ <
time){
STAILQ_REMOVE(&r_hist_,elm2,r_hist_entry,linfo_);
delete elm2;
} else
elm = elm2;
elm2 = STAILQ_NEXT(elm, linfo_);}
}}}
void DCCPTFRCAgent::removeAcksRecvHistory(){
struct r_hist_entry *elm1 = STAILQ_FIRST(&r_hist_);
struct r_hist_entry *elm2;
int num_later = 1;
while (elm1 != NULL && num_later <= num_dup_acks_){
num_later;
elm1 = STAILQ_NEXT(elm1, linfo_);
}
if(elm1 == NULL)
return;
elm2 = STAILQ_NEXT(elm1, linfo_);
while(elm2 != NULL){
if (elm2->type_ == DCCP_ACK){
STAILQ_REMOVE(&r_hist_,elm2,r_hist_entry,linfo_);
delete elm2;
} else {
elm1 = elm2;
}
elm2 = STAILQ_NEXT(elm1, linfo_);
}
}
inline r_hist_entry
*DCCPTFRCAgent::findDataPacketInRecvHistory(r_hist_entry *start){
while(start != NULL && start->type_ == DCCP_ACK)
start = STAILQ_NEXT(start,linfo_);
return start;
}
Ex.No: 8 Study of TCP/UDP performance using Simulation tool.
TCP performance:
PROGRAM:
set ns [new Simulator]
$ns color 0 Blue
$ns color 1 Red
$ns color 2 Yellow
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set f [open tcpout.tr w]
$ns trace-all $f
set nf [open tcpout.nam w]
$ns namtrace-all $nf
$ns duplex-link $n0 $n2 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 5Mb 2ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-up
$ns duplex-link-op $n1 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5
set tcp [new Agent/TCP]
$tcp set class_ 1
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $tcp
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.2 "$ftp start"
$ns at 1.35 "$ns detach-agent $n1 $tcp ; $ns detach-agent $n3 $sink"
$ns at 3.0 "finish"
proc finish {}
{
global ns f nf
$ns flush-trace
close $f
close $nf
puts "Running nam.."
exec xgraph tcpout.tr -geometry 600x800 &
exec nam tcpout.nam &
exit 0
}
$ns run
Output:
UDP Performance:
PROGRAM:
$ns trace-all $f
proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
exit 0
$ns run
Output:
Ex.No: 9 Simulation of Distance Vector/ Link State Routing algorithm.
PROGRAM
$ns rtproto LS
proc finish {} {
global ns f0 nf
$ns flush-trace
close $f0
close $nf
exit 0}
$ns run
Output:
DISTANCE VECTOR ROUTING
PROGRAM
$ns rtproto DV
# Open tracefile
global ns nf
$ns flush-trace
close $nf
exit 0}
# Create 8 nodes
#Schedule events for the CBR agent and the network dynamics
$ns run
OUTPUT
Ex.No:10 Simulation of Error Detection Code (like CRC)
PROGRAM:
import java.io.*;
class crc_gen{
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
data=new int[data_bits];
data[i]=Integer.parseInt(br.readLine());
divisor[i]=Integer.parseInt(br.readLine());
System.out.print(data[i]);
System.out.println();
System.out.println();
*/
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/* CRC GENERATION */
for(int i=0;i<data.length;i++)
div[i]=data[i];
System.out.print("Dividend (after appending 0's) are : "); for(int i=0; i< div.length; i++)
System.out.print(div[i]);
System.out.println();
rem[j] = div[j];}
for(int i=0;i<div.length;i++){
crc[i]=(div[i]^rem[i]);}
System.out.println();
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);
/* ERROR DETECTION */
System.out.println();
System.out.println("Enter CRC code of "+tot_length+" bits : "); for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());
System.out.print(crc[i]);
System.out.println();
rem[j] = crc[j];}
if(rem[i]!=0){
System.out.println("Error");
break;}
if(i==rem.length-1)
System.out.println("No Error");}
int cur=0;
while(true){
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);
cur++;
if((rem.length-cur)<divisor.length)
break;}
return rem;}}
OUTPUT :
Enter number of data bits :
7
Enter data bits :
1
0
1
1
0
0
1
Enter number of bits in divisor :
3
Enter Divisor bits :
1
0
1
Dividend (after appending 0's) are : 101100100
CRC code :
101100111
Enter CRC code of 9 bits :
1
0
1
1
0
0
1
0
1
crc bits are : 101100101
Error
THANK YOU. )
BUILD SUCCESSFUL (total time: 1 minute 34 seconds)