Advanced Computer Networks Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

ADVANCED COMPUTER NETWORKS

LIST OF EXPERIMENTS:

1. Write a java program to design a TCP Client-Server application to transfer a file.

2. Write a java program to design UDP Client-Server application to transfer a file.

3. Write a java program to design a ARP protocol.

4. Write a java program to distance vector routing protocol.

5. Write a java program to Dijkstra’s shortest path routing protocol.

6. Write a Java program to develop a DNS client server to resolve the given Hostname.

7. Implement a simple TCP client-server where in a server acts as a time and date server.

8. Create a simple Chat Program


EX(1)

TCP CLIENT-SERVER APPLICATION TO TRANSFER A FILE

AIM:

To write a java program for file transfer using TCP Sockets.

PROCEDURE

Server

Step 1: Import java packages and create class file server.


Step 2: Create a new server socket and bind it to the port.
Step 3: Accept the client connection
Step 4: Get the file name and stored into the BufferedReader.
Step 5: Create a new object class file and realine.
Step 6: If file is exists then FileReader read the content until EOF is reached.
Step 7: Stop the program.

Client

Step 1: Import java packages and create class file server.


Step 2: Create a new server socket and bind it to the port.
Step 3: Now connection is established.
Step 4: The object of a BufferReader class is used for storing data content which has been
retrieved from socket object.
Step 5 Then close the connection.
Step 6: Stop the program.
Program File

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


{
public static void main(String[] args) throws Exception
{
//Initialize Sockets
ServerSocket ssock = new ServerSocket(5000);
Socket socket = ssock.accept();
//The InetAddress specification
InetAddress IA = InetAddress.getByName("localhost");

//Specify the file


File file = new File("e:\\Bookmarks.html");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
//Get socket's output stream
OutputStream os = socket.getOutputStream();
//Read File Contents into contents array
byte[] contents;
long fileLength = file.length();
long current = 0;
long start = System.nanoTime();
while(current!=fileLength){
int size = 10000;
if(fileLength - current >= size)
current += size;
else{
size = (int)(fileLength - current);
current = fileLength;
}
contents = new byte[size];
bis.read(contents, 0, size);
os.write(contents);
System.out.print("Sending file ... "+(current*100)/fileLength+"% complete!");
}
os.flush();
//File transfer done. Close the socket connection!
socket.close();
ssock.close();
System.out.println("File sent succesfully!");
}}

File Client
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;

public class FileClient {


public static void main(String[] args) throws Exception{
//Initialize socket
Socket socket = new Socket(InetAddress.getByName("localhost"), 5000);
byte[] contents = new byte[10000];
//Initialize the FileOutputStream to the output file's full path.
FileOutputStream fos = new FileOutputStream("e:\\Bookmarks1.html");
BufferedOutputStream bos = new BufferedOutputStream(fos);
InputStream is = socket.getInputStream();
//No of bytes read in one read() call
int bytesRead = 0;
while((bytesRead=is.read(contents))!=-1)
bos.write(contents, 0, bytesRead);
bos.flush();
socket.close();
System.out.println("File saved successfully!");
}
}
Output
server
E:\nwlab>java FileServer
Sending file ... 9% complete!
Sending file ... 19% complete!
Sending file ... 28% complete!
Sending file ... 38% complete!
Sending file ... 47% complete!
Sending file ... 57% complete!
Sending file ... 66% complete!
Sending file ... 76% complete!
Sending file ... 86% complete!
Sending file ... 95% complete!

Sending file ... 100% complete!


File sent successfully!

E:\nwlab>client
E:\nwlab>java FileClient
File saved successfully!

E:\nwlab>

RESULT

Thus the file transfer application using TCP is done and executed successfully.
EX(2)

UDP CLIENT-SERVER APPLICATION TO TRANSFER A FILE

AIM:

To write a java program for file transfer using UDP.


PROCEDURE:

1) Start the program.


2) Write program for client program.
3) Inside client package use objects of DatagramSocket class and DatagramPacket class.
4) In server program by creating object for FileInputStream transfer data from file to byte array.
5) Using object of DatagramPacket class send the byte array to the client system.
6) Run client program then run server program.
7) Get the output.
8) Stop the program.
PROGRAM:
/*CLIENT CLASS*/

