Cs8581 Networks Laboratory List of Experiments
Cs8581 Networks Laboratory List of Experiments
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.
3. Applications using TCP sockets like:
Echo client and echo server
Chat
File Transfer
4. Simulation of DNS using UDP sockets.
5. Write a code simulating ARP /RARP protocols.
6. Study of Network simulator (NS) and Simulation of Congestion Control Algorithms using NS.
7. Study of TCP/UDP performance using Simulation tool.
8. Simulation of Distance Vector/ Link State Routing algorithm.
9. Performance evaluation of Routing protocols using Simulation tool.
10. Simulation of error correction code (like CRC).
Ex.No:2 Create a Socket for HTTP For Web Page Upload And Download
Aim:
To implement HTTP socket for web page upload and download in java.
Algorithm:
To Download
1. Start the program by initializing java.net.URL.
2. Find the location of the file to be downloaded and pass the URL through a string.
3. Connect to the internet and run the program, the desired file will be downloaded.
4. Stop the program.
To Upload
1. Initialize a local client and local server.
2. Get the location of the file to be uploaded
3. Connect the server with the client.
4. Mention the data type, size and location of the file to be uploaded.
5. Establish the connection and run the client program to upload the data.
6. Run the server program to receive the uploaded data.
7. Once data is received at the server end stop the program.
Program:
To Download Image:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class Download {
public static void main(String[] args) throws Exception {
try{
String fileName = "digital_image_processing.jpg";
String website = "http://tutorialspoint.com/java_dip/images/"+fileName;
System.out.println("Downloading File From: " + website);
URL url = new URL(website);
InputStream inputStream = url.openStream();
OutputStream outputStream = new FileOutputStream(fileName);
byte[] buffer = new byte[2048];
int length = 0;
while ((length = inputStream.read(buffer)) != -1)
{
System.out.println("Buffer Read of length: " + length);
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
}
}
To Upload Image:
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);
Result:
Ex.No:3 Applications using TCP Sockets
A. Echo Program:
Aim:
To write a program for implementing ECHO.
Algorithm:
1. Start a program by establishing connection between client and server.
2. Use datagram socket to establish connection
3. Use a datagram packet to get data from the client and pass to server.
4. using similar datagram packet in receive the data stream from client.
5. Repeat the steps 3 and 4 for chatting.
6. Stop the program after all chat communication.
Program:
ClientEcho.java
import java.net.*;
import java.util.*;
public class Clientecho
{
public static void main( String args[] ) throws Exception
{
InetAddress add = InetAddress.getByName("localhost");
DatagramSocket dsock = new DatagramSocket( );
String message1 = "This is client calling";
byte arr[] = message1.getBytes( );
DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
dsock.send(dpack); // send the packet
Date sendTime = new Date( ); // note the time of sending the message
dsock.receive(dpack); // receive the packet
String message2 = new String(dpack.getData( ));
Date receiveTime = new Date( ); // note the time of receiving the message
System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo
time for " + message2);
}
}
ServerEcho.java
import java.net.*;
import java.util.*;
public class ServerEcho
{
public static void main( String args[]) throws Exception
{
DatagramSocket dsock = new DatagramSocket(7);
byte arr1[] = new byte[150];
DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
while(true)
{
dsock.receive(dpack); byte arr2[] = dpack.getData();
int packSize = dpack.getLength();
String s2 = new String(arr2, 0, packSize);
System.out.println( new Date( ) + " " + dpack.getAddress( ) + " : " +
dpack.getPort( ) + " "+ s2);
dsock.send(dpack);
}
}
}
Result:
Result:
C.TCP File Transfer Program
Aim:
To implement file transfer using TCP from server to client in java.
Algorithm:
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 retrived 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:
Client:
import java.io.*;
import java.net.*;
import java.util.*;
public class ft2client
{
public static void main(String srgs[])throws IOException
{
Socket s=null;
BufferedReader get=null;
PrintWriter put=null;
try
{
s=new Socket("127.0.0.1",8081);
get=new BufferedReader(new InputStreamReader(s.getInputStream()));
put=new PrintWriter(s.getOutputStream(),true);
}
catch(Exception e)
{
System.exit(0);
}
String u,f;
System.out.println("Enter the file name to transfer from server:");
DataInputStream dis=new DataInputStream(System.in);
f=dis.readLine();
put.println(f);
File f1=new File(f);
FileOutputStream fs=new FileOutputStream(f1);
while((u=get.readLine())!=null)
{
byte jj[]=u.getBytes();
fs.write(jj);
}
fs.close();
System.out.println("File received");
s.close();
}
}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
public class ft2server
{
public static void main(String args[])throws IOException
{
ServerSocket ss=null;
try
{
ss=new ServerSocket(8081);
}
catch(IOException e)
{
System.out.println("couldn't listen");
System.exit(0);
}
Socket cs=null;
try
{
cs=ss.accept();
System.out.println("Connection established"+cs);
}
catch(Exception e)
{
System.out.println("Accept failed");
System.exit(1);
}
PrintWriter put=new PrintWriter(cs.getOutputStream(),true);
BufferedReader st=new BufferedReader(new
InputStreamReader(cs.getInputStream()));
String s=st.readLine();
System.out.println("The requested file is : "+s);
File f=new File(s);
if(f.exists())
{
BufferedReader d=new BufferedReader(new FileReader(s));
String line;
while((line=d.readLine())!=null)
{
put.write(line);
put.flush();
}
d.close();
System.out.println("File transfered");
cs.close();
ss.close();
}
}
}
Result:
Problem Description:
The purpose of DNS is to resolve the IP address for the given Domain Name. It is a
dependent protocol.DNS is simulated in this experiment by having a process which fetches the
domain name as an argument and resolves the IP address by using inet address class and local DNS
cache.
Algorithm:
Start the program
Define two character arrays with hostnames and IP addresses
Get the hostname from user
Match the hostname with array and get the index.
Display the corresponding IP Address.
End of the program
Program:
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class DNS
{
public static void main(String args[])
{
if (args.length != 1)
{
System.err.println("Print out DNS Record for an Internet Address");
System.err.println("USAGE: java DNS domainName|domainAddress");
System.exit(-1);
}
try
{
InetAddress inetAddress;
if (Character.isDigit(args[0].charAt(0)))
{
byte[] b = new byte[4];
String[] bytes = args[0].split("[.]");
for (int i = 0; i < bytes.length; i++)
{
b[i] = new Integer(bytes[i]).byteValue();
}
inetAddress = InetAddress.getByAddress(b);
}
else
{
inetAddress = InetAddress.getByName(args[0]);
}
System.out.println(inetAddress.getHostName() + "/" + inetAddress.getHostAddress());
InitialDirContext iDirC = new InitialDirContext();
Attributes attributes = iDirC.getAttributes("dns:/" + inetAddress.getHostName());
NamingEnumeration attributeEnumeration = attributes.getAll();
}
catch(Exception e){ }
}
Result:
if(x==0)
printf("\n Invalid address");
x=0;
break;
case 2:
printf("\n Enter the Ethernet address:");
scanf("%s",etadd);
for(i=0;i<20;i++)
{
if(strcmp(etadd,et[i])==0)
{
printf("\n Ip address is %s",ip[i]);
y=1;
}
}
if(y==0)
printf("\n Invalid address");
y=0;
break;
case 3:
exit(0);
}
}
}
Result: