Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
am trying to communicate between two client frequently (ClientA. Server. ClientB)
If ClientA send a message it goes to server then server sends it back to ClientA instead of sending that message to ClientB. same thing with ClientB both client can't communicate with each other but can connect to server.

What I have tried:

The Server Code

public class MultiUserChatServer {

    static List clientList;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        clientList = new ArrayList();
        System.out.println("Server Started\nWaiting For Client Connection");
        try {
            ServerSocket serSock = new ServerSocket(2018);
            while (true) {
                Socket sock = serSock.accept();
                ServerThread s = new ServerThread(sock);
                s.start();
                clientList.add(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class ServerThread extends Thread {

        private final Socket sock;
        private DataInputStream dataIn;
        private DataOutputStream dataOut;

        public ServerThread(Socket sock) {
            this.sock = sock;
        }

        @Override
        public void run() {
            try {
                dataIn = new DataInputStream(sock.getInputStream());
                dataOut = new DataOutputStream(sock.getOutputStream());

                String line;
                while (sock.isConnected()) {
                    line = dataIn.readUTF();
                    for (int i = 0; i < MultiUserChatServer.clientList.size(); i++) {
                        dataOut.writeUTF(line);
                        dataOut.flush();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


Client Code (Controller Class)

public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;
    @FXML
    private JFXTextArea textArea;
    @FXML
    private JFXTextField textField;
    private Socket sock;
    private DataInputStream dataIn;
    private DataOutputStream dataOut;

    @FXML
    private void sendAction(ActionEvent event) {
        try {
            dataOut.writeUTF(textField.getText());
            textArea.appendText("You: " + textField.getText() + "\n");
            dataOut.flush();
            textField.clear();
        } catch (Exception e) {e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        new ClientThread().start();
        label.setText("Connected");
    }

    private class ClientThread extends Thread {

        @Override
        public void run() {
            try {
                sock = new Socket(InetAddress.getLocalHost(), 2018);
                dataOut = new DataOutputStream(sock.getOutputStream());
                dataIn = new DataInputStream(sock.getInputStream());

                while (true) {
                    textArea.appendText(dataIn.readUTF() + "\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}


What i am doing wrong here?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900