Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should have just one <code>ServerSocket</code> listening on a port that is known to the client. When a client connects to the server, a new <code>Socket</code> object is created and the original <code>ServerSocket</code> goes back to listening again. You should then spin off a new <code>Thread</code> or hand over to an <code>Executor</code> the actual work of talking to the client, otherwise your server will stop listening for client connections.</p> <p>Here is a <em>very</em> basic sketch of the code you will need.</p> <pre><code>import java.net.*; import java.util.concurrent.*; public class CoordinateServer { public static void main(String... argv) throws Exception { // 'port' is known to the server and the client int port = Integer.valueOf(argv[0]); ServerSocket ss = new ServerSocket(port); // You should decide what the best type of service is here ExecutorService es = Executors.newCachedThreadPool (); // How will you decide to shut the server down? while (true) { // Blocks until a client connects, returns the new socket // to use to talk to the client Socket s = ss.accept (); // CoordinateOutputter is a class that implements Runnable // and sends co-ordinates to a given socket; it's also // responsible for cleaning up the socket and any other // resources when the client leaves es.submit(new CoordinateOutputter(s)); } } } </code></pre> <p>I have put sockets here since they are easier to get started with, but once you have this working well and want to boost your performance you will probably want to investigate the <code>java.nio.channels</code> package. There's a good tutorial <a href="https://www.ibm.com/developerworks/java/tutorials/j-nio/" rel="noreferrer">over at IBM</a>.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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