...
Code Block | ||
---|---|---|
| ||
// Exception handling has been omitted for the sake of brevity class EchoServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { 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); } } finally { try { serverSocket.close(); } catch (IOException x) { System.err.println(x.toString()); } } } } class EchoClient { public static void main(String[] args) throws UnknownHostException, IOException { Socket socket = null; try { 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()); } } finally { try { socket.close(); } catch (IOException x) { System.err.println(x.toString()); } } } } |
Note that the sockets are closed in accordance with ERR05-J. Do not let checked exceptions escape from a finally block. While merely printing close exceptions is frowned upon, the exceptions may be suppresed as per EX0 of ERR00-J. Do not suppress or ignore checked exceptions.
...
Code Block | ||
---|---|---|
| ||
// Exception handling has been omitted for the sake of brevity class EchoServer { public static void main(String[] args) throws IOException { SSLServerSocket sslServerSocket = null; try { SSLServerSocketFactory sslServerSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); 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); } } finally { try { sslServerSocket.close(); } catch (IOException x) { System.err.println(x.toString()); } } } } class EchoClient { public static void main(String[] args) throws IOException { SSLSocket sslSocket = null; try { SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); 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()); } } finally { try { sslSocket.close(); } catch (IOException x) { System.err.println(x.toString()); } } } } |
Note that a program that makes use of SSLSockets
will block indefinitely if it tries to connect to a port that is not using SSL. Similarly, a program that does not use SSLSockets
will block when trying to establish a connection through a port that uses SSL.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9790f9a5ddc43221-a79713b9-47b84632-a963b4ec-bfb4970c62f37bfa49bc6f4f"><ac:plain-text-body><![CDATA[ | [[MITRE 2009 | AA. Bibliography#MITRE 09]] | [CWE ID 311 | http://cwe.mitre.org/data/definitions/311.html] "Failure to Encrypt Sensitive Data" | ]]></ac:plain-text-body></ac:structured-macro> |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="beb49ce84e0f735f-f2d81c50-47674a8b-94eb85fc-9d9c9fe159a20360c8f43381"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="45123a750d860b9d-4dfa26c4-45194be7-9a71b0f5-ace28a216a4c43a2fb538145"><ac:plain-text-body><![CDATA[ | [[Gong 2003 | AA. Bibliography#Gong 03]] | 11.3.3 "Securing RMI Communications" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="26e7ba3855ecd202-01d6290f-42bd4607-a4228c1f-fb1e38654b2197638851ee3d"><ac:plain-text-body><![CDATA[ | [[Ware 2008 | AA. Bibliography#Ware 08]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
...