Bojje
Bojje

Reputation: 417

Can I use a list of sockets to send to multiple clients?

I have a ArrayList<Socket> listOfSockets that adds a socket when I receive clientsocket = serversocket.accept() like this listOfSockets.add(clientsocket) and then I send this whole list to another class like this.

Server_Client client = new Server_Client(clientSock, clientSocketList);
                    Thread X = new Thread(client);
                    X.start();

Here is the code for my class that I send the list and socket to

public class Server_Client implements Runnable{
//ArrayList<String> UserNameList;
ArrayList<Socket> clientSocketList;
Socket socket = null;

public Server_Client(Socket X, ArrayList<Socket> L){
    this.socket = X;
    this.clientSocketList = L;
}


@Override
public void run() {
    try {

        ListenforMessage NewMessages = new ListenforMessage(socket);
        Thread myThread = new Thread(NewMessages);
        myThread.start();            
    } catch (Exception e) {
        System.err.println(e);
    }
}
//------------------------------------------------------------------
//Class that handles incoming messages!
class ListenforMessage implements Runnable{
    Socket socket;
    DataInputStream IN;
    DataOutputStream OUT;
    public ListenforMessage(Socket x) {
        this.socket = x;
    }

    @Override
    public void run() {
        try {
            while (true) {                    
                    IN = new DataInputStream(socket.getInputStream());
                    OUT = new DataOutputStream(socket.getOutputStream());
                    String message = IN.readUTF();
                    //System.out.println(message);
                    for (Socket TEMP_SOCK : clientSocketList) {
                        if(TEMP_SOCK != this.socket){ 
                            SendMessage(message);
                        }
                    }
                }                                                  
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                        

}

public void SendMessage(String m) throws IOException{
    try {            
        DataOutputStream OUT = new DataOutputStream(socket.getOutputStream());
        OUT.writeUTF(m);
        System.out.println("User said: " + m);
    } catch (Exception e) {
    }

}
//------------------------------------------------------------------
}

In the ListenforMessage class, in the for-loop I go trough the size of list and if TEMP_SOCK is not this.socket then I want to send a message to that socket, and if it is then don't do anything. But my problem is that when I connect with 2 clients they can't send message to each other. They just send the message to the server and the server sends back the message to the client. My question is, can you use a list like I do to check if the socket is not the socket that you are YOURSELF.

cause now Client1 sends a messages and receive the same messages from sever, and Client2 does the same thing. I want Client1 to send and Client2 to receive the message(And other sockets on the same list)

Upvotes: 0

Views: 989

Answers (1)

Giulio Franco
Giulio Franco

Reputation: 3230

Your code doesn't work because you should let SendMessage know to which socket it should send the message (i.e. you should pass TEMP_SOCK to it), instead, you're making it use the socket variable, which is the socket connected to the client that just sent the message.

Upvotes: 2

Related Questions