From e901ae57f573e9c538596f04bd08c297928c7bd7 Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@nps.edu> Date: Tue, 31 Jul 2018 21:15:47 -0700 Subject: [PATCH] reformat, improve exception to silence warning --- .../TcpServer/src/tcpserver/TcpServer.java | 129 +++++++++--------- 1 file changed, 61 insertions(+), 68 deletions(-) diff --git a/projects/TcpExample3/Server/TcpServer/src/tcpserver/TcpServer.java b/projects/TcpExample3/Server/TcpServer/src/tcpserver/TcpServer.java index 547ad93bd0..9d0ca372d1 100644 --- a/projects/TcpExample3/Server/TcpServer/src/tcpserver/TcpServer.java +++ b/projects/TcpExample3/Server/TcpServer/src/tcpserver/TcpServer.java @@ -4,80 +4,73 @@ import java.io.*; import java.net.*; /** - * Very slightly more complex than example1. A complete copy of - * example 2. The only thing this does - * differently is introduce a loop into the response, so you don't - * have to restart the program after one response. Also, it prints - * out the socket pair the server sees. Run the program via telnet - * several times and compare the socket pairs. - * + * Very slightly more complex than example1. A complete copy of example 2. The + * only thing this does differently is introduce a loop into the response, so + * you don't have to restart the program after one response. Also, it prints out + * the socket pair the server sees. Run the program via telnet several times and + * compare the socket pairs. + * * telnet localhost 2317 - * - * If you're sophisticated you can contact the instructor's computer - * while running this program. - * - * telnet <ipOfServersLaptop> 2317 - * + * + * If you're sophisticated you can contact the instructor's computer while + * running this program. + * + * telnet [ipNumberOfServerLaptop] 2317 + * * And have him display the socket pairs he got. + * * @author mcgredo */ -public class TcpServer -{ +public class TcpServer { - public static void main(String[] args) - { - try - { - // ServerSocket waits for a connection from a client. - // Notice that it is outside the loop; ServerSocket - // needs to be made only once. - - ServerSocket serverSocket = new ServerSocket(2317); + @SuppressWarnings("ConvertToTryWithResources") + public static void main(String[] args) + { + try { + // ServerSocket waits for a connection from a client. + // Notice that it is outside the loop; ServerSocket + // needs to be made only once. - // Loop, infinitely, waiting for client connections. - // Stop the program somewhere else. - while(true) - { - Socket clientConnection = serverSocket.accept(); // block until connected - OutputStream os = clientConnection.getOutputStream(); - PrintStream ps = new PrintStream(os); + ServerSocket serverSocket = new ServerSocket(2317); - ps.println("This was written by the server"); - - // Print some information locally about the Socket - // connection. This includes the port and IP numbers - // on both sides (the socket pair.) - - InetAddress localAddress = clientConnection.getLocalAddress(); - InetAddress remoteAddress = clientConnection.getInetAddress(); - - int localPort = clientConnection.getLocalPort(); - int remotePort = clientConnection.getPort(); - - // My socket pair connection looks like this, to localhost: - // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) - // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) - // - // Why is the first IP/port the same, while the second set has - // different ports? + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while (true) + { + Socket clientConnection = serverSocket.accept(); // block until connected + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + ps.println("This was written by the server"); - System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + - remoteAddress.toString() + ", " + remotePort + " ))"); - - // Notice the use of flush() and close(). Without - // the close() to Socket object may stay open for - // a while after the client has stopped needing this - // connection. Close() explicitly ends the connection. - ps.flush(); - clientConnection.close(); // like it or not, you're outta here! - } - } - catch(Exception e) - { - System.out.println("problem with networking"); - } - - } - -} \ No newline at end of file + // Print some information locally about the Socket + // connection. This includes the port and IP numbers + // on both sides (the socket pair.) + InetAddress localAddress = clientConnection.getLocalAddress(); + InetAddress remoteAddress = clientConnection.getInetAddress(); + + int localPort = clientConnection.getLocalPort(); + int remotePort = clientConnection.getPort(); + + // My socket pair connection looks like this, to localhost: + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) + // + // Why is the first IP/port the same, while the second set has + // different ports? + System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); + + // Notice the use of flush() and close(). Without + // the close() to Socket object may stay open for + // a while after the client has stopped needing this + // connection. Close() explicitly ends the connection. + ps.flush(); + clientConnection.close(); // like it or not, you're outta here! + } + } + catch (IOException e) { + System.out.println("problem with networking"); + } + } +} -- GitLab