EX NO : 1 DATE : IMPLEMENTATION OF TCP/IP AIM: To implement client-server communication using TCP/IP in java. ALGORITHM: STEP 1 : Start the process STEP 2 : Create a server socket and client socket STEP 3 : Get the host name and establish the connection STEP 4 : Send the data through the output print writer and receive the message through the input stream reader STEP 5 : Similarly communication is done on both sides STEP 6 : End the process SERVER: import java.net.*; import java.io.*; public class TCPSER { public static void main(String ar[]) throws Exception { ServerSocket s=new ServerSocket(500); Socket s1=s.accept(); DataInputStream dis=new DataInputStream(System.in); DataInputStream dis1=new DataInputStream(s1.getInputStream()); PrintStream ps=new PrintStream(s1.getOutputStream(),true); String str; String str1; while(true) { str=dis.readLine(); ps.println(str); str1=dis1.readLine(); System.out.println(str1); if(str.equals("stop")) break; } } } CLIENT: import java.net.*; import java.io.*; public class TCPCL { public static void main(String ar[]) throws Exception { Socket s=new Socket("localhost",500); DataInputStream dis=new DataInputStream(System.in); DataInputStream dis1=new DataInputStream(s.getInputStream()); PrintStream ps=new PrintStream(s.getOutputStream(),true); String str; String str1; while(true) { str=dis.readLine(); ps.println(str); str1=dis1.readLine(); System.out.println(str1); if(str.equals("stop")) break; } } } OUTPUT: D:\java\jdk1.5\bin>javac TCPSER.java D:\java\jdk1.5\bin>java TCPSER hai client hai server D:\java\jdk1.5\bin>javac TCPCL.java D:\java\jdk1.5\bin>java TCPCL hai server hai client RESULT: Thus the program to implement client server communication using TCP/IP was executed and the output was obtained. EX NO : 2 DATE : IMPLEMENTATION OF UDP AIM: To implement client-server communication using UDP in java. ALGORITHM: STEP 1 : Start the process STEP 2 : Get source and destination port numbers STEP 3 : Get the host name and establish the connection STEP 4 : Create datagram socket to establish the connection and send message through the datagram packet STEP 5 : Similarly communication is done on both sides. When exit is typed close the connection. STEP 6 : End the process. PROGRAM import java.lang.*; import java.io.*; import java.net.*; class UDP1 { public static void main(String args[]) { try { String s1,s2,host; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter port number for the source and destination"); s1=in.readLine(); s2=in.readLine(); System.out.println("Enter the host name of the destination"); host=in.readLine(); System.out.println("Type exit to quit"); DatagramSocket ds=new DatagramSocket(Integer.parseInt(s1)); new Sender(ds,s2,host); new receive(ds); } catch(Exception fd) { } } System.out.println(“ERROR”); } class Sender extends Thread { DatagramSocket ds; byte data[]=new byte[100]; int i=0,port; String host; Sender(DatagramSocket d1,String str,String host1) { ds=d1; port=Integer.parseInt(str); host=host1; start(); } public void run() { while(true) { try { data[i++]=(byte)System.in.read(); if(data[i-1]=='\n') { ds.send(new DatagramPacket(data,i2,InetAddress.getByName(host),port)); if(new String(data,0,i-2).equals("exit")) { System.out.println("your write mode on Exit--you can't write to other use"); break; } i=0; }} catch(Exception fd) { } } } } class receive extends Thread { DatagramSocket ds; byte data[]=new byte[100]; receive(DatagramSocket d1) { ds=d1; start(); } public void run() { while(true) { try { DatagramPacket dp=new DatagramPacket(data,data.length); ds.receive(dp); System.out.println(new String(dp.getData(),0,dp.getLength())); if(new String(dp.getData(),0,dp.getLength()).equals("exit")) { System.out.println("You read mode on exit--you can't read from other use"); break; }} catch(Exception tr) {} } } } OUTPUT: D:\jdk1.5\bin>javac UDP1.java D:\jdk1.5\bin>java UDP1 Enter port number for the source and destination 12 14 Enter the host name of the destination Local host Type exit to quit Hai Welcome Hello D:\jdk1.5\bin>javac UDP1.java D:\jdk1.5\bin>java UDP1 Enter port number for the source and destination 14 12 Enter the host name of the destination Local host Type exit to quit Hai Welcome Hello RESULT: Thus the program to implement client – server communication using UDP in java was executed and output was obtained. EX NO : 3 DATE : IMPLEMENTATION OF FTP AIM: To implement File Transfer Protocol using Java. ALGORITHM: STEP 1 : Start the process STEP 2 : Create server socket and socket and establish the connection between the source and destination systems. STEP 3 : In the server side, read the content of the source file using FILEINPUTSTREAM and write it in the destination file using OUT.PRINTLN. STEP 4 : In client side, connect the socket and get source and destination file names STEP 5 : Transfer the source file from server to the destination file in the client. STEP 6 : Terminate the program. SERVER: import java.io.*; import java.net.*; class fileServer { public static void main(String a[])throws Exception { Socket s=null; ServerSocket ss=null; DataOutputStream dos=null; DataInputStream dis=null; ss=new ServerSocket(55555); s=ss.accept(); dos=new DataOutputStream(s.getOutputStream()); dis=new DataInputStream(s.getInputStream()); String s1; s1=dis.readLine(); FileInputStream fis=new FileInputStream(s1); int y; while((y=fis.read())!=-1) { dos.writeBytes(" "+(char)y); System.out.print((char)y); ` } System.out.println("File has been sent successfully\n"); dos.close(); s.close(); } } CLIENT: import java.io.*; import java.net.*; public class fileClient { public static void main(String a[]) throws Exception { Socket s=null; DataInputStream dis=null; s=new Socket("localhost",55555); dis=new DataInputStream(s.getInputStream()); DataInputStream inp=new DataInputStream(System.in); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String str; System.out.println("Enter the fileneame\n"); str=inp.readLine(); dos.writeBytes(str); dos.writeBytes("\n"); File o=new File("out.txt"); FileOutputStream fos=new FileOutputStream(o); int x; while((x=dis.read())!=-1) { fos.write(x); System.out.print((char)x); } System.out.println("\n File received successfully\n"); } } OUTPUT: D:\java\jdk1.5\bin>javac fileClient.java D:\java\jdk1.5\bin>java fileClient Enter the filename data.java import java.io.*; class data { public static void main(String args[]) { System.out.println(“welcome to all”); } } D:\java\jdk1.5\bin>javac fileServer.java D:\java\jdk1.5\bin>java fileServer import java.io.*; class data { public static void main(String args[]) { System.out.println(“welcome to all”); } } RESULT: Thus the file transfer protocol using java is implemented. EX NO : 4 DATE : IMPLEMENTATION OF ECHO/PING/TALK AIM: To implement the echo/ping/talk commands using the java. ALGORITHM: ECHO PROGRAM: STEP 1 : Start the process STEP 2 : get the string from the user and store it in an array. STEP 3 : Scan the string and if any escape character (e.g., \n, \t) Occurs, then do appropriate operation. STEP 4 : Terminate the process. PING PROGRAM: STEP 1 : Start the process. STEP 2 : Create the socket and get the domain name and the socket no and store it in a variable. STEP 3 : If connected then remote host will reply with the address and the no of packets sent and received. STEP 4 : Terminate the process. TALK PROGRAM : STEP 1 : Start the process. STEP 2 : In client, get the domain name of the server and establish the connection. Send message to server. STEP 3 : In server, create ServerSocket and establish the connection. STEP 4: Get the incoming message through the InputStreamReader and print message through the OutputStreamReader. STEP 5: Terminate the process. SERVER: [ECHO] import java.io.*; import java.net.*; import java.lang.String; public class echoserver { public static void main(String a[])throws IOException { ServerSocket ss=new ServerSocket(55555); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(s.getInputStream()); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String str; System.out.println("\nSERVER SIDE\n"); while(true) { str=dis.readLine(); dos.writeBytes(str+"\n"); System.out.println("Message from Client\n"); System.out.println(str+"\n"); } } } CLIENT: [ECHO] import java.io.*; import java.net.*; import java.lang.String; public class echoclient { public static void main(String a[])throws IOException { DataInputStream dis=new DataInputStream(System.in); Socket s=new Socket("localhost",55555); DataInputStream inp=new DataInputStream(s.getInputStream()); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String str; System.out.println("CLIENT SIDE\n"); System.out.println("Type exit to quit\n"); System.out.println("Enter the message\n"); while((str=dis.readLine())!=null) { dos.writeBytes(str+"\n"); if(str.equals("exit")) { dos.writeBytes("Client terminated\n"); break; } else { System.out.println("Echo from server\n"); System.out.println(inp.readLine()); System.out.println("Enter the message\n"); } } } } OUTPUT: [ECHO] D:\java\jdk1.5\bin>javac echoserver.java D:\java\jdk1.5\bin>java echoserver SERVER SIDE Message from Client hai Message from Client welcome D:\java\jdk1.5\bin>javac echoclient.java D:\java\jdk1.5\bin>java echoclient CLIENT SIDE Type exit to quit Enter the message hai Echo from server Welcome Echo from server SERVER: [ PING] import java.io.*; import java.net.*; import java.util.*; import java.text.*; class pings { public static void main(String a[])throws Exception { ServerSocket ss=new ServerSocket(5555); Socket s=ss.accept(); int c=0; while(c<4) { DataInputStream dis=new DataInputStream(s.getInputStream()); PrintStream out=new PrintStream(s.getOutputStream()); String str=dis.readLine(); out.println("Reply from"+InetAddress.getLocalHost()+";Length"+str.length()); c++; } s.close(); } } CLIENT: [ PING] import java.io.*; import java.net.*; import java.util.Calendar; class pingc { public static void main(String a[])throws Exception { String str; int c=0; long t1,t2; Socket s=new Socket("localhost",5555); DataInputStream dis=new DataInputStream(s.getInputStream()); PrintStream out=new PrintStream(s.getOutputStream()); while(c<4) { t1=System.currentTimeMillis(); str="welcome to network programming world"; out.println(str); System.out.println(dis.readLine()); t2=System.currentTimeMillis(); System.out.println(";TTL="+(t2-t1)+"ms"); c++; } s.close(); } } OUTPUT: D:\java\jdk1.5\bin>javac pings.java D:\java\jdk1.5\bin>java pings D:\java\jdk1.5\bin> D:\java\jdk1.5\bin>javac pingc.java D:\java\jdk1.5\bin>java pingc Reply from mscit/127.0.0.1;Length36 :TTL=15ms Reply from mscit/127.0.0.1;Length36 :TTL=0ms Reply from mscit/127.0.0.1;Length36 :TTL=16ms Reply from mscit/127.0.0.1;Length36 :TTL=0ms D:\java\jdk1.5\bin> SERVER: [TALK] import java.io.*; import java.net.*; public class talks { public static void main(String a[]) throws Exception { DataInputStream inp=new DataInputStream(System.in); ServerSocket ss=new ServerSocket(5555); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(s.getInputStream()); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String str; System.out.println("SERVER\n"); while(true) { System.out.println("MESSAGE FROM CLIENT\n"); String str1=dis.readLine(); if(str1.equals("exit")) { System.out.println("exit"); break; } else { System.out.println(str1 +" "); System.out.println("Enter the server side message\n"); str=inp.readLine(); dos.writeBytes(str); dos.writeBytes("\n"); } } } } CLIENT: [TALK] import java.io.*; import java.net.*; public class talkc { public static void main(String a[]) throws Exception { DataInputStream inp=new DataInputStream(System.in); Socket s=new Socket(InetAddress.getLocalHost(),5555); DataInputStream dis=new DataInputStream(s.getInputStream()); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String str; System.out.println("CLIENT\n"); System.out.println("Enter the message\n"); while((str=inp.readLine())!=null) { dos.writeBytes(str); dos.writeBytes("\n"); if(str.equals("exit")) { System.out.println("Client Terminated\n"); break; } else { System.out.println("REPLY\n"); System.out.println(dis.readLine()); System.out.println("Enter the client side message\n"); } } s.close(); } } OUTPUT: RESULT: Thus the echo/ping/talk is implemented using java. EX NO : 5 DATE : IMPLEMENTATION OF REMOTE COMMAND EXECUTION AIM: To implement the remote command execution using java. ALGORITHM: STEP 1 : Start the process STEP 2 : In client, get the domain name of the server and establish the connection. Send command to server. STEP 3 : In server, create ServerSocket and establish the connection. STEP 4 : Get the command through the InputStreamReader and execute the command through the exec(command) method. STEP 5: Terminate the process. SERVER: import java.io.*; import java.net.*; public class remotes { public static void main(String a[])throws Exception { ServerSocket ss=new ServerSocket(22222); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(s.getInputStream()); String str=dis.readLine(); Runtime r=Runtime.getRuntime(); Process p=null; p=r.exec(str); System.out.println(str+" has been executed successfully"); } } CLIENT: import java.io.*; import java.net.*; public class remotec { public static void main(String a[])throws Exception { Socket s=new Socket("localhost",22222); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); DataInputStream dis=new DataInputStream(System.in); String str; System.out.println("Enter the command to be executed"); str=dis.readLine(); dos.writeBytes(str+"\n"); System.out.println(str+" has been executed on the server"); s.close(); } } OUTPUT: D:\java\jdk1.5\bin>javac remotes.java D:\java\jdk1.5\bin>java remotes notepad has been executed successfully D:\java\jdk1.5\bin> D:\java\jdk1.5\bin>javac remotec.java D:\java\jdk1.5\bin>java remotec Enter the command to be executed notepad notepad has been executed on the server D:\java\jdk1.5\bin> RESULT: Thus the implementation of remote command execution program was executed successfully EX NO : 6 DATE : IMPLEMENTATION OF ARP AIM: To implement the ARP to get the Ethernet address and IP address using java. ALGORITHM: STEP 1 : Start the process STEP 2 : Store the IP address and the Ethernet address in array. STEP 3 : Create a socket and a datagram packet. STEP 4 : Send the datagram packet containing the data, data length, address, and port no. STEP 5: If the packet address is equal to IP address then print the corresponding Ethernet address. STEP 6 : If the packet address is equal to Ethernet address then print the corresponding IP address. STEP 7: Terminate the process. SERVER: import java.io.*; import java.net.*; class arps { public static void main(String a[]) throws Exception { ServerSocket ss=new ServerSocket(555); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(s.getInputStream()); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); DataInputStream in=new DataInputStream(System.in); System.out.println("Enter an IP address"); String str=in.readLine(); dos.writeBytes(str+"\n"); System.out.println("The corresponding MAC address is"+dis.readLine()); } } CLIENT: import java.io.*; import java.net.*; class arpc { public static void main(String a[]) throws Exception { Socket s=new Socket("localhost",555); DataInputStream dis=new DataInputStream(s.getInputStream()); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String ip[]= {"172.16.5.39","172.16.5.40","172.16.5.41","172.16.5.42"}; String mac[]={"01-10-B5-F2-EF-39","01-10-B5-F2-EF-40", "01-10-B5-F2-EF-41","01-10-B5-F2-EF-42"}; String str=dis.readLine(); System.out.println("IP address received from server"+str); int flag=0; for(int i=0;i<5;i++) { if(str.equals(ip[i])==true) { flag=1; String str1=mac[i]; dos.writeBytes(str1+"\n"); break; } } if(flag==0) System.out.println("Given IP address is not in this network"); s.close(); } } OUTPUT: D:\java\jdk1.5\bin>javac arps.java D:\java\jdk1.5\bin>java arps Enter an IP address 172.16.5.40 The corresponding MAC address is01-10-05-F2-EF-40 D:\java\jdk1.5\bin> D:\java\jdk1.5\bin>javac arpc.java D:\java\jdk1.5\bin>java arpc IP address received from server172.16.5.40 D:\java\jdk1.5\bin> RESULT: Thus the address resolution protocol is implemented using java. EX NO : 7 DATE : IMPLEMENTATION OF RARP AIM: To implement the RARP to get the Ethernet address and IP address using java. ALGORITHM: STEP 1 : Start the process STEP 2 : Store the IP address and the Ethernet address in array. STEP 3 : Create a socket and a datagram packet. STEP 4 : Send the datagram packet containing the data, data length, address, and port no. STEP 5: If the packet address is equal to IP address then print the corresponding Ethernet address. STEP 6 : If the packet address is equal to Ethernet address then print the corresponding IP address. STEP 7: Terminate the process. SERVER: import java.io.*; import java.net.*; class rarps { public static void main(String a[]) throws Exception { ServerSocket ss=new ServerSocket(555); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(s.getInputStream()); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); DataInputStream in=new DataInputStream(System.in); System.out.println("Enter MAC address"); String str=in.readLine(); dos.writeBytes(str+"\n"); System.out.println("The corresponding IP address is"+dis.readLine()); } } CLIENT: import java.io.*; import java.net.*; class rarpc { public static void main(String a[]) throws Exception { Socket s=new Socket("localhost",555); DataInputStream dis=new DataInputStream(s.getInputStream()); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String ip[]= {"172.16.5.39","172.16.5.40","172.16.5.41","172.16.5.42"}; String mac[]={"01-10-B5-F2-EF-39","01-10-B5-F2-EF-40", "01-10-B5-F2-EF-41","01-10-B5-F2-EF-42"}; String str=dis.readLine(); System.out.println("MAC address received from server"+str); int flag=0; for(int i=0;i<5;i++) { if(str.equals(mac[i])==true) { flag=1; String str1=ip[i]; dos.writeBytes(str1+"\n"); break; } } if(flag==0) System.out.println("Given MAC address is not in this network"); s.close(); } } OUTPUT: D:\java\jdk1.5\bin>javac rarps.java D:\java\jdk1.5\bin>java rarps Enter MAC address 01-10-B5-F2-EF-40 The corresponding IP address is172.16.5.40 D:\java\jdk1.5\bin> D:\java\jdk1.5\bin>javac rarpc.java MAC address received from server01-10-B5-F2-EF-40 D:\java\jdk1.5\bin> RESULT: Thus the reverse address resolution protocol is implemented using java. EX NO : 8 DATE : IMPLEMENTATION OF REMOTE METHOD INVOCATION AIM: To write, a java program for the implementation of remote method invocation. ALGORITHM: STEP 1 : In the interface program declare the argument X and Y. STEP 2 : In the implementation program, the class name is IMPL that implements with the RMI interface. STEP 3 :Call the addition function and return the sum of the value of X and Y. STEP 4 : To bind the implementation function with sum of return value. STEP 5: Client program value is assigned to the variable ‘I’. STEP 6 : The output is printed and the sum of the value X and Y is displayed. STEP 7: Terminate the process. INTERFACE import java.rmi.*; public interface InfoServer extends Remote { int add(int x, int y) throws RemoteException; } SERVER: import java.rmi.*; import java.io.*; import java.rmi.server.*; public class InfoServerImpl extends UnicastRemoteObject implements InfoServer { public InfoServerImpl() throws RemoteException { } public int add(int x, int y) throws RemoteException { return (x+y); } public static void main(String as[]) throws Exception { InfoServerImpl i= new InfoServerImpl(); Naming.rebind("AddServer", i); } } CLIENT: import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.rmi.*; public class InfoClient extends Applet { public static void main(String as[]) throws Exception { InfoServer in= (InfoServer) Naming.lookup("AddServer"); System.out.println(" The Sum of 100 +200 is :"+ in.add(100,200)); } } OUTPUT: D:\java\jdk1.5\bin>javac InfoServer.java D:\java\jdk1.5\bin>start rmiregistry D:\java\jdk1.5\bin>javac InfoServerImpl.java D:\java\jdk1.5\bin>javac InfoClient.java D:\java\jdk1.5\bin>java InfoClient The Sun of 100 +200 is :300 D:\java\jdk1.5\bin> RESULT: Thus the RMI program was implemented and verified. EX NO : 9 DATE : IMPLEMENTATION OF SLIDING WINDOW PROTOCOL AIM: To implement sliding window protocol using java. ALGORITHM: STEP 1 : Start the process STEP 2 : In the server side, create serversocket() to send the messages and a socket() to receive the acknowledgement then get the window size and enter the message and transmit. STEP 3 : In client side, receive the messages and sends the acknowledgement number. STEP 4 : Terminate the process. SERVER: import java.io.*; import java.net.*; class sender { public static void main(String a[]) throws IOException { ServerSocket ss=new ServerSocket(10); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(System.in); DataInputStream inp=new DataInputStream(s.getInputStream()); String buf[]=new String[8]; PrintStream p; int sptr=0,sws=8,nf,ackno,i; String ch; do { p=new PrintStream(s.getOutputStream()); System.out.println("Enter the no. of frames\n"); nf=Integer.parseInt(dis.readLine()); p.println(nf); if(nf<=sws-1) { System.out.println("Enter"+nf+"Messages\n"); for(i=1;i<=nf;i++) { buf[sptr]=dis.readLine(); p.println(buf[sptr]); sptr=++sptr%8; } sws=sws-nf; System.out.println("Acknowledgement received\n"); ackno=Integer.parseInt(inp.readLine()); System.out.println("ACK"+ackno); sws=sws+nf; } else { System.out.println("Exceeds window size"); break; } System.out.println("Do you want to send more frames\n"); ch=dis.readLine(); p.println(ch); } while(ch.equals("y")); s.close(); } } CLIENT: import java.net.*; import java.io.*; class receiver { public static void main(String a[]) throws IOException { Socket s=new Socket(InetAddress.getLocalHost(),10); DataInputStream dis=new DataInputStream(s.getInputStream()); PrintStream p=new PrintStream(s.getOutputStream()); int i=0,rptr=-1,nf,rws=8; String buf[]=new String[8]; String ch; do { nf=Integer.parseInt(dis.readLine()); if(nf<=rws-1) { for(i=1;i<=nf;i++) { rptr=++rptr%8; buf[rptr]=dis.readLine(); System.out.println("frame"+rptr+""+buf[rptr]); } rws=rws-nf; System.out.println("Acknowledge sent"); p.println(rptr+1); rws=rws+nf; } else break; ch=dis.readLine(); } while(ch.equals("y")); } } OUTPUT: D:\java\jdk1.5\bin>javac sender.java D:\java\jdk1.5\bin>java sender. Enter the no. of frames 5 Enter 5Message hai hello java welcomes you acknowledgement received ACK5 Do you want to send more frames n D:\java\jdk1.5\bin> D:\java\jdk1.5\bin>javac receiver.java D:\java\jdk1.5\bin>java receiver frame0hai frame1hello frame2java frame3welcomes frame4you acknowledge sent D:\java\jdk1.5\bin> RESULT: Thus the program of sliding window protocol using java was executed successfully. EX NO : 10 DATE : IMPLEMENTATION OF SHORTEST PATH ROUTING ALGORITHM AIM: To implement shortest path routing algorithm using java. ALGORITHM: STEP 1 : Start the process STEP 2 : Declare the initialize the variables. STEP 3 : Provide the distance vector table. STEP 4 : Given a host, find the shortest path through the routers to the destination. STEP 5 : Terminate the process. PROGRAM import java.io.*; class shortest { int cost[][]=new int[10][10]; boolean known[]=new boolean[10]; int d[]=new int[10]; int p[]=new int[10]; String route=new String(); int n,s; DataInputStream dis=new DataInputStream(System.in); void initialize() throws IOException { int i,j; System.out.println("Enter the source"); s=Integer.parseInt(dis.readLine()); System.out.println("Enter the no. of hosts"); n=Integer.parseInt(dis.readLine()); System.out.println("Enter the distance table"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) cost[i][j]=Integer.parseInt(dis.readLine()); for(i=1;i<=n;i++) { known[i]=false; p[i]=0; d[i]=99; } } void shortpath() { int nh=1,h=1,i,min; d[s]=0; p[s]=0; while(nh<=n) { min=999; for(i=1;i<=n;i++) { if(d[i]<min && !known[i]) { min=d[i]; h=i; } } known[h]=true; for(i=1;i<=n;i++) { if(cost[h][i]!=0) if(!known[i]&&(d[h]+cost[h][i])<d[i]) { d[i]=d[h]+cost[h][i]; p[i]=h; } } nh++; } } void display() { int j=0; System.out.println("Shortest route from H"+s+"to all hosts\n\n"); System.out.println("Host Distance Route\n"); for(int i=1;i<=n;i++) { System.out.print("H"+i+"\t\t"+d[i]+"\t\t"); j=p[i]; if(i!=s) route="H"+Integer.toString(p[i]); else route=Integer.toString(p[i]); while(p[j]!=0) { route="H"+Integer.toString(p[j])+""+route; j=p[j]; } System.out.println(route); System.out.println("\n"); } } public static void main(String a[]) throws IOException { shortest s=new shortest(); s.initialize(); s.shortpath(); s.display(); } } OUTPUT: Enter the no. of hosts 5 Enter the distance table 2 4 6 8 0 4 7 3 4 3 12 5 8 4 0 1 8 0 3 7 2 4 1 5 7 Shortest route from H1to all hosts Host Distance Route H1 0 0 H2 4 H1 H3 6 H1 H4 8 H1 H5 7 H1H2 RESULT: Thus the implementation of shortest path routing algorithm using java was executed successfully.