简体   繁体   中英

Manage all incoming chat messages in java socket programming

I am developing a program that has a chat feature and I am using sockets in it. In my case, I want to handle each of the client in a different window chat(PLEASE SEE ATTACHED IMAGE).

As of now, when 1 client is connected, there is no problem. But when 2 clients are connected, the first client will be overridden by the 2nd one and he can't receive messages from server not unless I close the connection for the latest client connected(Server still receiving messages from all client although only 1 client can receive from server).

How am I gonna do this? I am using captain casa framework I want to manage it like what did the image below do.

IMAGE HERE

Here is my code: Server:

public void mainserver(){
        Thread server = new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    serverSocket = new ServerSocket(port);
                    System.out.println("Server Online... \nWaiting for Connections");
                } catch (IOException e) {
                    e.printStackTrace();
                }

                while (accept){
                    try {
                        socket = serverSocket.accept();
                        System.out.println("New Connection Estasblished!!!");

                        chatHandler chat = new chatHandler(socket);
                        chat.start();
                    } catch (IOException e) {
                        System.out.println("server not terminate all connections");
                        System.exit(-1);
                    }
                }
            }
        });
        server.start();
    }

public class chatHandler extends Thread{

       Socket socket;

       public chatHandler(Socket socket){
           this.socket = socket;
       }

       public void run(){
           try {
               din = new DataInputStream(socket.getInputStream());
               dout = new DataOutputStream(socket.getOutputStream());

               dout.writeUTF("Hi! Thank you for reaching us! How may I help you!?");

               while (!read.equals(".end")){

                   read = din.readUTF();

                   if (getServerArea()!=null){
                       setServerArea(getServerArea()+"\n"+read);
                   }else {
                       setServerArea(read);
                   }
               }
               System.out.println("end of chat server");
           } catch (IOException e) {
               e.printStackTrace();
           }finally {
               System.out.println("Exit");
               try {
                   dout.close();
                   din.close();
                   socket.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }

       }
   }

public void serverSend(javax.faces.event.ActionEvent event) { // "Send" button
        write = getServerField();
        try {
            dout.writeUTF(write);
            dout.flush();
            if (getServerArea()!=null){
                setServerArea(getServerArea()+"\n"+write);
                setServerField("");
            }else {
                setServerArea(write);
                setServerField("");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(write);
    }

Client:

public void client(){
        Thread client = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    socket = new Socket("localhost",port);

                    din  = new DataInputStream(socket.getInputStream());
                    dout = new DataOutputStream(socket.getOutputStream());

                    while (!read.equals("bye")){
                        read = din.readUTF();

                        if (getClientArea()!=null){
                            setClientArea(getClientArea()+"\n"+read);
                        }else {
                            setClientArea(read);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    try {
                        din.close();
                        dout.close();
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        client.start();
    }

    public void clientSend(javax.faces.event.ActionEvent event) {
        write = getClientField();
        try {
            dout.writeUTF(write);
            dout.flush();
            if (getClientArea()!=null){
                setClientArea(getClientArea()+"\n"+write);
                setClientField("");
            }else {
                setClientArea(write);
                setClientField("");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(write);
    }

I believe I understand the problem, and how to correct it.

You are using a unique thread (chatHandler) for each new connection.
This thread writes an automatic "Hello" upon connection, but thereafter is dedicated to reading messages (in the while loop you only read din) and updating the console accordingly. Since each thread is managing a reference to din, all incoming messages are OK.

However, it seems that writing back to a client (serverSend) is not in a thread; it is triggered by a button event. At this point, dout will be a reference to the most recent connection, and not a reference to the client intended to get the message. That is why the most recent client gets all future messages.

The correction is to choose the correct 'dout' for the intended client. When the server 'operator' chooses to write a message back (clicking the send button), somehow you need to obtain the correct 'dout' for that client.

One way to do this is to establish dout prior to creating the thread (using socket), and maintain a relationship between each client, and it's corresponding dout (ie in a Map).

If the problem is still not clear (that each client must have a unique reference to dout), please let me know and I will try to clarify.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM