Note that there are some explanatory texts on larger screens.

plurals
  1. POHandle exceptions without crashing socket server?
    primarykey
    data
    text
    <p>I am attempting to run a threaded socket server that handles multiple clients simultaneously. However after I telnet into the server and then exit in a non-graceful way, say by closing the window. </p> <p>The server crashes and returns <code>System.out.println("Runnable terminating with exception: " + e );</code> with <code>e</code> being <code>java.Lang.NullPointerException</code>.</p> <p><strong>My question is how can I simply close the socket and keep the server running even if something goes wrong in <code>handleSession()</code>, so that others can connect?</strong> </p> <p>I am new to exceptions so my understand is still elementary.</p> <pre><code>publc class ThreadedHandler implements Runnable { Socket incoming; BufferedReader in; PrintWriter out; SortedTopicList topics; ThreadedHandler(Socket s) { incoming = s; } public void run() { try { handleSession(incoming); }catch (Exception e) { System.out.println("Runnable terminating with exception: " + e ); } } public void handleSession(Socket client) { try { //Code goes here } catch (IOException e) { System.err.println(e.getMessage()); } finally { shutdown(); } } public void shutdown() { try { in.close(); out.close(); incoming.close(); } catch (Exception e) { System.err.println(e.getMessage()); } } } </code></pre> <p>My main method is as follows:</p> <pre><code>public class MessageBoardServer { public static void main(String[] args) { Thread t; try { ServerSocket ss = new ServerSocket(118118); while(true) { Socket session = ss.accept(); t = new Thread(new ThreadedHandler(session)); t.start(); } } catch (Exception e) { System.err.println(e.getMessage()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload