Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Echo Client&Server Application EchoClient import java.net.*; import java.io.*; class EchoClient { public static void main(String[] args) throws IOException { if (args.length < 2) { System.err.println("Usage: java EchoClient <IP address> <Port number>"); System.exit(0); } BufferedReader in = null; PrintWriter out = null; BufferedReader fromUser = null; Socket sock = null; try { sock = new Socket(args[0], Integer.parseInt(args[1])); // set up the necessary communication channels in = new BufferedReader(new InputStreamReader(sock.getInputStream())); fromUser = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(sock.getOutputStream(),true); while (true) { String line = fromUser.readLine(); if (line == null || line.equals("bye")) { out.println("bye"); break; } out.println(line); System.out.println(in.readLine()); } } catch (IOException ioe) { System.err.println(ioe); } finally { if (in != null) in.close(); if (fromUser != null) fromUser.close(); if (out != null) out.close(); if (sock != null) sock.close(); } } } EchoServer: import java.net.*; import java.io.*; public class EchoServer { public static void main(String[] args) throws IOException { if (args.length < 1) { System.err.println("Usage: java EchoServer <Port Number> "); System.exit(0); } ServerSocket sock = null; try { // establish the socket sock = new ServerSocket(Integer.parseInt(args[0])); /** * listen for new connection requests. * when a request arrives, service it * and resume listening for more requests. */ while (true) { // now listen for connections Socket client = sock.accept(); // service the connection ServiceConnection(client); } } catch (IOException ioe) { System.err.println(ioe); } finally { if (sock != null) sock.close(); } } public static void ServiceConnection(Socket client) { BufferedReader networkBin = null; OutputStreamWriter networkPout = null; try { /** * get the input and output streams associated with the socket. */ networkBin = new BufferedReader(new InputStreamReader(client.getInputStream())); networkPout = new OutputStreamWriter(client.getOutputStream()); /** * the following successively reads from the input stream and returns * what was read. The loop terminates with ^D or the string "bye\r\n" * from the input stream. */ while (true) { String line = networkBin.readLine(); if ( (line == null) || line.equals("bye")) { break; } networkPout.write("Server [ "+line+" ]\r\n"); networkPout.flush(); } } catch (IOException ioe) { System.err.println(ioe); } finally { try { if (networkBin != null) networkBin.close(); if (networkPout != null) networkPout.close(); if (client != null) client.close(); } catch (IOException ioee) { System.err.println(ioee); } } // end try } // end ServiceConnection } 1. Program Execution Echo Client Server Compilation javac EchoServer.java javac EchoClient.java Execution java EchoServer portno(123) java EchoClient ipaddrofserver(localhost) portno(123) the character must typed by the client must reflect back at the client....until the client quits by typing 'bye' REMOTE COMMAND EXECUTION RceClient import java.net.*; import java.io.*; class RceClient { public static void main(String args[]) { try { DatagramSocket ds=new DatagramSocket(2000); InetAddress ia=InetAddress.getByName("127.0.0.1"); byte[] msg=new byte[255]; DataInputStream dis = new DataInputStream(System.in); String ss = dis.readLine(); msg = ss.getBytes(); DatagramPacket dp = new DatagramPacket(msg,msg.length,ia,1000); ds.send(dp); } catch(Exception e) { } } } RceServer import java.io.*; import java.net.*; class RceServer { public static void main(String args[]) { try { DatagramSocket ds = new DatagramSocket(1000); byte[] msg = new byte[255]; DatagramPacket dp = new DatagramPacket(msg,msg.length); ds.receive(dp); String ss = new String(msg); System.out.println("The Path is:" + ss); Runtime rt = Runtime.getRuntime(); Process p = null; p = rt.exec(ss); } catch(Exception e) { } } } Program Execution Compilation javac RceServer.java javac RceClient.java Execution java RceServer java RceClient mspaint.exe Chat Application //Client Application import java.net.*; import java.io.*; public class Client implements Runnable { Socket s; BufferedReader br; BufferedWriter bw; BufferedReader ppp; String input = null; public static void main(String arg[]) { new Client(); } public void run() { try{ s.setSoTimeout(1); } catch(Exception e){} while (true) { try{ System.out.println("Server: "+br.readLine()); } catch (Exception h){} } } public Client() { try { s = new Socket("127.0.0.1",100); br = new BufferedReader(new InputStreamReader(s.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); ppp = new BufferedReader(new InputStreamReader(System.in)); Thread th; th = new Thread(this); th.start(); bw.write("Hello"); bw.newLine(); bw.flush(); while(true){ input = ppp.readLine(); bw.write(input); bw.newLine(); bw.flush(); } } catch(Exception e){} } } //Server Application import java.net.*; import java.io.*; public class ServerApp implements Runnable { ServerSocket s; Socket s1; BufferedReader br; BufferedWriter bw; BufferedReader ppp; String input = null; public void run() { try{ s1.setSoTimeout(1); } catch(Exception e){} while (true) { try{ System.out.println("Client: "+br.readLine()); } catch (Exception h){} } } public static void main(String arg[]) { new ServerApp(); } public ServerApp() { try{ s = new ServerSocket(100); s1=s.accept(); br = new BufferedReader(new InputStreamReader(s1.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream())); ppp = new BufferedReader(new InputStreamReader(System.in)); Thread th; th = new Thread(this); th.start(); while(true){ input = ppp.readLine(); bw.write(input); bw.newLine(); bw.flush(); } }catch(Exception e){} } } Program Execution Chat Application Compilation javac ServerApp.java javac Client.java Execution Z:\NetworkLab\Chat>java ServerApp Client: Hello Client: hjsdfsdf dsflkfl Z:\NetworkLab\Chat>java Client hjsdfsdf Server: dsflkfl Sdffsa REMOTE COMMAND EXECUTION RceClient import java.net.*; import java.io.*; class RceClient { public static void main(String args[]) { try { DatagramSocket ds=new DatagramSocket(2000); InetAddress ia=InetAddress.getByName("127.0.0.1"); byte[] msg=new byte[255]; DataInputStream dis = new DataInputStream(System.in); String ss = dis.readLine(); msg = ss.getBytes(); DatagramPacket dp = new DatagramPacket(msg,msg.length,ia,1000); ds.send(dp); } catch(Exception e) { } } } RceServer import java.io.*; import java.net.*; class RceServer { public static void main(String args[]) { try { DatagramSocket ds = new DatagramSocket(1000); byte[] msg = new byte[255]; DatagramPacket dp = new DatagramPacket(msg,msg.length); ds.receive(dp); String ss = new String(msg); System.out.println("The Path is:" + ss); Runtime rt = Runtime.getRuntime(); Process p = null; p = rt.exec(ss); } catch(Exception e) { } } } Program Execution Compilation javac RceServer.java javac RceClient.java Execution java RceServer java RceClient mspaint.exe