0% found this document useful (0 votes)
26 views17 pages

Expt No1-3 Cnlab

The document outlines various network commands such as tcpdump, netstat, ipconfig, nslookup, traceroute, and ping, detailing their usage and syntax for capturing and analyzing network traffic. It also describes the implementation of a HTTP web client program using TCP sockets to download a webpage, along with an echo client-server program and a chat application using UDP sockets. The results indicate successful execution of the commands and programs, demonstrating effective network communication and data transfer.

Uploaded by

rajalakshmir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views17 pages

Expt No1-3 Cnlab

The document outlines various network commands such as tcpdump, netstat, ipconfig, nslookup, traceroute, and ping, detailing their usage and syntax for capturing and analyzing network traffic. It also describes the implementation of a HTTP web client program using TCP sockets to download a webpage, along with an echo client-server program and a chat application using UDP sockets. The results indicate successful execution of the commands and programs, demonstrating effective network communication and data transfer.

Uploaded by

rajalakshmir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Expt No.

1 Network Commands
Date:

AIM
To Learn to use network commands like tcpdump, netstat, ipconfig, nslookup and traceroute.
Capture ping and traceroute PDUs using a network protocol analyzer and examine in
Linux /windows environment.

COMMANDS
1. Tcpdump
Display traffic between 2 hosts
To display all traffic between two hosts (represented by variables host1 and host2):
# tcpdumphost 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 networkconnections. 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

C:\>ipconfig: The ipconfig command displays information about the host (the computer your
sitting at)computer TCP/IP configuration.
C:\>ipconfig /all: This command displays detailed configuration information about your
TCP/IP connection including Router, Gateway, DNS, DHCP, and type of Ethernet adapter in
your system.
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 TTLvalue 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 wayto the network device with the hostname

www.google.com.

#tracert google.com

C:\>tracert: The tracert command displays a list of all the routers that a packet has to go
through toget from the computer where tracert is run to any other computer on the
internet.
C:\>route: The route command displays the computers routing table.
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 networkwith 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
C:\>ping: Ping is the most basic TCP/IP command, and it’s the same as placing a phone
call to your best friend. You pick up your telephone and dial a number, expecting your
best friend to replywith “Hello” on the other end. Computers make phone calls to each
other over a network by usinga Ping command. The Ping commands main purpose is to
place a phone call to another computeron the network, and request an answer. Ping has
2 options it can use to place a phone call to another computer on the network. It can use
the computers name or IP address.

C:\>pathping: Pathping is unique to Window’s, and is basically a combination of the Ping


and Tracert commands. Pathping traces the route to the destination address then launches
a 25 second test of each router along the way, gathering statistics on the rate of data loss
along each hop.

RESULT
Thus the various network commands like tcpdump, netstat, ipconfig, nslookup and traceroute
ping are executed successfully.
Expt No:2 HTTP web client program to download a web page using TCP sockets
Date:

AIM:
To Write a HTTP web client program to download a web page using TCP sockets.
THEORY:
 A socket is a software object that acts as an end point establishing a bidirectional
networkcommunication link between a server-side and a client-side program.
 Client–server model is a distributed application structure that partitions tasks or
workloads between the providers of a resource or service, called servers, and service
requesters, called clients.
 HTTP means HyperText Transfer Protocol. HTTP is the underlying protocol used by
the World Wide Web and this protocol defines how messages are formatted and
transmitted, and whatactions Web servers and browsers should take in response to
various commands.
 A webpage is a document commonly written in HTML (Hypertext Markup Language)
that is accessible through the Internet or other networks using an Internet browser.
 URL stands for Uniform Resource Locator, and is used to specify addresses on the
World Wide Web. A URL is the fundamental network identification for any resource
connected to the web (e.g., hypertext pages, images, and sound files). The protocol
specifies how information from link is transferred.

ALGORITHM:

CLIENT SIDE
1) Start the program.
2) Create a socket which binds the Ip address of server and the port address to acquire
service.
3) After establishing connection send the url to server.
4) Open a file and store the received data into the file.
5) Close the socket.
6) End the program.

SERVER SIDE
1) Start the program.
2) Create a server socket to activate the port address.
3) Create a socket for the server socket which accepts the connection.
4) After establishing connection receive url from client.
5) Download the content of the url received and send the data to client.
6) Close the socket.
7) End the program.
PROGRAM

CLIENT:
import java.net.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;

