0
votes

I have created a simple server class and a simple client class. I have two client programs that connect to the server. The client & server class can send & receive messages.

The problem is that when both clients connect to the server, one of the clients can send multiple messages to the server whilst the other client can only send one message to the server.

Here is the constructor for my Server class:

   public MyServer () {
      setUpConnection ();
   }

Here is the code that connects the client to a port:

   public void setUpConnection () {
     clientOutputStreams = new ArrayList ();
     try {
       serverSocket = new ServerSocket (5000);
     while (true) {
       Socket clientSocket = serverSocket.accept ();
       printWriter = new PrintWriter (clientSocket.getOutputStream ());
       clientOutputStreams.add (printWriter);

       Thread thread = new Thread (new clientHandler (clientSocket));
       thread.start();

       System.out.println ("Client is connected to server");
     }    
    } catch (Exception e) {
       e.printStackTrace ();
    }
  }

This is the code that sends the messages received by the server to other clients:

    public void tell everyone (String message) {
     Iterator outputObject = clientOutputStreams.iterator();

     while (outputObject.hasNext()) {
        try {
            PrintWriter writer = (PrintWriter) outputObject.next();
            writer.println (message);
            writer.flush ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
     }
 }

This is the code that runs in a separate thread that listens for messages sent by clients:

    public class clientHandler implements Runnable {

       Socket clientSocket;

       public clientHandler (Socket socket) {
          try {
           clientSocket = socket;
           isr = new InputStreamReader (clientSocket.getInputStream());
           reader = new BufferedReader (isr); 
          } catch (Exception e) {
          e.printStackTrace ();
       }
    }

    public void run () {
        String message;

      try {
        while ((message = reader.readLine()) != null) {
            System.out.println ("I recieved message: " + message);
            tellEveryone (message);
        }  
      } catch (Exception e) {
          e.printStackTrace();
      }
     }
    }
 }

Here is the constructor for my client class:

   public MyClient() {
      initComponents();
      setUpConnection ();
      Thread thread = new Thread (new recievingMessages ());
      thread.start ();
      jtaRec.setEditable (false);
      jtfSend.addFocusListener (this);
      jtfSend.requestFocus();
   }                

Here is the code that runs when the client presses the send button:

private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    try {
        writer.println (jtfSend.getText ());
        System.out.println (jtfSend.getText ());
        writer.flush ();
        System.out.println ("message has sent");
    } catch (Exception e) {
        e.printStackTrace ();
    }//CLOSE CATCH STATEMENT
    jtfSend.setText ("");
    jtfSend.requestFocus();
}                                          

Here is the code that connects the client to the server:

public void setUpConnection () {
  try {
    socket = new Socket ("127.0.0.1", 5000);
    writer = new PrintWriter (socket.getOutputStream ());
    stream = new InputStreamReader (socket.getInputStream ());
    reader = new BufferedReader (stream);

    System.out.println ("Connection Made");
  } catch (Exception e) {
      e.printStackTrace ();
  }
}

Here is the code runs in a separate thread and listens for messages sent by the server:

  public class recievingMessages implements Runnable {
      public void run () {
        String message;
        try {
         while ((message = reader.readLine ()) != null) {
            jtaRec.append (message + "\n");
            System.out.println ("I recieved the message" + " " + message);
         }
        } catch (Exception e) {
           e.printStackTrace();
       }
    }
  }

I have tried finding a solution but I just don't understand why one client can only send one message whilst the other client can send multiple messages even though both programs share the same class.

1

1 Answers

0
votes

I played a bit with your networking code and it seems to be working fine.

I mean, both clients have been able to send and receive multiple messages:

#### server logs ####           ### Jack logs ###           ### Gill logs ###
Starting server
Client is connected to server   Jack connected
SRV:1 received: Jack: Aa #1     SND:Jack - Jack: Aa #1
                                RCV:Jack - Jack: Aa #1
Client is connected to server   SND:Jack - Jack: Aa #2      Gill connected
SRV:1 received: Jack: Aa #2     RCV:Jack - Jack: Aa #2      RCV:Gill - Jack: Aa #2
SRV:2 received: Gill: Zz #1     RCV:Jack - Gill: Zz #1      RCV:Gill - Gill: Zz #1
                                                            SND:Gill - Gill: Zz #1
SRV:2 received: Gill: Zz #2     RCV:Jack - Gill: Zz #2      RCV:Gill - Gill: Zz #2
                                                            SND:Gill - Gill: Zz #2
SRV:1 received: Jack: Aa #3     RCV:Jack - Jack: Aa #3      RCV:Gill - Jack: Aa #3
                                SND:Jack - Jack: Aa #3
SRV:2 received: Gill: Zz #3     RCV:Jack - Gill: Zz #3      RCV:Gill - Gill: Zz #3
                                                            SND:Gill - Gill: Zz #3
SRV:2 received: Gill: Zz #4     RCV:Jack - Gill: Zz #4      RCV:Gill - Gill: Zz #4
                                                            SND:Gill - Gill: Zz #4
SRV:1 received: Jack: Aa #4     SND:Jack - Jack: Aa #4
                                RCV:Jack - Jack: Aa #4      RCV:Gill - Jack: Aa #4
SRV:2 received: Gill: Zz #5     RCV:Jack - Gill: Zz #       RCV:Gill - Gill: Zz #5
                                                            SND:Gill - Gill: Zz #5
SRV:1 received: Jack: Aa #5     SND:Jack - Jack: Aa #5
                                RCV:Jack - Jack: Aa #5

Most likely, in your case one of the clients connected too late and was unable to get entire history as this feature is not implemented at the server side.

Also, one of your clients might have disconnected too early and lose part of the history.

In order to further analyze the reasons of lost messages, you may need to provide details of your UI code (how you build UI in initComponents() and run the client app).

It could be that the UI components could fail to update if you changed them outside the GUI thread, so calling jtaRec.revalidate() may help.