Client-Server-Client communication using Sockets

后端 未结 3 2029
旧巷少年郎
旧巷少年郎 2020-12-14 05:06

I am building a small chat application in which client A wants to send something to client C with server B in between. First of all is this a correct approach for the proble

相关标签:
3条回答
  • 2020-12-14 05:35

    When your clients connect to the server, your server creates a Socket for it, here it is Socket socket = ss.accept();, your socket variable will be holding that client.

    now if you just keep adding your client socket to a arraylist in your while loop, you will have a list of clients actively connected with your server like:

    after the accept:

    clients = new ArrayList<DataOutputStream>();
    Socket socket = ss.accept();
    os = new DataOutputStream(socket.getOutputStream());
    clients.add(os);
    

    Now as you have all the clients in that clients arraylist, you can loop through it, or with some protocol define which client should i send the data after reading.

    Iterator<DataOutputStream> it = clients.iterator();
    while ((message = reader.readLine()) != null) { //reading    
        while (it.hasNext()) {
            try {
                DataOutputStream oss = it.next();
                oss.write(message);//writing
                oss.flush();
            }
            catch (Exception e) { }
         }
     }
    

    This will loop through all the available clients in the arraylist and will send to all. you can define ways to send to only some.

    For example: maintain a ActiveClients arraylist and with some GUI interaction may be or maybe, define what all clients you want to send the message. Then add just those clients outputStreams to ActiveClients

    ActiveClients.add(clients.get(2));
    

    or remove them, if you don't want them.

    ActiveClients.remove(clients.get(2));
    

    and now just loop through this arraylist to send the data as above.

    0 讨论(0)
  • 2020-12-14 05:41

    You can create message queue for each client:

    1. Client A sends message 'Hi' with address Client C to server B.
    2. Server B receives message and adds it to message queue of client C.
    3. Thread in server B which communicates with client C check message queue, retrieve message and sends it to client C.
    4. Client C receives message.
    0 讨论(0)
  • 2020-12-14 05:45

    If I am not mistaken, you must be having a problem with receiving a message from the Server or SSocket class. What happens with your code is that when you send a message from the client to the server the Server class receives your messages also gives an echo of the message in the client. However, when you send a message from the Server class, you don't get any messages in the Client Class.

    To get this to work, you would have to modify your code in the following fashion:

    SSocket Class

    String line = null;
    while (true) {
        line = dIn.readUTF();
        System.out.println("Recievd the line----" + line);
        dOut.writeUTF(line + " Comming back from the server");
        dOut.flush();
        System.out.println("waiting for the next line....");
    }
    

    You should add these lines:

    String Line2 = take.nextLine(); //The user types a message for the client
    dOut.writeUTF(Line2 + " Comming back from the server"); //The message is sent to the client
    

    Replace the while loop with this one and it will work fine.

    while (true) {       
        line = dIn.readUTF(); //Takes the msg from the client
        System.out.println("Recievd the line----" + line); //Prints the taken message
    
        String Line2 = take.nextLine(); //The user types a message for the client
        dOut.writeUTF(Line2 + " Comming back from the server"); //The message is sent to the client
        dOut.flush();
        System.out.println("waiting for the next line....");
    }
    
    0 讨论(0)
提交回复
热议问题