Question

I try to get a connection to multiple clients using the Sockets in Java. Everything seems to work, but the problem is, that the server just listens to the first client. If there are multiple clients, the server can send them all messages, but he can just listen to the messages that came from the first client. I tried this all out (I'm at this problem since yesterday). So I'm pretty sure, that the fault has to be in the class "ClientListener".

Explanation: There is a List with clients (connection to communicate with Strings). In the GUI there is a list, where I can choose, with which client I'd like to communicate. If I change the client, the variable currentClient (int) switches to another number

networkClients is an ArrayList, where all the different connections are "stored".

The first connected client is exactly the same as the other clients, there is nothing special about him. He is called, when the variable currentClient is set to 0 (per default). The variable-switching is working. Like I said, all the clients give me a response if I send them an order, but just networkClients.get(0) is heard by the server (ClientListener).

class ClientListener implements Runnable {
String request;

@Override
public void run() {
  try {
    while (networkClients.size() < 1) {
      Thread.sleep(1000);
    }

    //***I'm pretty sure, that the problem is in this line
    while ((request = networkClients.get(currentClient).getCommunicationReader().readLine()) != null) {
    //***
       myFileList.add(new MyFile(request));
    }
   }
   } catch (Exception e) {
      e.printStackTrace();
   }
}

I hope someone can help me. I tried many things, but nothing worked.

EDIT: Like I wrote in the code example, is it possible that the while-loop isn't able to switch the number of "currentClient" (which is handled by another Thread)? I tested/simulated something similar in a testclass and the result was, that a while-loop of course can can update the state in it (meaning, that if a variable changes in the () of a while loop, it will of course be checked after every repeat).

Was it helpful?

Solution 2

I found the solution:

The Thread sits in the declared method I mentioned in the starting post (in the code snippet) and waits unlimited time for a new response of the client. So changing the index of the list "networkClients" won't do anything, because nothing will happen there, until there is a new order sent by the client (which lets the thread go further).

So you need to implement an extra listener for each client.

OTHER TIPS

You should take a look at multithreading.

Your server program should be made out of:

- The main thread
- A thread that handles new connections.
(Upon creating a new connection, start a new thread and pass the connection on to that thread)
- A thread for each connected client, listening to the each client separately

Take a look at some examples like: (1) (2)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top