import java.net.*;
import java.io.*;
public class client
{
public static void main(String args[])throws Exception
{
byte b[]=new byte[1024];
FileInputStream f=new FileInputStream("D:/miet.txt");
DatagramSocket dsoc=new DatagramSocket(2000);
int i=0;
while(f.available()!=0)
{
b[i]=(byte)f.read();
i++;
}
f.close();
dsoc.send(new DatagramPacket(b,i,InetAddress.getLocalHost(),1000));
}

/*SERVER CLASS*/

import java.net.*;
import java.io.*;
public class server
{
public static void main(String args[])throws IOException
{
byte b[]=new byte[3072];
DatagramSocket dsoc=new DatagramSocket(1000);
FileOutputStream f=new FileOutputStream("D:/mca.txt");
while(true)
{
DatagramPacket dp=new DatagramPacket(b,b.length);
dsoc.receive(dp);
System.out.println(new String(dp.getData(),0,dp.getLength()));

}
}
}
OUTPUT:
/*Client class*/
miet.txt
miet arts & science college
D:\java\EXNO-7>java client
/*server class*/
D:\java\EXNO-7>java server
miet arts & science college

RESULT

Thus the file transfer application using UDP is done and executed successfully.
EX(3)
DESIGNING OF ARP PROTOCOL

AIM
To write a java program to design ARP protocol.

PROCEDURE

CLIENT SIDE

1. Establish a connection between the Client and Server.


2. Create instance output stream writer
3. Get the IP Address to resolve its physical address.
4. Send the IPAddress to its output Stream.
5. Print the Physical Address received from the server.

SERVER SIDE

1. Accept the connection request by the client.


2. Get the IPaddress from its inputstream.
3. During runtime execute the processRuntime r=Runtime.getRuntime();
Process p=r.exec("arp -a "+ip);
4. Send the Physical Address to the client.
PROGRAM

ARP CLIENT

import java.io.*;
import java.net.*;

class ArpClient
{
public static void main(String args[])throws IOException
{
try
{
Socket ss=new Socket(InetAddress.getLocalHost(),1100);
PrintStream ps=new PrintStream(ss.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String ip;
System.out.println("Enter the IPADDRESS:");
ip=br.readLine();
ps.println(ip);
String str,data;
BufferedReader br2=new BufferedReader(new InputStreamReader(ss.getInputStream()));
System.out.println("ARP From Server::");
do
{
str=br2.readLine();
System.out.println(str);
}
while(!(str.equalsIgnoreCase("end")));
}
catch(IOException e)
{
System.out.println("Error"+e);

}}}

ARP SERVER

import java.io.*;
import java.net.*;

class ArpServer
{
public static void main(String args[])throws IOException
{
try
{
ServerSocket ss=new ServerSocket(1100);
Socket s=ss.accept();
PrintStream ps=new PrintStream(s.getOutputStream());
BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream()));
String ip;
ip=br1.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("arp -a "+ip);
BufferedReader br2=new BufferedReader(new InputStreamReader(p.getInputStream()));
String str;
while((str=br2.readLine())!=null)
{
ps.println(str);
}}
catch(IOException e)
{
System.out.println("Error"+e);
}}}
OUTPUT

C:\NetworkingPrograms>java ArpServer
C:\Networking Programs>java ArpClient
Enter the IPADDRESS:
192.168.11.58
ARP From Server::
Interface: 192.168.11.57 on Interface 0x1000003
Internet Address Physical Address Type
192.168.11.58 00-14-85-67-11-84 dynamic

Result :

Thus the program is designed for ARP protocol successfully.


EX(6)
DNS CLIENT SERVER TO RESOLVE A GIVEN HOST NAME

AIM:
To implement a DNS server and client in java using UDP sockets.

ALGORITHM:
Server
1. Create an array of hosts and its ip address in another array
2. Create a datagram socket and bind it to a port
3. Create a datagram packet to receive client request
4. Read the domain name from client to be resolved
5. Lookup the host array for the domain name
6. If found then retrieve corresponding address
7. Create a datagram packet and send ip address to client
8. Repeat steps 3-7 to resolve further requests from clients
9. Close the server socket
10. Stop

