Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should keep a thread always executing <code>socket.accept()</code>. That is:</p> <ul> <li>Whenever you receive a client (via <code>socket.accept()</code>), you should <ul> <li>Start a new <code>Thread</code>, that will from now on take care of that client. (I'll call that <code>Thread</code> extending class <code>ClientNanny</code>);</li> <li>and then get back to run <code>socket.accept()</code> again.</li> </ul></li> </ul> <p>This is what your <code>waitForConnection()</code> method shoud look like:</p> <pre><code>private List&lt;ClientNanny&gt; clients = new ArrayList&lt;ClientNanny&gt;(); private void waitForConnection() throws IOException { while (true) { /* or some other condition you wish */ connection = server.accept(); /* will wait here */ /* this code is executed when a client connects... */ showMessage("\nNow connected to " + connection.getInetAddress().toString()); ClientNanny nanny = new ClientNanny(connection); /* call a nanny to take care of it */ clients.add(nanny); /* make sure you keep a ref to it, just in case */ nanny.start(); /* tell nanny to get to work as an independent thread */ clientCount += 1; /* you dont need this: use clients.size() now */ } } </code></pre> <p>In case you didn't notice, <code>ClientNanny</code> should be something like:</p> <pre><code>class ClientNanny extends Thread { ClientNanny(Socket baby) { this.myBaby = baby; } @Override public void run() { bed.put(myBaby); /* and probably other commands :) */ } } </code></pre> <p>Now, you should notice that the highlighted code below...</p> <pre><code>try { showMessage("Waiting For a Player To Connect..."); waitForConnection(); setupStreams(); /* &lt;--------- THIS LINE! HI! I'M A HIGHLIGHT! */ whileChatting(); /* &lt;--------- THIS LINE! HI! I'M A HIGHLIGHT TOO! */ } </code></pre> <p>Will not get executed when you change <code>waitForConnection()</code> as I suggested (as <code>waitForConnection()</code> will keep on rolling inside the <code>while(true)</code> loop). Maybe you could put the <code>socket.accept()</code> on a thread of its own... Anyway, I trust you can take it from now on, yeah?</p> <p><sup>PS.: As a final pointer, the solution above uses <code>new Thread()</code> (or <code>new ClientNanny()</code> for that matter) as it is easiest way to explain your problem. The very very optimal solution, though, might involve using an <code>ExecutorService</code> (roughly, a thread pool), as quickly pointed out in <a href="https://stackoverflow.com/a/12017742/1850609">this answer</a>.</sup></p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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