简体   繁体   English

我可以使用套接字列表发送给多个客户端吗?

[英]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. 我有一个ArrayList<Socket> listOfSockets ,当我收到像这个listOfSockets.add(clientsocket) clientsocket = serversocket.accept()这样的listOfSockets.add(clientsocket) clientsocket = serversocket.accept()时,它会添加一个套接字,然后将整个列表发送给这样的另一个类。

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. ListenforMessage类中,在for循环中,我会TEMP_SOCK列表的大小,如果TEMP_SOCK不是this.socket那么我想向该套接字发送一条消息,如果它不执行任何操作。 But my problem is that when I connect with 2 clients they can't send message to each other. 但是我的问题是,当我与2个客户端连接时,它们无法彼此发送消息。 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. 因为现在Client1发送一条消息并从服务器接收相同的消息,而Client2做同样的事情。 I want Client1 to send and Client2 to receive the message(And other sockets on the same list) 我希望Client1发送并且Client2接收消息(以及同一列表中的其他套接字)

您的代码无效,因为您应该让SendMessage知道它将消息发送到哪个套接字(即,您应该将TEMP_SOCK传递给它),而是使用socket变量(它是连接到客户端的套接字)刚刚发送了消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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