Date post: | 27-Apr-2015 |
Category: |
Documents |
Upload: | sunu-abilash |
View: | 378 times |
Download: | 2 times |
COMPUTER NETWORKING LAB
EXPT. NO. 1 INET ADDRESS DEMO DATE: 27.07.2011
AIM
To familiarize the InetAddress methods in java.
ALGORITHM
Step 1: Start
Step 2: Get the IP Address using method InetAddress.getLocalHost().
Step 3: Get the name of local host using getByName method of localhost and display it.
Step 4: Get the name of domain using getByName method with parameter as a domain and display.
Step 5: Get an array of InetAddress of a particular domain using getAllByName and display.
Step 6: Stop.
PROGRAM
import java.io.*;import java.net.*;class Inetadd{ public static void main(String args[]) throws Exception { try {
System.out.println("Get local host method : " +InetAddress.getLocalHost()); System.out.println("Get by name method: " + InetAddress.getByName("localhost")); System.out.println(InetAddress.getByName("www.facebook.com")); InetAddress n[]=InetAddress.getAllByName("www.google.com"); for(int i=0;i<=n.length;i++) System.out.println(n[i]);
} catch(Exception e) {
System.out.println(e); } }}
OUTPUT
COMPUTER NETWORKING LAB
RESULT
The program was successfully executed and familiarized methods of Inet Address.
EXPT. NO. 2 URL AND URL CONNECTION DATE: 02.08.2011
AIM
COMPUTER NETWORKING LAB
To design and implement URL and URL Connection class.
ALGORITHM
Step 1: Start
Step 2: Create a URL object, specifying the path of file.
Step 3: Create a URL Connection object using the openConnection method of URL object.
Step 4: Using the getPort method, display the port number.
Step 5: Host name is displayed using the getHost method of url object.
Step 6: Using the getPath of URL object, display the path.
Step 7: Using the getFile method of URL object, display the file.
Step 8: Display the file creation date using getDate method of URL Connection object.
Step 9: Content type of the file is displayed using the getContentType of URL Connection object.
Step 10: Expiry date is displayed using the getExpiration method of URL Connection object.
Step 11: Display the length of content using getContentLength method of URL Connection object.
Step 12: Read the data of file using the getInputStream method.
Step 13: Stop.
PROGRAM
import java.io.*;import java.net.*;import java.util.Date;
class urldemo{ public static void main(String args[]) throws Exception { try { URL nurl=new URL("file:/D:/Sample.txt"); URLConnection nurlco=nurl.openConnection();
System.out.println("Port:" +nurl.getPort());System.out.println("Path:" +nurl.getPath());System.out.println("File:" +nurl.getFile());
Date dt=new Date(nurlco.getDate());System.out.println( "Date:" + dt);System.out.println("Content Type:"+ nurlco.getContentType());
System.out.println("Expires:" +new Date(nurlco.getExpiration()));
COMPUTER NETWORKING LAB
System.out.println("Date Modified:" +new Date(nurlco.getLastModified())); System.out.println("Content Length:" +nurlco.getContentLength());
System.out.println("\nContent of file"); System.out.println("- - - - - - - - - - -"); InputStream is=nurlco.getInputStream();
int s; while((s=is.read())!=-1)
System.out.print((char) s); } catch(Exception e) {
System.out.println(e); } }}
OUTPUT
COMPUTER NETWORKING LAB
RESULT
The program was successfully executed and URL & URL Connection class was done
successfully.
EXPT. NO. 3 UDP ECHO SERVER DATE: 03.08.2011
COMPUTER NETWORKING LAB
AIM
To design and implement echo UDP server using java.
ALGORITHM
Server sideStep 1: Start
Step 2: Create a datagram socket.
Step 3: Create a datagram packet send and receive.
Step 4: Read the data from client through the packet receive.
Step 5: Display the data from the client along with the time.
Step 6: Read the data to be send from keyboard.
Step 7: Send the data to client through the datagram packet, display the time it was send.
Step 8: If the send data is “end”, close the server.
Step 9: Stop.
Client sideStep 1: Start
Step 2: Create datagram socket with port no as parameters.
Step 3: Read data from keyboard.
Step 4: Convert the text to bytes, using getBytes() and store in byte array.
Step 5: Create a datagram packet to send the client data to the server using servers IP address and port.
Step 6: Send the packet to the server along with the time at which it was send.
Step 7: Read packet from server side.
Step 8: Convert datagram packet to string.
Step 9: Display string along with the time at which it was received.
Step 10: Stop.
PROGRAM
Server sideimport java.io.*;import java.net.*;
COMPUTER NETWORKING LAB
class echoudpserver{ public static void main(String args[]) throws Exception { DatagramSocket serverSocket=null; try { serverSocket = new DatagramSocket(9876); while(true) { byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; int port; InetAddress ip; DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String( receivePacket.getData()); if(sentence.equals("end")) break; port=receivePacket.getPort(); ip=receivePacket.getAddress(); System.out.println("Msg from client is:"+ sentence.trim() +" PortNo:"+ port + " IPAddress:" + ip); sendData = sentence.getBytes(); DatagramPacket sendPacket =new DatagramPacket(sendData, sendData.length, ip, port); serverSocket.send(sendPacket); } }
catch(Exception e) { System.out.println(e); } finally { serverSocket.close(); } }}
Client sideimport java.io.*;import java.net.*;import java.util.*;
class echoudpclient
COMPUTER NETWORKING LAB
{ public static void main(String args[]) throws Exception { DatagramSocket clientSocket=null; try { BufferedReader inFromUser =new BufferedReader(new InputStreamReader(System.in)); clientSocket = new DatagramSocket(); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; while(true) { InetAddress IPAddress = InetAddress.getByName("localhost"); System.out.println("Enter the msg "); String sentence = inFromUser.readLine(); if(sentence.equals("end")) break; sendData = sentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); Date senddate=new Date(); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); Date receivedate=new Date(); String nSentence = new String(receivePacket.getData()); System.out.println("Msg from server:" + nSentence.trim() + " Echo Time " + (receivedate.getTime() - senddate.getTime()) + "ms"); }//while }//try catch(Exception e) { System.out.println(e); } finally { clientSocket.close(); } }}
OUTPUT
Server side
COMPUTER NETWORKING LAB
Client side
RESULT
The program was successfully executed and the echo UDP server was done successfully.
EXPT. NO. 4 UDP CLIENT SERVER COMMUNICATION DATE: 05.08.2011
AIM
To design and implement a client-server program to perform bi-directional communication using UDP.
ALGORITHM
COMPUTER NETWORKING LAB
Server sideStep 1: Start
Step 2: Create a datagram socket.
Step 3: Read the input data using DataInputStream object.
Step 4: Create a byte array.
Step 5: Create a datagram packet with byte array.
Step 6: Send the datagram packet to the server using socket.
Step 7: Create a new datagram packet with byte array to receive the message from the server.
Step 8: Receive the packet from server using socket.
Step 9: Retrieve the message from received packet.
Step 10: Output the message.
Step 11: Close the socket.
Step 12: Stop.
Client sideStep 1: Start
Step 2: Create a datagram socket.
Step 3: Create a datagram with a byte array to receive data from client.
Step 4: Receive the message from the client using socket.
Step 5: Display the client message.
Step 6: Read the input data, using data input stream object.
Step 7: Create a byte array.
Step 8: Create a data gram packet with byte array.
Step 9: Send the datagram packet to the client using socket.
Step 10: Close the socket.
Step 11: Stop
PROGRAM
Server sideimport java.io.*;import java.net.*;import java.util.*;
COMPUTER NETWORKING LAB
class bidirudpserver { public static void main(String args[]) throws Exception { DatagramSocket serverSocket=null; try { BufferedReader msgfromserv =new BufferedReader(new InputStreamReader(System.in)); serverSocket = new DatagramSocket(9876); while(true) { byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; int port; InetAddress ip; DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String( receivePacket.getData()); port=receivePacket.getPort(); ip=receivePacket.getAddress(); System.out.println("Client:"+ sentence.trim()); System.out.println("Server:"); String servsen=msgfromserv.readLine(); sendData = servsen.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ip, port); serverSocket.send(sendPacket); if(servsen.equals("bye")) System.exit(0); } }//try catch(Exception e) { System.out.println(e); } finally { serverSocket.close(); } } }
Client sideimport java.io.*;import java.net.*;
COMPUTER NETWORKING LAB
import java.util.*;
class bidirudpclient{ public static void main(String args[]) throws Exception { DatagramSocket clientSocket=null; try { BufferedReader inFromClient =new BufferedReader(new InputStreamReader(System.in)); clientSocket = new DatagramSocket(); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; while(true) { InetAddress IPAddress = InetAddress.getByName("localhost"); System.out.println("Client: "); String sentence = inFromClient.readLine(); sendData = sentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); if(sentence.equals("bye")) break; DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String nSentence = new String(receivePacket.getData()); System.out.println("Server:" + nSentence.trim() ); if(nSentence.equals("bye")) System.exit(0); } }//try catch(Exception e) { System.out.println(e); } finally { clientSocket.close(); } }}
OUTPUT
Server side
COMPUTER NETWORKING LAB
Client side
RESULT
The program was successfully executed and the bidirectional udp communication was done
successfully.
COMPUTER NETWORKING LAB
EXPT. NO. 5 TCP DATE SERVER DATE: 09.08.2011
AIM
To design and implement date server using TCP in java.
ALGORITHM
Server sideStep 1: Start
Step 2: Create the server socket and bind it to the port.
Step 3: Listen for client response.
COMPUTER NETWORKING LAB
Step 4: Accept the connection.
Step 5: Using print writer output the data to the channel.
Step 6: Use sleep function to get delay in time intervals.
Step 7: Stop.
Client sideStep 1: Start
Step 2: Create Socket with server’s IP address and port number.
Step 3: Get the data from the server using the getInputStream of socket object.
Step 4: Output the date and time obtained from server side.
Step 5: Stop.
PROGRAM
Server sideimport java.net.*;import java.io.*;import java.util.*;
class dateserver { public static void main(String args[]) throws Exception { try { ServerSocket s=new ServerSocket(5217); System.out.println("Waiting For Connection ..."); Socket soc=s.accept(); PrintWriter pw=new PrintWriter(soc.getOutputStream(),true); for(int i=0;i<=10;i++) { Date d=new Date(); pw.println(d); Thread.sleep(1000); } }//try catch(Exception e) { System.out.println(e); } }
COMPUTER NETWORKING LAB
}
Client sideimport java.io.*;import java.net.*;import java.util.*;
class dateclient { public static void main(String args[]) throws Exception { try { Socket soc=new Socket(InetAddress.getLocalHost(),5217); BufferedReader in=new BufferedReader( new InputStreamReader(soc.getInputStream())); for(int i=0;i<=10;i++) { String str=in.readLine(); System.out.println(str); } } catch(Exception e) { System.out.println(e); } }}
OUTPUT
Server side
Client side
COMPUTER NETWORKING LAB
RESULT
The program was successfully executed and the data server was implemented successfully.
EXPT. NO. 6 TCP ECHO SERVER DATE: 10.08.2011
AIM
To design and implement echo TCP server to display echo messages at client side.
ALGORITHM
Server sideStep 1: Start
Step 2: Create server socket and bind it to the port.
Step 3: Listen for client response.
Step 4: Accept the connection.
Step 5: Get the message from client using getInputStream of socket object.
Step 6: Display the message from client and using print writer, send the same message to the client.
COMPUTER NETWORKING LAB
Step 7: Stop
Client sideStep 1: Start
Step 2: Create client socket.
Step 3: Get the data from the keyboard using input stream reader.
Step 4: Using print writer output the message to server.
Step 5: Receive the echo message from server using the getInputStream of socket object.
Step 6: Display the echo message.
Step 7: Stop
PROGRAM
Server sideimport java.net.*;import java.io.*;import java.util.*;
class echotcpserver { public static void main(String args[]) throws Exception { try { ServerSocket s=new ServerSocket(5217); System.out.println("Connecting with client........."); Socket soc=s.accept(); while(true) { BufferedReader brin=new BufferedReader(new InputStreamReader(soc.getInputStream())); String smsg=brin.readLine(); System.out.println("Message from client:" + smsg); if(smsg.equals("end")) break; PrintWriter pw=new PrintWriter(soc.getOutputStream(),true); pw.println(smsg); } } catch(Exception e) { System.out.println(e);
COMPUTER NETWORKING LAB
} } }
Client sideimport java.io.*;import java.net.*;import java.util.*;
class echotcpclient { public static void main(String args[]) throws Exception { try { Socket soc=new Socket(InetAddress.getLocalHost(),5217); while(true) { System.out.println("Enter the message to server"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String cmsg=br.readLine(); PrintWriter pw=new PrintWriter(soc.getOutputStream(),true); pw.println(cmsg); if(cmsg.equals("end")) break; BufferedReader in=new BufferedReader( new InputStreamReader(soc.getInputStream())); String str=in.readLine(); System.out.println("Echo Message:" +str); } } catch(Exception e) { System.out.println(e); } } }OUTPUT
Server side
COMPUTER NETWORKING LAB
Client side
RESULT
The program was successfully executed and the echo message was successfully obtained at the
client side.
COMPUTER NETWORKING LAB
EXPT. NO. 7TCP TALK SERVER
TO FIND PRIME NUMBERSDATE: 12.08.2011
AIM
To find the prime numbers at server side below a given limit obtained from client side.
ALGORITHM
Server sideStep 1: Start
Step 2: Create server socket and bind it to the port.
Step 3: Listen for client response.
Step 4: Accept the connection.
Step 5: Get the limit from client.
COMPUTER NETWORKING LAB
Step 6: Find all prime numbers below the given limit.
Step 7: Send the prime numbers to the client using print writer.
Step 8: Stop
Client sideStep 1: Start
Step 2: Create client socket using server’s id and port number.
Step 3: Establish connection with server.
Step 4: Send the limit to server using print writer.
Step 5: Get the prime number from server using the getInputStream of socket object.
Step 6: Display the prime numbers.
Step 7: Stop
PROGRAM
Server sideimport java.net.*;import java.io.*;import java.util.*;
class primtcpserv { public static void main(String args[]) throws Exception { try { ServerSocket s=new ServerSocket(5217); System.out.println("Connecting with client........."); Socket soc=s.accept(); PrintWriter pw; int i,j,n,prime; while(true) { BufferedReader brin=new BufferedReader(new InputStreamReader(soc.getInputStream())); String smsg=brin.readLine(); if(smsg.equals("end")) break; System.out.println("Finding prime numbers below " + smsg); n=Integer.parseInt(smsg); pw=new PrintWriter(soc.getOutputStream(),true);
COMPUTER NETWORKING LAB
for(i=2;i<n;i++) { prime=1; for(j=2;j<i;j++) if(i%j==0) { prime=0; break; } if(prime==1) { pw.println(i); } }//for pw.println("end"); }//while } catch(Exception e) { System.out.println(e); } }}
Client sideimport java.io.*;import java.net.*;import java.util.*;
class primtcpcli { public static void main(String args[]) throws Exception { try { Socket soc=new Socket(InetAddress.getLocalHost(),5217); while(true) { System.out.println("Enter the limit"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String cmsg=br.readLine(); PrintWriter pw=new PrintWriter(soc.getOutputStream(),true); pw.println(cmsg); if(cmsg.equals("end"))
COMPUTER NETWORKING LAB
break; BufferedReader in=new BufferedReader( new InputStreamReader(soc.getInputStream())); String str; System.out.println("Prime Numbers below " +cmsg +" are:"); do { str=in.readLine(); if(str.equals("end")) break; System.out.println(str); } while(str!="end"); }//while } catch(Exception e) { System.out.println(e); } }}
OUTPUT
Server side
Client side
COMPUTER NETWORKING LAB
RESULT
The program was successfully executed and the prime numbers below the given limit was found
successfully.
EXPT. NO. 8 TCP BIDIRECTIONAL CHAT DATE: 16.08.2011
AIM
To design and implement a client-server program to perform bidirectional communication using socket
programming in java.
ALGORITHM
Server sideStep 1: Start
Step 2: Create Server created which implements Runnable.
Step 3: Create server socket and bind it to the port.
Step 4: Listen for client response.
COMPUTER NETWORKING LAB
Step 5: Accept the connection.
Step 6: Create two threads one for writing data to channel and another to accept data from channel.
Step 7: If current thread is t1, the data is written to the channel, otherwise data accepted from channel.
Step 8: Stop
Client sideStep 1: Start
Step 2: Create client socket.
Step 3: Create client class which implements runnable.
Step 4: Establish connection with server.
Step 5: Create two threads one for channel output and another to accept data from channel.
Step 6: Stop
PROGRAM
Server sideimport java.net.*;import java.io.*;import java.util.*;import java.lang.*;public class BitcpServer implements Runnable{ Thread t1=null,t2=null; ServerSocket s=null; Socket soc=null; String smsg,str2; BufferedReader brin,brout; PrintWriter pw; //Constructor public BitcpServer() { try { s=new ServerSocket(5217); System.out.println("Connecting with client........."); soc=s.accept(); } catch(Exception e) { System.out.println(e);
COMPUTER NETWORKING LAB
} } //Implementation public void run() { try { if(Thread.currentThread()==t1) { brin=new BufferedReader(new InputStreamReader(soc.getInputStream())); do { smsg=brin.readLine(); System.out.println("Client says:" + smsg); } while(!smsg.equals("bye")); }//if else { brout=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(soc.getOutputStream(),true); do { str2=brout.readLine(); pw.println(str2); } while(!str2.equals("bye")); if(str2.equals("bye")) System.exit(0); }//else }//try catch(Exception e) { System.out.println(e); } } public static void main(String args[]) throws Exception { try { BitcpServer s1=new BitcpServer(); s1.t1=new Thread(s1); s1.t2=new Thread(s1); s1.t1.start(); s1.t2.start();
COMPUTER NETWORKING LAB
} catch(Exception e) { System.out.println(e); } }//main}
Client sideimport java.io.*;import java.net.*;import java.util.*;
public class BitcpClient implements Runnable{ Socket soc=null; BufferedReader br,in; String cmsg,str; PrintWriter pw; Thread t1=null,t2=null; //Constructor public BitcpClient() { try { soc=new Socket(InetAddress.getLocalHost(),5217); } catch(Exception e) { System.out.println(e); } } //Implementation public void run() { try { br=new BufferedReader(new InputStreamReader(System.in)); if(Thread.currentThread()==t1) { do { cmsg=br.readLine(); pw=new PrintWriter(soc.getOutputStream(),true);
COMPUTER NETWORKING LAB
pw.println(cmsg); } while(!cmsg.equals("bye")); if(cmsg.equals("bye")) System.exit(0); } else { in=new BufferedReader( new InputStreamReader(soc.getInputStream())); do { str=in.readLine();
System.out.println("Server says:" +str); } while(!str.equals("bye")); }//else }//try catch(Exception e) { System.out.println(e); } } public static void main(String args[]) throws Exception { try { BitcpClient c1=new BitcpClient(); c1.t1=new Thread(c1); c1.t2=new Thread(c1); c1.t1.start(); c1.t2.start(); } catch(Exception e) { System.out.println(e); } }//main}//BitcpClient
OUTPUT
Server side
COMPUTER NETWORKING LAB
Client side
RESULT
The program was successfully executed and the data transfer from client to server was done
successfully using TCP.
COMPUTER NETWORKING LAB
EXPT. NO. 9 BROADCASTING DATA DATE: 17.08.2011
AIM
To design and implement client- server program to perform broadcasting using TCP in java.
ALGORITHM
Server sideStep 1: Start
Step 2: Create server class which implements runnable.
Step 3: Create a socket array and bind it to the port.
Step 4: Listen for client’s response.
Step 5: Accept the connection.
Step 6: Create two threads ,one for accepting connection and another for sending server message to
channel.
Step 7: Close the socket.
Step 8: Stop
COMPUTER NETWORKING LAB
Client sideStep 1: Start
Step 2: Create socket using server’s id and port number.
Step 3: Get the message from server using getInputStream of socket object.
Step 4: Display the message.
Step 5: Stop.
PROGRAM
Server sideimport java.net.*;import java.io.*;import java.util.*;import java.lang.*;
public class brodserver implements Runnable { Thread t1=null,t2=null; ServerSocket s=null; String str2; BufferedReader brin,brout; PrintWriter pw; Socket soc[]=new Socket[5]; int i=1; public brodserver() { try { s=new ServerSocket(5217); } catch(Exception e) { System.out.println(e);} } public void run() { try { if(Thread.currentThread()==t1) { for(;;)
COMPUTER NETWORKING LAB
{ soc[i]=s.accept(); i++; } }//if else { brout=new BufferedReader(new InputStreamReader(System.in)); do { str2=brout.readLine(); for(int k=1;k<i;k++) { pw=new PrintWriter(soc[k].getOutputStream(),true); pw.println(str2); } }while(!str2.equals("quit")); if(str2.equals("quit")) System.exit(0); } } catch(Exception e) { System.out.println(e); } } public static void main(String args[]) throws Exception { try { brodserver s1=new brodserver(); s1.t1=new Thread(s1); s1.t2=new Thread(s1); s1.t1.start(); s1.t2.start(); } catch(Exception e) { System.out.println(e); } }//main}
Client sideimport java.io.*;
COMPUTER NETWORKING LAB
import java.net.*;import java.util.*;
public class brodclient { public static void main(String args[]) throws Exception { Socket soc=null; BufferedReader in; String str; PrintWriter pw; try { soc=new Socket(InetAddress.getLocalHost(),5217); in=new BufferedReader( new InputStreamReader(soc.getInputStream())); do { str=in.readLine(); System.out.println("Server:" +str); } while(!str.equals("quit")); } catch(IOException e) { System.out.println(e); } }}OUTPUT
Server side
Client 1
COMPUTER NETWORKING LAB
Client 2
Client 3
RESULT
The program was successfully executed and broadcasting was implemented successfully.
COMPUTER NETWORKING LAB
EXPT. NO. 10 PUBLIC CHAT USING GUI DATE: 23.08.2011
AIM
To design and implement client- server program to perform public chat using TCP.
ALGORITHM
Server sideStep 1: Start
Step 2: Create server socket and bind it to the port.
Step 3: Listen for client’s response.
Step 4: Accept the connection.
Step 5: Create Thread array accepting different clients.
Step 6: Get the message from client.
Step 7: Display the client message to all other connected client’s.
Step 8: Stop
Client sideStep 1: Start
Step 2: Create socket using server’s id and port number.
COMPUTER NETWORKING LAB
Step 3: Get the data from keyboard, to be send to server.
Step 4: Write the message into the channel.
Step 5: Display the message send by other client through the server.
Step 6: Stop.
PROGRAM
Server sideimport java.net.*;import java.io.*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;
class Listener implements Runnable{ BufferedReader in; int cid; String sms; Listener(BufferedReader in, int id) { this.in=in; cid=id; } public void run() { try { while(true) { sms=in.readLine(); System.out.println("Got message from client" +cid +":" + sms); if(sms.equals("end")) break; spubchat.send("Client" + cid +":" +sms); Thread.yield(); } }catch(Exception e) { }}
COMPUTER NETWORKING LAB
}public class spubchat{ JTextArea txta1; JFrame f; ServerSocket s; Socket soc[]=new Socket[10]; BufferedReader in[]=new BufferedReader[10]; static PrintWriter out[]=new PrintWriter[10]; Thread t[]=new Thread[10]; static int count=0; public spubchat() throws IOException { f=new JFrame("Server"); txta1=new JTextArea(); f.setSize(new Dimension(400,300)); f.getContentPane().setLayout(null); f.setVisible(true); f.getContentPane().add(txta1); txta1.setBounds(10,10,370,220); txta1.append("Waiting for connection"); s=new ServerSocket(5217); while(count<10) { soc[count]=s.accept(); txta1.append("\nAccepted Client" +(count+1)); in[count]=new BufferedReader(new InputStreamReader(soc[count].getInputStream())); out[count]=new PrintWriter(soc[count].getOutputStream(),true); t[count]=new Thread(new Listener(in[count],count+1)); t[count].start(); count++; }}public static void send(String sms){ int i; for(i=0;i<count;i++) { out[i].println(sms); }}public static void main(String args[]) throws IOException{ spubchat sc=new spubchat(); }
COMPUTER NETWORKING LAB
}
Client Sideimport java.net.*;import java.util.*; import java.awt.*;import java.awt.event.*;import javax.swing.*;
public class cpubchatE{ JButton b1; JButton ex1; static JTextField txt1; static JTextArea txa1; JFrame f; static Socket soc; private final static String newline = "\n"; static String str,str1; BufferedReader in; PrintWriter pw; String sstr,rstr; public cpubchatE() throws IOException { f=new JFrame("Client"); Panel p=new Panel(); f.getContentPane().add(p); p.add(txt1=new JTextField(10)); p.add(b1=new JButton("Send")); p.add(ex1=new JButton("Quit")); p.add(txa1=new JTextArea(15,30)); p.setVisible(true); f.setVisible(true); f.setSize(400,300); f.setTitle("Client"); f.setResizable(true); soc=new Socket(InetAddress.getLocalHost(),5217); in=new BufferedReader( new InputStreamReader(soc.getInputStream())); pw=new PrintWriter(soc.getOutputStream(),true); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try
COMPUTER NETWORKING LAB
{ send(); } catch(IOException ee) { System.out.println(ee); } } }); ex1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { System.exit(0); } catch(Exception ee) { System.out.println(ee); } }}); while(true) { sstr=in.readLine(); txa1.append(sstr+newline); } } public void send() throws IOException { sstr = txt1.getText(); pw.println(sstr); if (sstr.equals("end")) System.exit(0); txt1.setText(""); } public static void main(String args[]) throws Exception { try { new cpubchatE(); } catch(Exception e) { System.out.println(e); }
COMPUTER NETWORKING LAB
}}
OUTPUT
Server side
Client 1
COMPUTER NETWORKING LAB
Client 2
Client 3
COMPUTER NETWORKING LAB
RESULT
The program was successfully executed and public chat was implemented successfully.
EXPT. NO. 11 REMOTE METHOD INVOCATION DATE: 14.09.2011
COMPUTER NETWORKING LAB
AIM
To design and implement Remote Method Invocation to evaluate factorial of a given number.
ALGORITHM
Interface
Step 1: Start.
Step 2: Create an interface that extends Remote class.
Step 3: Declare the function to find the factorial of a given number.
Step 4: Stop.
Implemention
Step 1: Start.
Step 2: Create an implementation that extends the UnicastRemoteObject and implements the interface
class.
Step 3: Define the factorial function.
Step 4: Stop.
Server Side
Step 1: Start.
Step 2: Create an implementation class object.
Step 3: Using Naming.rebind("rmi://localhost:1099/FactServer",implobj) bind the implementation class
object to the string “FactServer” in the RMI registry.
Step 4: Stop.
Client Side
Step 1: Start.
Step 2: Look up the RMI registry for the factorial service using
Naming.lookup("rmi://localhost:1099/FactServer") and assign it to an interface object.
Step 3: Read the number from the user.
Step 4: Call the remote function for finding the factorial with the number as parameter
Step 5: Display the result
Step 6: Stop
COMPUTER NETWORKING LAB
PROGRAM
Interfaceimport java.rmi.*;public interface FacInf1 extends Remote{ long fact(long f) throws RemoteException;}
Implementationimport java.rmi.*;import java.rmi.server.*;public class FacImpl1 extends UnicastRemoteObject implements FacInf1{ public FacImpl1() throws RemoteException { super(); } public long fact(long f) throws RemoteException { long n=f,fac=1; while(n>0) { fac=fac*n; n--; } System.out.println("Factorial calculated successfully"); return fac; }}
Server sideimport java.net.*;import java.rmi.*;public class FacServer1{ public static void main(String args[]) { try { FacImpl1 implobj=new FacImpl1(); Naming.rebind("rmi://localhost:1099/FactServer",implobj); } catch(Exception e) { System.out.println("Exception"+e);
COMPUTER NETWORKING LAB
} }}
Client sideimport java.rmi.*;import java.io.*;import java.lang.*;
public class FacClient1{ public static void main(String args[]) { try { FacInf1 infobj=(FacInf1)Naming.lookup("rmi://localhost:1099/FactServer"); System.out.println("Enter the number to find factorial"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str=br.readLine(); long k=Long.parseLong(str); System.out.println("The factorial of "+ k + " is " + infobj.fact(k)); } catch(Exception e) { System.out.println("Exception:"+e); } }}
OUTPUT
Server side
COMPUTER NETWORKING LAB
Client side
RESULT
The method was invoked remotely and executed successfully, and the result was successfully
displayed at the client side.
EXPT. NO. 12 FILE SERVER DATE: 20.09.2011
AIM
COMPUTER NETWORKING LAB
To design and implement a file server to transfer a file from server side to client side.
ALGORITHM
Server SideStep1: Start
Step2: Create a Sever socket and bind it to a port number
Step3: Listens client request
Step4: Establish connection with the client using accept( ) method
Step5: Read filename from client and then read contents of file using FileReader
Step7: Send the file contents to client using OutputStream
Step 8:Close the socket
Step 9:Stop
Client SideStep1:Start
Step2: Create a client socket and connect it to server port
Step3: Read filename from keyboard using InputStream
Step4: Send filename to server using PrintWriter
Step5: Receive file contents from server using InputStream
Step6: Read new path for the file
Step 6:Write file to new location using FileWriter
Step 7:Close the socket
Step 8:Stop
PROGRAM
Server sideimport java.net.*;import java.io.*;public class ftpserv{ public static void main(String args[]) throws Exception { ServerSocket ss = null;
COMPUTER NETWORKING LAB
Socket s = null; try { ss = new ServerSocket(4000); System.out.println("Connecting with client"); s = ss.accept(); System.out.println("Connection is successful"); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); String str = br.readLine( ); BufferedReader br2= new BufferedReader(new FileReader(str) ); System.out.println("File found"); // keeping output stream ready to send the contents PrintWriter pw = new PrintWriter(s.getOutputStream(), true); String str2; while((str2 = br2.readLine()) != null) // reading line-by-line from file { pw.println(str2); // sending each line to client } System.out.println("File send successfully"); } catch (SocketException e){ System.out.println(e);} catch (FileNotFoundException e) { System.out.println("File does not exist"); } catch (IOException e) { System.out.println(e); } catch (Exception e) { System.out.println(e); } finally { s.close(); ss.close(); // closing network sockets } }}
Client sideimport java.net.*;import java.io.*;public class ftpclient{ public static void main( String args[ ] ) throws Exception { Socket s = null; try { s = new Socket( "localhost", 4000); System.out.print("Enter path of file at server: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
COMPUTER NETWORKING LAB
String str = br.readLine(); PrintWriter pw = new PrintWriter(s.getOutputStream(), true); pw.println(str); BufferedReader br2 = new BufferedReader(new InputStreamReader(s.getInputStream())); String str2; System.out.print("Enter new path of file at client: "); str2= br.readLine(); BufferedWriter bw = new BufferedWriter(new FileWriter(str2)); while((str2 = br2.readLine()) != null) // reading line-by-line { bw.write(str2); bw.newLine(); } bw.close(); System.out.println("File saved"); } catch (SocketException e) { System.out.println(e); } catch (FileNotFoundException e) { System.out.println("File does not exist"); } catch (IOException e) { System.out.println(e); } catch (Exception e) { System.out.println(e); } finally { s.close(); } }}
OUTPUT
Server
COMPUTER NETWORKING LAB
Client
RESULT
The program successfully executed and the file transfer from server to client done successfully.
EXPT. NO. 13 PING SERVER DATE: 07.10.2011
AIM
COMPUTER NETWORKING LAB
To design and implement a ping server program using java.
ALGORITHM
Server side
Step 1: Start
Step 2: Create Server Socket and bind it to the port.
Step 3: Listen for client response.
Step 4: Accept the connection.
Step 5: Get the message from client.
Step 6: Send back the data to the client using PrintWriter.
Step 7: Stop.
Client side
Step 1: Start
Step 2: Create client socket and bind it to the port.
Step 3: Read the input from client and send it to server.
Step 4: Receive the response from the server and find the ping statistics.
Step 6: Display the ping output with packet lost count, response time, round trip times.
Step 7: Stop
PROGRAM
Server sideimport java.net.*;import java.io.*;import java.net.SocketException;import java.util.*;import java.lang.*;class pingtcpserver{ public static void main(String args[]) throws Exception { ServerSocket s=null; Socket soc=null; String smsg="";
COMPUTER NETWORKING LAB
PrintWriter pw; try { s=new ServerSocket(5219); System.out.println("Connecting with client........."); soc=s.accept(); System.out.println("Connection Established"); BufferedReader brin=new BufferedReader(new InputStreamReader(soc.getInputStream())); for(int i=0;i<5;i++) { pw=new PrintWriter(soc.getOutputStream(),true); smsg=brin.readLine(); if(i==3) Thread.sleep(100); pw.println(smsg); } } catch(SocketException e) { System.out.println(e); } finally { s.close(); soc.close(); } }}
Client sideimport java.io.*;import java.net.*;import java.net.SocketException;import java.util.*;
class pingtcpclient{ public static void main(String args[]) throws Exception { Socket soc=null; try { String str=""; String ip="127.0.0.1"; String cmsg="Ping Testing";
COMPUTER NETWORKING LAB
int sendcount=0,recvcount=0,lost=0; long diff=0,avg=0,min=0,max=0,tottime=0; soc=new Socket("localhost",5219); BufferedReader in=new BufferedReader( new InputStreamReader(soc.getInputStream())); PrintWriter pw=new PrintWriter(soc.getOutputStream(),true); System.out.println(""); System.out.println("Pinging"+" "+ ip+" "+"with"+" "+cmsg.length()+" "+"bytes of data:"); System.out.println(""); for(int i=0;i<5;i++) { Date senddate=new Date(); sendcount=sendcount+1; pw.println(cmsg); str=in.readLine(); Date receivedate=new Date(); if (!str.equals("")) { recvcount=recvcount+1; } lost=recvcount-sendcount; diff=((receivedate.getTime())-(senddate.getTime())); if(recvcount==1) { min=diff; } tottime=tottime+diff; if(diff<min) { min=diff; } if(diff>max) { max=diff;}avg=(tottime/recvcount);if (diff > 2000)System.out.println("Timeout");elseSystem.out.println("Reply from "+ ip +": "+"bytes=" +str.length() +" "+"time<"+diff+ "ms TTL=128");}//forSystem.out.println("");System.out.println("Ping statistics for "+ip+":");System.out.println (" Packets: Sent = "+cmsg.length()+", Received= "+str.length() +", Lost= "+ lost+"("+(lost/sendcount)*100+"%loss),");System.out.println("Approximate round trip times in milli-seconds:");
COMPUTER NETWORKING LAB
System.out.println(" Minimum= "+min+"ms, Maximum= "+max+"ms, Average= "+avg+"ms");}//trycatch(Exception e) { System.out.println(e); } finally { soc.close(); } }}
OUTPUT
Server side
Client side
COMPUTER NETWORKING LAB
RESULT
The program was successfully executed and ping server was implemented successfully.
EXPT. NO. 14 REMOTE COMMAND EXECUTION DATE: 11.10.2011
COMPUTER NETWORKING LAB
AIM
To implement a client server program to execute a command from client in a remote server.
ALGORITHM
Server sideStep 1: Start
Step 2: Create a serversocket.
Step 3: Bind the connection to a port
Step 4: Listen for client response.
Step 5: Accept the connection
Step 6: Receive the command from client.
Step 7: Create an instance of the Runtime class using the getRuntime() Method
Step 8: Execute the command by creating a Process class instance using the exec() method of the
Runtime class with the command as parameter.
Step 9: Display the output on the server.
Step 10: Close the socket.
Step 11:Stop
Client sideStep 1: Start
Step 2: Create a socket with the remote server IP and port
Step 3: Read the command from the user.
Step 4: Assign the command to some string variable.
Step 5: Send the string to the server
Step 6: Close the socket.
Step 7: Stop
PROGRAM
Server side import java.net.*;import java.io.*;
COMPUTER NETWORKING LAB
import java.util.*;
public class RemotecmdServer{ public static void main(String[] args) { String cmd =null; int port=5217; String pingResult = ""; try { ServerSocket ss=new ServerSocket(port); System.out.println("Waiting for connection"); Socket sock=ss.accept(); System.out.println("Connection established"); BufferedReader br=new BufferedReader(new InputStreamReader(sock.getInputStream())); cmd=br.readLine(); Runtime r = Runtime.getRuntime(); Process p = r.exec(cmd); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String inputLine; while ((inputLine = br.readLine()) != null) { System.out.println(inputLine); pingResult += inputLine; } br.close(); }//try catch (IOException e) { System.out.println(e); } }}
Client sideimport java.net.*;import java.io.*;import java.util.*;public class RemotecmdClient{ public static void main(String[] args) { String cmd =""; Socket socket;
COMPUTER NETWORKING LAB
PrintWriter pw; try { socket=new Socket("localhost",5217); pw=new PrintWriter(socket.getOutputStream(),true); InetAddress addr=InetAddress.getLocalHost(); for(int i=0;i<args.length;i++) { cmd=cmd +args[i]+" "; } pw.println(cmd); } catch(Exception e) { System.out.println("Exception"+e); } } }
OUTPUT
Server side
Client Side
COMPUTER NETWORKING LAB
RESULT
The commands given by client was successfully executed in the server through remote command
execution.
EXPT. NO. 15 CYCLIC REDUNDANCY CHECK DATE: 12.10.2011
COMPUTER NETWORKING LAB
AIM
To design and implement a cyclic redundancy check program in java.
ALGORITHM
Server sideStep 1: Start
Step 2: Create a ServerSocket and bind it to a port.
Step 3: Listen for the client response.
Step 4: Accept the client connection using accept() method.
Step 5: Receive the encapsulated data from the client.
Step 6: Retrieve the data and checksum from the received encapsulated data.
Step 7: Calculate the checksum of the retrieved data.
Step 8: Check if the calculated checksum and received checksum are equal, then go to step 9.
Else go to step 10.
Step 9: Send a message to client that data received correctly.
Step 10:Send a message to client that data has been corrupted.
Step 11:Close the ServerSocket.
Step 12:Stop
Client sideStep 1: Start
Step 2: Create a client socket and connect it to a server.
Step 3: Read the keyboard data using InputStream object.
Step 4: Calculate the checksum of data.
Step 5: Encapsulate data with checksum.
Step 6: Send the encapsulated data to server.
Step 7: Receive the acknowledgement message from the server.
Step 8: Display the acknowledgement message.
Step 9: Close the client socket.
Step 10:Stop
PROGRAM
COMPUTER NETWORKING LAB
Server sideimport java.net.*;import java.io.*;import java.util.*;import java.util.zip.CRC32;
class crctcpserver{ public static void main(String args[]) throws Exception { ServerSocket s=null; Socket soc=null; String smsg,str1,str2,str=""; BufferedReader brin; int k; PrintWriter pw; long l; try { s=new ServerSocket(5217); System.out.println("Connecting with client........."); soc=s.accept(); System.out.println("Connecting Established"); brin =new BufferedReader(new InputStreamReader(soc.getInputStream())); smsg=brin.readLine(); System.out.println("\nData from client together with Checksum:" + smsg); k=smsg.indexOf("|"); str1=smsg.substring(0,k); str2=smsg.substring(k+1,smsg.length()); System.out.println("\nReceived Data:"+str2); System.out.println("Received Checksum:"+str2); CRC32 crcs =new CRC32(); crcs.update(str1.getBytes()); System.out.println("\nCalculated CheckSum at Server:"+crcs.getValue()); pw=new PrintWriter(soc.getOutputStream(),true); l=Long.parseLong(str2); if(l==(crcs.getValue())) pw.println("Data Received Correctly"); else pw.println("Data is corrupted"); } catch(Exception e) { System.out.println(e);
COMPUTER NETWORKING LAB
} finally { s.close(); soc.close(); } } }
Client sideimport java.io.*;import java.net.*;import java.util.*;import java.util.zip.CRC32;
class crctcpclient{ public static void main(String args[]) throws Exception { String str,cmsg,msg=""; Socket soc=null; PrintWriter pw; BufferedReader in,br; try { soc=new Socket("localhost",5217); System.out.println("Enter the data to be send to server"); br=new BufferedReader(new InputStreamReader(System.in)); cmsg=br.readLine(); CRC32 crcs =new CRC32(); crcs.update(cmsg.getBytes()); pw =new PrintWriter(soc.getOutputStream(),true); msg=cmsg+"|"+crcs.getValue(); System.out.println( "\nData send to server together with Checksum:"+msg); pw.println(msg); in=new BufferedReader( new InputStreamReader(soc.getInputStream())); str=in.readLine(); System.out.println("\nAcknowledgement from server:" +str); } catch(Exception e) { System.out.println(e); } finally
COMPUTER NETWORKING LAB
{ soc.close(); } }}
OUTPUT
Server side
Client side
COMPUTER NETWORKING LAB
RESULT
The program was successfully executed and the cyclic redundancy check was done successfully.
EXPT. NO. 16 WEB SERVER DATE: 14.10.2011
COMPUTER NETWORKING LAB
AIM
To design and implement web server using java.
ALGORITHM
Server Side:Step1: Start
Step2: Create a ServerSocket and bind it to a port
Step3: Listen for the client connection
Step4: Accept Connection from client
Step5: Receive the request for the web page from the client
Step6: Serve the client with the requested web page
Step7: Close the socket
Step8: Stop
Client SideStep1 :Start
Step2: Create a Client Socket with a port number and IP
Step3: Send the GET request for web page to the server
Step4: Receive the requested web page from the server and display it in browser
Step5: Close the socket
Step 6: Stop
PROGRAM
Server sideimport java.net.*;import java.io.*;
public class WebServer {public static void main(String args[]) throws Exception {ServerSocket sersock = null;Socket sock = null;int connects = 0;try { // Establish the connection with server
COMPUTER NETWORKING LAB
sersock = new ServerSocket(80, 5); // http port, 5 clients System.out.println("Server ready for connection"); while (connects < 5) { sock = sersock.accept(); System.out.println("Client " + (connects + 1) + " connected: "+ sock); ServiceClient(sock); connects++; } } catch (SocketException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } catch (Exception e) { System.out.println(e); } finally { sock.close(); sersock.close(); // closing network sockets }}public static void ServiceClient(Socket cs) throws IOException {DataInputStream dis = null;DataOutputStream dos = null;try{ dis = new DataInputStream(cs.getInputStream()); dos = new DataOutputStream(cs.getOutputStream()); StringBuffer sbuf = new StringBuffer("<html><head><title>My WebPage</title></head><body bgcolor=\"silver\"><br><hr><H1><MARQUEE><b>WELCOME TO COMPUTER NETWORKING LAB</b></MARQUEE></H1><br><hr></c><br><b>M.Tech CSD</b><br><br> <br> <b> COMPUTER NETWORKING LAB<br><br><b>LAB CYCLE</b><br><br><b>CYCLE I</b> <br><br>1. INET ADDRESS DEMO<br>2. URL AND URL CONNECTION<br>3.ECHO UDP SERVER<br>4.UDP BI-DIRECTIONAL COMMUNICATION<br>5.DATE SERVER USING TCP<br>6.ECHO TCP SERVER<br>7.TCP TALK SERVER<br>8.TCP BI-DIRECTIONAL COMMUNICATION<br>9.BROADCASTING</body></html>");
String str;while ((str = dis.readLine()) != null) {if (str.equals("")) { dos.writeBytes(sbuf.toString()); // send the html page to client break; } }}catch (Exception e)
COMPUTER NETWORKING LAB
{ System.out.println(e); } finally { dis.close(); dos.close(); System.out.println("Cleaning up connection: " + cs); cs.close();// closing network sockets } }}
Client Sideimport java.net.*;import java.io.*;
public class WebClient { public static void main(String args[]) throws Exception { Socket sock = null; try { sock = new Socket("127.0.0.1", 80); // reading the file name from keyboard. Uses input stream System.out.println("Client: " + sock); getPage(sock); } catch (SocketException e) {System.out.println(e); } catch (IOException e) {System.out.println(e); } catch (Exception e) {System.out.println(e); } finally { sock.close(); }}public static void getPage(Socket cs) throws Exception { DataInputStream dis = null; DataOutputStream dos = null; try { dis = new DataInputStream(cs.getInputStream()); dos = new DataOutputStream(cs.getOutputStream()); dos.writeBytes("GET/HTTP/1.0\r\n\r\n");
COMPUTER NETWORKING LAB
String strResponse; while ((strResponse = dis.readLine()) != null) { if (strResponse.indexOf("</html>") != -1)
break; } System.out.println("Webpage rendered to client. \nOpen \"http://localhost\" in IE"); } catch (Exception e) { System.out.println(e); } finally { dis.close(); dos.close(); cs.close();// closing network sockets } }}
OUTPUT
Server Side
Client Side
COMPUTER NETWORKING LAB
My Webpage
RESULTThe program was successfully executed and the data transfer from client to server was done
successfully.
EXPT. NO. 17 TELNET PROTOCOL DATE: 18.10.2011
COMPUTER NETWORKING LAB
AIM
To design and implement, remote login using TELNET protocol.
ALGORITHM
Server sideStep1: Start.
Step2: Create a server socket and bind it to a port.
Step3: Listen for any request from client.
Step4: Establish a connection using accept() method.
Step5: Receive the username and password from client using BufferedReader object.
Step6: If the username and password are valid, set the status as “Success”.
Step7: Using a PrintWriter object, send the list of directories and files in the specified path to the client.
Step8: Close the socket.
Step9: Stop.
Client sideStep1: Start.
Step2: Create a client socket.
Step3: Establish a connection with server.
Step4: Using BufferedReader object read the username and password from the user, then send it to
server using PrintWriter object.
Step5: If status is “Success”, display the list of directories and files received from server.
Step6: Close the socket.
Step7: Stop.
PROGRAM
Server side import java.net.*;import java .io.*;import java.util.Date;public class telserver{
COMPUTER NETWORKING LAB
public static void main(String args[]) throws IOException { ServerSocket ss; Socket cs; BufferedReader br; PrintWriter pw; String username,pwd,status; try { ss=new ServerSocket(6789); cs=ss.accept();
br=new BufferedReader(new InputStreamReader(cs.getInputStream())); pw=new PrintWriter(cs.getOutputStream(),true); username=br.readLine(); pwd=br.readLine(); if((username.equals("admin"))&&(pwd.equals("admin")))
status = "Sucess";else
status="Login failed !!!! Try it next time";pw.println(status);pw.flush();File dir = new File("Z:\\");String[] children = dir.list();if (children == null) {
System.out.println("Directory not found\n");}else {
for (int i=0; i<children.length; i++) {
String filename = children[i];pw.println(filename);
}pw.println("bye");
}}
catch(Exception e) { System.out.println("exception"); }
}}
Client side
COMPUTER NETWORKING LAB
import java.net.*;import java.io.*;import java.util.Date;public class telclient{
public static void main(String args[])throws IOException { Socket cs; BufferedReader br1,br2; PrintWriter pw; String username,pwd,status,result; try {
br1=new BufferedReader(new InputStreamReader(System.in)); cs=new Socket("localhost",6789);
br2=new BufferedReader(new InputStreamReader(cs.getInputStream())); pw=new PrintWriter(cs.getOutputStream(),true); System.out.println("Enter the username : ");
username=br1.readLine();System.out.println("Enter the password : ");pwd=br1.readLine();pw.println(username);pw.println(pwd);status=br2.readLine();if(status.equals("Sucess")){
System.out.println("Login sucess \n");result=br2.readLine();System.out.println("Listed directories and files : \n");while(!(result.equals("bye"))){
System.out.println(result);result=br2.readLine();
}}else{
System.out.println("Sorry!!! Login failed....Try next time!!");}
} catch(Exception e) { System.out.println("exception"); } }
COMPUTER NETWORKING LAB
}
OUTPUT
Server side
Client side
RESULT
The program to implement remote login using TELNET protocol was executed successfully and
output was obtained.
COMPUTER NETWORKING LAB
EXPT. NO. 18 ADDRESS RESOLUTION PROTOCOL DATE: 19.10.2011
AIM
To design and implement Address Resolution Protocol using java.
ALGORITHM
COMPUTER NETWORKING LAB
Server sideStep1: Start
Step2: Create a Server Socket and accept the connection from the client
Step3: Read the IP address from the client.
Step4: Create a hash table that consists of the IP Address as the key and MAC Address as the
corresponding value.
Step5: Iterate through the key values of the hashtable to find a match for the ip address.
Step6: If the match is found then send corresponding MAC Address to the client.
Step7: Close the socket
Step8: Stop
Client sideStep1: Start
Step2: Create a Socket and connect to the server port.
Step3: Read the IP address from the user.
Step4: Send the IP address to the server.
Step5: Read the MAC address send from the server.
Step6: Display the MAC Address.
Step7: Close the socket.
Step8: Stop
PROGRAM
Server Sideimport java.io.*;import java.net.*;import java.util.*;public class ARPServer{ public static void main(String args[]) {
int port=9876;Socket s=null;BufferedReader br;PrintWriter pw=null;String ip,key;
COMPUTER NETWORKING LAB
try {
ServerSocket sersock=new ServerSocket(port); s=sersock.accept();
br=new BufferedReader(new InputStreamReader(s.getInputStream()));pw=new PrintWriter(s.getOutputStream(),true);ip=br.readLine();System.out.println("ip::"+ip );Hashtable<String,String> ht=new Hashtable<String,String>();
ht.put("127.16.8.16","00:14:0B:49:58:C3");ht.put("127.16.8.15","00:14:OB:49:58:55");ht.put("127.16.8.7","00:14:0B:49:58:09");
Enumeration en=ht.keys();//to enumerate through the key values of the hashtablwhile(en.hasMoreElements()){
key=(String)en.nextElement();if(key.equals(ip)){pw.println((String)ht.get(key));}
}
} catch(SocketException e) {
System.out.println("socket exception ::" +e);
}
catch(UnknownHostException e) { System.out.println("UnknownHost exception ::" +e); } catch(Exception e) { System.out.println("Exception::"+e); } }}Client Sideimport java.io.*;import java.net.*;public class ARPClient{ public static void main(String args[])
COMPUTER NETWORKING LAB
{ int port=9876; String ip; Socket clientsocket=null; BufferedReader br,brsock; PrintWriter pw=null; try { clientsocket=new Socket("localhost",port); br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the ip address:"); ip=br.readLine(); pw=new PrintWriter(clientsocket.getOutputStream(),true); pw.println(ip); brsock=new BufferedReader(new InputStreamReader(clientsocket.getInputStream())); System.out.println("Mac Address::"+brsock.readLine()); clientsocket.close(); } catch(SocketException e) {
System.out.println("socket exception ::" +e); } catch(UnknownHostException e) {
System.out.println("UnknownHost exception ::" +e);
}catch(Exception e)
{ System.out.println("Exception ::" +e); } }}
OUTPUT
Server Side
COMPUTER NETWORKING LAB
Client Side
RESULT
The program for demonstrating the Address Resolution protocol was executed successfully and
output verified.