0

I'm trying to develop a java chat server using thread pool, but i don't know how to handle incoming message from clients. i've think to save every socket connection in a hashmap and add the task to the queue of thread pool.. but how the server can know when he's receveing a message from a client without instantiate a bufferedreader?

2 Answers2

0

Your server will need to use agent objects that hold a BufferedReader that reads from their socket. Perhaps you will need to create a collection of these agent objects.

For example,

class ServerAgent implements Runnable {
   private OutputStream out;
   private BufferedReader br;

   public ServerAgent(Socket clientSocket) throws IOException {
      out = clientSocket.getOutputStream();
      br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

      // ....
   }


   @Override
   public void run() {
      // TODO finish code that reads from br, BufferedReader

   }
}

And your Server could have code like:

while (true) {
   Socket clientSocket = server.accept();
   futureList.add(threadPoolExector.submit(new ServerAgent(clientSocket)));
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • sorry, i don't know what agent objects are. i had google it but i don't have found nothing. could you pass me a guide or something? –  Sep 15 '13 at 19:24
  • so, if i'm understanding, i should not use a "sockets list" but when a client want to send a message to an other client, the server recreate the socket connection, right? –  Sep 16 '13 at 15:55
  • @Daniele: think on it. What good would a list of Sockets be? Not much. – Hovercraft Full Of Eels Sep 16 '13 at 16:15
  • ok, but with this implementation the server should accept many time also a single client, my question is: to accept a client is very expensive (for the cpu and the memory)? –  Sep 16 '13 at 16:31
  • @Daniele: To find it's expense, you will need to profile things. I don't think that the "cost" of the client will be your issue though. More important is having a program that works. – Hovercraft Full Of Eels Sep 16 '13 at 16:37
  • i mean that if i'm using thread pool, i'm also interested to the performance of the server. i would like to know the best way to implement a chat server :) –  Sep 16 '13 at 16:56
  • @Daniele: to know performance, you profile. Else you're doing premature optimization, which is what your current discussion is all about. You could try to do what ns47731 is suggesting and have a very efficient server that doesn't work, or you can create one that works. Your choice. – Hovercraft Full Of Eels Sep 16 '13 at 17:00
0

You dont need to initialize a buffered reader for each one of your sockets. You can look through and check if there is data waiting to be read.

for(Socket socket : socketsList)
    if(socket.getInputStream().available() > 0) {
        // you have data to be read from this socket
    }
ug_
  • 11,267
  • 2
  • 35
  • 52
  • cool. but a thread that keeps to iterate the sockets list it's a very expensive operation for the cpu, isn't it? –  Sep 15 '13 at 19:35
  • I wouldn't say so, you can combine it with some other basic logic like pinging out messages. So when you receive a message from this socket you also have to send it back out to everyone else. You can combine this logic and the other to make one send/receive thread. – ug_ Sep 15 '13 at 19:38
  • i don't know by but "available" method always returns 0 –  Sep 16 '13 at 15:40
  • Sorry I didn't check that the sockets input stream would .available() would be implemented check out the answer to this post http://stackoverflow.com/questions/5826198/inputstream-available-is-0-always – ug_ Sep 16 '13 at 15:46
  • 1
    solved with "avaliable" method from "PushbackInputStream" class –  Sep 16 '13 at 17:27