0% found this document useful (0 votes)
109 views15 pages

Cs8581 Networks Laboratory List of Experiments

Here are the steps to implement a TCP file transfer program from server to client in Java: Server: 1. Create a ServerSocket to listen for connection requests on a specified port. 2. Accept the connection request from client using accept() method which returns a Socket. 3. Get the input and output streams from the socket. 4. Read the file to be transferred using FileInputStream and write the content to output stream. 5. Close the input and output streams. Client: 1. Create a Socket to connect to server on the specified port. 2. Get the input and output streams from the socket. 3. Create a FileOutputStream to write
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views15 pages

Cs8581 Networks Laboratory List of Experiments

Here are the steps to implement a TCP file transfer program from server to client in Java: Server: 1. Create a ServerSocket to listen for connection requests on a specified port. 2. Accept the connection request from client using accept() method which returns a Socket. 3. Get the input and output streams from the socket. 4. Read the file to be transferred using FileInputStream and write the content to output stream. 5. Close the input and output streams. Client: 1. Create a Socket to connect to server on the specified port. 2. Get the input and output streams from the socket. 3. Create a FileOutputStream to write
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

CS8581 NETWORKS LABORATORY

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

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

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:

B. TCP Chat Program


Aim: To write a program for implementing TCP Chat.
Algorithm:
Server:
1. Establish the connection of socket.
2. Assign the local Protocol address 8080 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.
6. Write the data into socket.
7. Close the socket.
Client:
1. Open the socket.
2. Get the host name and port number 8080 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 / Buffer.
8. Close the socket
Program:
Server:
import java.io.*;
import java.net.*;
class tcpser
{
static final int port = 8080;
public static void main(String args[ ])
{
try
{
ServerSocket ss=new ServerSocket(port);
System.out.println("server started"+ss);
Socket s=ss.accept();
System.out.println("Connection accepted"+s);
String str="hai";
while(!str.equals("exit"))
{
BufferedReader in=new BufferedReader(new BufferedReader(new InputStreamReader
(s.getInputStream())));
str=in.readLine();
System.out.println(str);
if(str.equals("exit"))
return;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(new
BufferedWriter(newOutputStreamWriter(s.getOutputStream())),true);
out.println(br.readLine());
}
System.out.println("Closing");
s.close();
}
catch(Exception e)
{}
}
}
Client:
import java.io.*;
import java.net.*;
class tcpcli
{
static final int port = 8080;
public static void main(String args[])
{
try
{
InetAddress addr=InetAddress.getByName("localhost");
Socket s=new Socket(addr,port);
while(true)
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(s.getOutputStream())),true);
out.println(in.readLine());
BufferedReader brr=new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(brr.readLine());
}
}
catch(Exception e)
{}
}}

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:

Ex.No:4 Simulation of DNS using UDP sockets


Aim:
To write a java program for Dns application program

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:

Ex.No:5 Write a Code Simulating ARP /RARP Protocols


Aim:
To write a program to implement Address Resolution Protocol(ARP) and Reverse Address
Resolution Protocol(RARP) by using C.
Algorithm:
1. Start the program.
2. Initialize the ARP and RARP address in array.
3. For ARP read the input of IP address by using strcmp function. Find the
Ethernet address corresponding to the given IP address and give the
corresponding Ethernet address for given IP address, else print the given IP
address is invalid.
4. For RARP read the input of Ethernet address by using strcmp function. Find the
IPaddress corresponding to the given Ethernet address and give the
corresponding IP address for given Ethernet address, else print the given
Ethernet address is invalid.
5. End the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
char ip[20][20]={"192.43.24.67","192.56.72.5","145.57.32.7"};
char et[20][20]={"00.00.A8.00","14.E3.56.9","A6.45.7.D3"};
char ipadd[20],etadd[20];
int i,op;
int x=0;int y=0;
clrscr();
while(1)
{
printf("\n1.ARP\n2.RARP\n3.EXIT");
printf("\n Enter your choice:");
scanf(" %d",&op);
switch(op)
{
case 1:
printf("Enter the IP address:");
scanf("%s",ipadd);
for(i=0;i<20;i++)
{
if(strcmp(ipadd,ip[i])==0)
{
printf("\n Ethernet address is %s",et[i]);
x=1;
}
}

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:

You might also like