Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that the declaration of <code>Socket fromClient = server.accept();</code> on the server is inside the scope of the <code>for</code> loop. So at the end of the <code>for</code> loop (after <code>serverOutput.flush();</code>) the <code>fromClient</code> variable is destroyed and a new one is created the next time the loop begins. You will need to restructure it like this if you want the same connection to be able to send/receive multiple messages:</p> <pre><code>import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.HashMap public class User extends Runnable { public String username; public Socket socket; public TestServer testServerReference; ObjectInputStream clientInput; ObjectOutputStream serverOutput; public User(Socket s, TestServer tS) { socket = s; testServerReference = tS; } /** * Creates the in/out Streams, returns the username */ public String init() { serverOutput = new ObjectOutputStream (fromClient.getOutputStream()); clientInput = new ObjectInputStream (fromClient.getInputStream()); this.username = (String)clientInput.readObject(); //the first thing the client should send is a username } public void run() { try { //add send/receive logic here for connected client } catch (Exception ex) { } finally { testServerReference.removeUser(username); } } } public class TestServer { public static HashMap&lt;String,User&gt; userList = new HashMap&lt;String,User&gt;(); public static void main(String[] args) { try { ServerSocket server = new ServerSocket(5555, 100); while (true) //start accepting new connections { System.out.println ("Listening for connections"); Socket fromClient = server.accept(); User newUser = new User(fromClient,this); String newUsername = newUser.init(); userList.add(newUsername,newUser); (new Thread(newUser)).start(); } } catch (ClassNotFoundException ex) { Logger.getLogger(TestServer.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(TestServer.class.getName()).log(Level.SEVERE, null, ex); } } public static void removeUser(String username) { userList.remove(username); } } </code></pre> <p>I just threw this code together really quickly and have not yet test compiled it, so it may still contain a couple errors, but the general idea is there.</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.
 

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