CLIENT
1. Create a datagram socket
2. Get domain name from user
3. Create a datagram packet and send domain name to the server
4. Create a datagram packet to receive server message5. Read server's response
6. If ip address then display it else display "Domain does not exist"
7. Close the client socket
8. Stop
PROGRAM:
// UDP DNS Server -- udpdnsserver.java

import java.io.*;
import java.net.*;
public class udpdnsserver
{
private static int indexOf(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"};
String[] ip = {"68.180.206.184", "209.85.148.19","80.168.92.140","69.63.189.16"};
System.out.println("Press Ctrl + C to Quit");
while (true)
{
DatagramSocket serversocket=new DatagramSocket(1362);
byte[] senddata = new byte[1021];
byte[] receivedata = new byte[1021];
DatagramPacket recvpack = new DatagramPacket(receivedata,
receivedata.length);
serversocket.receive(recvpack);
String sen = new String(recvpack.getData());
InetAddress ipaddress = recvpack.getAddress();
int port = recvpack.getPort();
String capsent;
System.out.println("Request for host " + sen);
if(indexOf (hosts, sen) != -1)
capsent = ip[indexOf (hosts, sen)];
else
capsent = "Host Not Found";
senddata = capsent.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,
senddata.length,ipaddress,port);
serversocket.send(pack);
serversocket.close();
} }}
//UDP DNS Client -- udpdnsclient.java
import java.io.*;
import java.net.*;
public class udpdnsclient
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientsocket = new DatagramSocket();
InetAddress ipaddress;
if (args.length == 0)
ipaddress = InetAddress.getLocalHost();
else
ipaddress = InetAddress.getByName(args[0]);
byte[] senddata = new byte[1024];
byte[] receivedata = new byte[1024];
int portaddr = 1362;
System.out.print("Enter the hostname : ");
String sentence = br.readLine();
Senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length,
ipaddress,portaddr);
clientsocket.send(pack);
DatagramPacket recvpack =new DatagramPacket(receivedata,receivedata.length);
clientsocket.receive(recvpack);
String modified = new String(recvpack.getData());
System.out.println("IP Address: " + modified);
clientsocket.close();
}}
OUTPUT:
SERVER

$ javac udpdnsserver.java
$ java udpdnsserver
Press Ctrl + C to Quit
Request for host yahoo.com
Request for host cricinfo.com
Request for host youtube.com
CLIENT:
$ javac udpdnsclient.java
$ java udpdnsclient
Enter the hostname : yahoo.com
IP Address: 68.180.206.184
$ java udpdnsclient
Enter the hostname : cricinfo.com
IP Address: 80.168.92.140
$ java udpdnsclient
Enter the hostname : youtube.com
IP Address: Host Not Found

Result
Hence the program to develop a client that contacts a given DNS server to resolve a given
host name is executed successfully.
EX(7)

TCP SOCKETS DATE AND TIME SERVER

AIM:
To implement date and time display from client to server using TCP Sockets

DESCRIPTION:
TCP Server gets the system date and time and opens the server socket to
read the client details. Client sends its address to the server. Then client receives
the date and time from server to display. TCP socket server client connection is
opened for communication. After the date time is displayed the server client
connection is closed with its respective streams to be closed.

ALGORITHM:
Server
1. Create a server socket and bind it to port.
2. Listen for new connection and when a connection arrives, accept it.
3. Send server‟s date and time to the client.
4. Read client‟s IP address sent by the client.
5. Display the client details.
6. Repeat steps 2-5 until the server is terminated.
7. Close all streams.
8. Close the server socket.
9. 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.
3. Send its address to the server.
4. Display the date & time sent by the server.
5. Close the input and output streams.
6. Close the client socket.
7. Stop.
PROGRAM:

//TCP Date Server--tcpdateserver.java

import java.net.*;
import java.io.*;
import java.util.*;
class tcpdateserver
{
public static void main(String arg[])
{
ServerSocket ss = null;
Socket cs;
PrintStream ps;
BufferedReader dis;
String inet;
try
{
ss = new ServerSocket(4444);
System.out.println("Press Ctrl+C to quit");
while(true)
{
cs = ss.accept();
ps = new PrintStream(cs.getOutputStream());
Date d = new Date();
ps.println(d);
dis = new BufferedReader(new
InputStreamReader(cs.getInputStream()));
inet = dis.readLine();
System.out.println("Client System/IP address is :"+ inet);
ps.close();
dis.close();
}
}
catch(IOException e)
{
System.out.println("The exception is :" + e);
}}}

