1

So I have a homework assignment to create 2 clients on 1 server communicate on one server. Client 1 and Client 2 can communicate to the server but I need the 2 Clients to communicate to each and I am stumped??

I am pretty sure I'm on the right track using ArrayList to add clients and use a for loop to go through them. I just don't know how to connect the t clients to communicate to each other. Here is my code.

//Communication Thread class In Server.class file. The for loop in the while true loop should make the 2 clients communicate but isn't??

class ComThreads implements Runnable{
              private Socket s;
              java.util.Date date=new java.util.Date();

              public ComThreads(Socket s)
              {
                this.s=s;
              }

              public void run()
              {     try {
                  DataInputStream inputFromClient = new DataInputStream(
                        s.getInputStream());
                    DataOutputStream outputToClient = new DataOutputStream(
                        s.getOutputStream());

                    while(true) {
                            String  line=inputFromClient.readUTF();
                    for(int i=0; i < clientList.size(); i++) {  
                        if(clientList.get(i).equals(s)) {
                    Socket tempSoc=clientList.get(i);
                    DataOutputStream msOut=new DataOutputStream(tempSoc.getOutputStream());
                    msOut.writeUTF(line);
                    //outputToClient.writeUTF(message);
                    msOut.flush();
                        }

                    }
                    Platform.runLater(()->{
                        ta.appendText(line);
                        ta.appendText("\n");
                    });


                    }

                } catch (IOException e) {
                  e.printStackTrace();
                }finally {
                    try {
                        s.close();
                    }catch(IOException e) {
                        // later
                    }

                }

              }

            }           

        }   

Just need the 2 clients to communicate. Any help would be appreciated. Thanks

rli
  • 1,745
  • 1
  • 14
  • 25
  • 2
    I did not follow the code. Please post [mcve]. I assume that the 2 clients communicate via the server. – c0der Apr 04 '19 at 05:31

1 Answers1

0

I got it working. All I had to do is add a Runnable class that reads the Client message and communicates it to the other client. So i was just missing a thread in my Client class. Thanks.