6 Replies Latest reply on Oct 6, 2006 6:18 AM by 791266

    multiple client chat server

      Thanks to the help I got from the good people of this forum today I modified the chat server program which I am making and now it is working a bit better. Right now it can accept several (up to 8) clients and store their open sockets in a SocketCollection list which can later be accessed so that messages get sent to all of them. Problem is that when I send a message, it is displayed instantaneously on the client that sent that message (my machine), but is displayed only after the other clients press enter on the other clients' machines (for testing purposes it was the same machine, different port opened for communication with server which is also on the same machine). To clarify that - all clients and server are the same machine (that shouldn't be a problem, right?). Code for printing incoming data for client program is below:
      while ((fromServer = in.readLine()) != null) {
                  if (fromServer!=null){
                         System.out.println("print on client screen msg received from server");
                  fromUser = stdIn.readLine();
                  if (fromUser != null) {
                System.out.println("Client: " + fromUser);
      Code to deliver the message to all clients from the server application is below as well:
      while ((inputLine = in.readLine()) != null) {
                       PrintWriter multiOut;
                     for (int x=0; x<8; x++){
                           System.out.println("INSIDE MULTI-TELL FOR LOOP");
                           if (socketCollection[x]!=null){
                                 Socket c=socketCollection[x];
                                     System.out.println("tried to send to all\nSocket c = " + c);
                                     out=new PrintWriter(c.getOutputStream(), true);
                                         out.println(c.getInetAddress() + inputLine);
                                  catch(IOException ioe){}
      In the server's DOS window I can clearly see that it enters to display the message to multiple clients and it should send it. The sockets are all different and they point to different ports on different clients' machines. Maybe the problem is just in the client program's code. If you could help me out, it will be greatly appreciated. Thank you very much.
        • 1. Re: multiple client chat server
          How are you reading the 8 different sockets on the server side? What is in?
          • 2. Re: multiple client chat server
            The sockets get created one by one when each client connects. Afterwards when the thread is made, that socket gets copied into the SocketCollection array which can afterwards be accessed in order for the server to distribute the message to all its connected clients.

            The for loop in the second code example where it takes SocketCollection[x] and gets its outputStream and all that is where it prints the message and sends it to each client (sends it but it is not displayed on the client side until the client sends a message to the server again). My question is how to make it so the client does not have to send a message in order to see the server message (message to all clients) that was sent before.
            • 3. Re: multiple client chat server
              Okay right here's the problem.

              You are only reading one Socket (on the server) at a time. That method
              is blocking. So you have to have it be unblocked for the server to try
              and read the next socket. And so on.
              • 4. Re: multiple client chat server
                How do I do that? I don't have a lot of experience with networking so that is why I've kind of flooded the forums with my questions over the past few days :D

                This sending out of mass message occurs inside the run method in myThread class (extends Thread) which is created when the server accepts an incoming client connection.
                • 5. Re: multiple client chat server
                  How do I do that? I don't have a lot of experience
                  with networking
                  This isn't a networking problem any more. This is a threading and
                  program flow issue. Namely you have a burning need for multiple
                  listening threads (one for each Socket).

                  Take a look at this http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
                  • 6. Re: multiple client chat server
                    Take a look at this
                    Nice to see another Swede who answers quesions. Welcome :)