// TCP Date Client--tcpdateclient.java

import java.net.*;
import java.io.*;
class tcpdateclient
{
public static void main (String args[])
{
Socket soc;
BufferedReader dis;
String sdate;
PrintStream ps;
try
{
InetAddress ia = InetAddress.getLocalHost();
if (args.length == 0)
soc = new Socket(InetAddress.getLocalHost(),4444);
else
soc = new Socket(InetAddress.getByName(args[0]),4444);
dis = new BufferedReader(new
InputStreamReader(soc.getInputStream()));
sdate=dis.readLine();
System.out.println("The date/time on server is : " +sdate);
ps = new PrintStream(soc.getOutputStream());
ps.println(ia);
ps.close();
catch(IOException e)
{
System.out.println("THE EXCEPTION is :" + e);
}}}
OUTPUT
Server:
$ javac tcpdateserver.java
$ java tcpdateserver
Press Ctrl+C to quit
Client System/IP address is : localhost.localdomain/127.0.0.1
Client System/IP address is : localhost.localdomain/127.0.0.1
Client:
$ javac tcpdateclient.java
$ java tcpdateclient
The date/time on server is: Wed Jan 04 07:12:23 GMT 2023
Every time when a client connects to the server, server‟s date/time will be
returned to the client for synchronization.

RESULT:
Thus the program for implementing to display date and time from client
to server usingTCP Sockets was executed successfully and output verified using
various samples.
EX (8)
CHAT PROGRAM

AIM
To write a client-server application for chat using TCP

ALGORITHM

CLIENT
1. Start the program
2. Include necessary package in java
3. To create a socket in client to server.
4. The client establishes a connection to the server.
5. The client accept the connection and to send the data from client to server.
6. The client communicates the server to send the end of the message
7. Stop the program.

SERVER
1. Start the program
2. Include necessary package in java
3. To create a socket in server to client
4. The server establishes a connection to the client.
5. The server accept the connection and to send the data from server to client and
6. vice versa
7. The server communicate the client to send the end of the message.
8. Stop the program.
PROGRAM

TCPserver1.java

import
java.net.*;
import
java.io.*;

public class TCPserver1


{
public static void main(String arg[])
{
ServerSocket s=null;
String line;
DataInputStream is=null,is1=null;
PrintStream os=null;
Socket c=null;
try
{
s=new ServerSocket(9999);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
c=s.accept();
is=new DataInputStream(c.getInputStream());
is1=new DataInputStream(System.in);
os=new PrintStream(c.getOutputStream());
do
{
line=is.readLine();
System.out.println("Client:"+line);
System.out.println("Server:");
line=is1.readLine();
os.println(line);
}
while(line.equalsIgnoreCase("quit")==false);
is.close();
os.close();
}
catch(IOException e)
{
System.out.println(e);
} }}
TCPclient1.java

import
java.net.*;
import
java.io.*;

public class TCPclient1


{
public static void main(String arg[])
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
{
c=new Socket("10.0.200.36",9999);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());
do
{
System.out.println("Client:");
line=is.readLine();
os.println(line);
System.out.println("Server:" + is1.readLine());
}

while(line.equalsIgnoreCase("quit")==false);
is1.close();
os.close();
}
catch(IOException e)
{
System.out.println("Socket Closed!Message Passing is over");
}}
OUTPUT :

SERVER

C:\Program Files\Java\jdk1.5.0\bin>javac TCPserver1.java

C:\Program Files\Java\jdk1.5.0\bin>java
TCPserver1

Client: Hai Server


Server:Hai Client
Client: How are
youServer:Fine
Client:
quit
Server:q
uit

CLIENT

C:\Program Files\Java\jdk1.5.0\bin>javac TCPclient1.java

C:\Program Files\Java\jdk1.5.0\bin>java TCPclient1

Client:Hai Server
Server: Hai Client
Client:How are you
Server: Fine
Client:quit
Server: quit

RESULT

Thus the above program a client-server application for chat using TCP / IP was
executed and successfully.

You might also like