public class client {


public static void main(String args[]) {
Socket soc = null;

try {
soc = new Socket("localhost", 4000);
System.out.println("Client is running.");

BufferedImage img = ImageIO.read(new File("1.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.");

} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
} finally {
try {
if (soc != null && !soc.isClosed()) {
soc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

SERVER :
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
public class server {
public static void main(String args[]) {
ServerSocket server = null;
Socket socket = null;

try {
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);

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);

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// Close the resources in a finally block to ensure they get closed
if (socket != null && !socket.isClosed()) {
socket.close();
}
if (server != null && !server.isClosed()) {
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
CLIENT OUTPUT

SERVER OUTPUT

RESULT:
The webpage is successfully downloaded and the contents are displayed and verified.
Expt No.3a Echo Client Server using TCP Sockets
Date:

AIM:
To write a socket program for implementation of echo.

THEORY:

 Socket programs are used to communicate between various processes usually running
ondifferent systems. It is mostly used to create a client-server environment.

 The java.net package provides 4 kinds of Sockets: Socket is a TCP client API, and will
typically be used to connect to a remote host. ServerSocket is a TCP server API, and
will typically accept connections from client sockets. DatagramSocket is a UDP
endpoint API and is used to send and receive datagram packets.

 Echo describes when data is sent to a computer or other network devices, and that
information is sent back to verify the information was received. Echo can be a command
used withoperating systems, network devices, or a function of a device.

ALGORITHM

Client
1. Start
2. Create the TCP socket which binds the Ip address of server and the port address to
acquireservice
3. Establish connection with the server
4. Get the message to be echoed fromthe user
5. Send the message to the server
6. Receive the message echoed by the server
7. Display the message received from the server
8. Terminate the connection
9. Stop

Server
1. Start
2. Create TCP socket, make it a listening socket
3. Accept the connection request sent by the client for connection establishment
4. Receive the message sent bythe client
5. Display the received message
6. Send the received message to the client from which it receives
7. Close the connection when client initiates termination and server becomes a
listening server,waiting for clients.
8. Stop.
PROGRAM:

ECHO CLIENT

Client program:-
import java.io.*;
import java.net.*;
public class echoclient
{
public static void main(String args[])
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
{
c=new Socket("localhost",8080);
}
catch(IOException e)
{
System.out.println(e);
}
try
{

os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());

System.out.println("CONNECTION ESTABLISHED");
do
{

System.out.println("client");
System.out.println("Enter the message:");
line=is.readLine();

os.println(line);
if(!line.equals("exit"))
System.out.println("server:"+is1.readLine());
}while(!line.equals("exit"));
}
catch(IOException e)
{
System.out.println("socket closed");
}}}
ECHO SERVER

Server program:-
package javaprograms;

import java.io.*;
import java.net.*;
import java.lang.*;
public class echoserver
{
public static void main(String args[])throws IOException
{
ServerSocket s=null;
String line;

DataInputStream is;
PrintStream ps;
Socket c=null;
try
{
s=new ServerSocket(8080);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
c=s.accept();
is=new DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());
System.out.println("CONNECTION ACCEPTED");
while(true)
{
line=is.readLine();
System.out.println("msg received by server and sent back to client");
ps.println(line);
}

}
catch(IOException e)
{
System.out.println(e);
}
}
}
OUTPUT:

CLIENT

SERVER

RESULT:
Thus the program for simulation of echo client server was written & executed.
Expt No:3b Client- Server Chat Application using Sockets
Date:

AIM:
To write a client-server application for chat using UDP socket.

THEORY:
 TCP/IP stands for Transmission Control Protocol/Internet Protocol, which is a set of
networking protocols that allows two or more computers to communicate. The Defense
Data Network, part of the Department of Defense, developed TCP/IP, and it has been
widely adopted asa networking standard.
 A socket API is an application programming interface (API), usually provided by the
operating system, that allows application programs to control and use network sockets.
 Chat is a real-time communication via keyboard between two or more users on a local
network (LAN) or over the Internet

ALGORITHM

Client
1. Start
2. Create the UDP datagram socket
3. Get the request message to be sent from the user
4. Send the request message to the server
5. If the request message is “END” go to step 10
6. Wait for the reply message fromthe server
7. Receive the reply message sent by the server
8. Display the reply message received from the server
9. Repeat the steps from 3 to 8
10. Stop

Server
1. Start
2. Create UDP datagram socket, make it a listening socket
3. Receive the request message sent bythe client
4. If the received message is “END” go to step 10
5. Retrieve the client’s IP address fromthe request message received
6. Display the received message
7. Get the reply message from the user
8. Send the reply message to the client
9. Repeat the steps from 3 to 8.
10. Stop.
PROGRAM

Client:-
package test2;

import java .io.*;


import java.net.*;
class UDPclient
{
public static DatagramSocket ds;
public static int clientport=789,serverport=790;
public static void main(String args[])throws Exception
{
byte buffer[]=new byte[1024];
ds=new DatagramSocket(serverport);
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
System.out.println("server waiting");
InetAddress ia=InetAddress.getLocalHost();
System.out.println("CONNECTION ESTABLSHED");
while(true)
{
System.out.println("Client");
System.out.println("Enter the message:");
String str=dis.readLine();
if(str.equals("end"))
break;
buffer=str.getBytes();
ds.send(new DatagramPacket(buffer,str.length(),ia,clientport));
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData(),0,p.getLength());

System.out.println("Server:" + psx);
}
}
}
SERVER:-
package test2;
import java.io.*;
import java.net.*;
class UDPserver
{
public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];
public static int clientport=789,serverport=790;
public static void main(String args[])throws Exception
{
ds=new DatagramSocket(clientport);
System.out.println("press ctrl+c to quit the program");
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));

InetAddress ia=InetAddress.getLocalHost();
System.out.println("CONNECTION ACCEPTED");
while(true)
{
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData(),0,p.getLength());
System.out.println("Client:" + psx);
System.out.println("Server");
System.out.println("Enter the message:");
String str=dis.readLine();
if(str.equals("end"))
break;
buffer=str.getBytes();

ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));
}
}
}
OUT PUT:

Client:

Server:

RESULT
Thus the client-server application for chat was executed successfully.

You might also like