Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok people. Thanks to aoi222 and Kely French mainly, but thanks to anyone else. I found the solution which is:</p> <p>1.- Obviously you need to create a thread that serves a client exclusively. First time the client connects to the server to a general port. This create a thread listening to a particular port and let the client know which the new port, the new thread will be listening to. Then the client change its port and create a new socket to start the conversation with the client.</p> <p>2.- The server, running as a separate thread, must keep listening to a loop as aoi222 indicated for ever. The accept is just out of the loop for the exclusive thread which has been assigned a client. Please see the code below and sorry for the name of the variables as they are a bit confusing:</p> <pre><code> fromClient = server.accept(); clientInput = new ObjectInputStream (fromClient.getInputStream()); serverOutput = new ObjectOutputStream (fromClient.getOutputStream()); Hello received; while ( (received = (Hello)clientInput.readObject()) != null ) { received.setHello("pepito grillo"); serverOutput.writeObject(received); } </code></pre> <p>3.- Please see that the main thread server DO need to have the server.accept within the loop in order to accept new clients connection</p> <pre><code> for (;;) { newClient = mainServer.accept(); new exclusiveThread ().start () } </code></pre> <p>"exclusiveThread" is a thread that implements the code in 2.</p> <p><strong>Edited:</strong> I forgot to say that the client must send a end message for the server to identify that the conversation has finished and stop the lopp. Otherwise it will result in a java.io.EOFException.</p> <p>Thanks guys a lot for your help.</p>
 

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