...
Code Block | ||
---|---|---|
| ||
// Exception handling has been omitted for the sake of brevity class EchoServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(10007); Socket socket = serverSocket.accept(); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); out.println(inputLine); } // close out, in, socket (preferably in a try-finally block) } } class EchoClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost", 9999); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println(in.readLine()); } // close out, in, stdIn, socket (preferably in a try-finally block) } } |
...
Code Block | ||
---|---|---|
| ||
// Exception handling has been omitted for the sake of brevity class EchoServer { public static void main(String[] args) throws IOException { SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(9999); SSLSocket sslsocket = (SSLSocket) sslserversocket.accept(); PrintWriter out = new PrintWriter( sslsocket.getOutputStream(),true); BufferedReader in = new BufferedReader( new InputStreamReader( sslsocket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); out.println(inputLine); } // close out, in, sslsocket (preferably in a try-finally block) } } class EchoClient { public static void main(String[] args) throws IOException { SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999); PrintWriter out = new PrintWriter(sslsocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(sslsocket.getInputStream())); BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println(in.readLine()); } // close out, in, stdIn, sslsocket (preferably in a try-finally block) } } |
...