3

I am creating a server class for a little Instant Messaging app. Everything is working when I try to have the server connect to a single client, but I am attempting to expand it be connect to multiple clients. I have tested and it is able to connect to multiple clients, but the problem lies with the fact that I cannot read inputs from all of them at the same time.

Do I need to open a new thread for each client to constantly be checking for inputs? I feel like this is extremely inefficient.

This is the code that checks for inputs from the clients, which are stored in an Arraylist at the moment. I believe that it is similar to a scanner, where it does not continue the code until there is an input.

private void chat() throws IOException{
    String message = " You are now connected! ";
    sendMessage(message);
    ableToType(true);
    do{
        try{
                //right now it is only checking the first (0) client 
                message = (String) inputs.get(0).readObject(); // this is the line that checks for inputs
            showMessage("\n" + message, new java.awt.Color(20, 124, 34));
        }catch(ClassNotFoundException classNotFoundException){
            showMessage("\nUnreadable Message Sent by Client");
        }
    }while(!message.contains("END"));

}
Slushy_G
  • 137
  • 2
  • 9

1 Answers1

1

The simple solution is: you dedicate one complete thread on your server ... per client.

But as you already figured: that is rather inefficient - as most of your threads will be idling most of the time. And even in 2016; threads are still "expensive"; meaning that you don't want to waste them.

Thus you could be looking into solutions that use pooling. Meaning: your server has a pool of n threads, that together work on m clients (where m could be much bigger than n). One starting point could be this tutorial.

But: if you are doing this for "learning purposes"; then I suggest: go with the "one thread per client" approach first. You see, it will be a challenging task to get that kind of implementation to work correctly (multi-threading quickly translates to multi-probleming). Solve that puzzle first; and only then go to the next level by using pools!

You see: in a "learning" setup with one server and a handful of clients, it really doesn't matter if you have 5 threads or 10 on your local machine. Pooling becomes interesting when we talk about values of m way beyond 100, 500, .. and given how your current code looks like; I assume all of that is "playing around" anyway. So start playing softball before going hardball.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • And thanks to the anonymous downvoter. Any comments on how should improve my question in your opinion? – GhostCat Oct 08 '16 at 19:29
  • Thanks for the quick response. I think that I will just open a new thread for each client and limit the number of clients because I don't really need to have more than a few clients connected. That tutorial looks very helpful, and I will use it in the future if I need to expand this further. – Slushy_G Oct 08 '16 at 19:36