使用套接字进行客户端-服务器-客户端通信

5
我正在构建一个小型聊天应用程序,其中客户端A想要通过位于服务器B中间的方式将内容发送给客户端C。首先,这是否是解决问题的正确方法? 我能够向服务器发送和接收数据,但仅限于每个客户端。例如,如果客户端A向服务器B发送数据,客户端C也向服务器B发送数据,则我可以像回显服务器一样向A和C发送数据。但我想要的是将来自客户端A的数据通过服务器B转发到客户端C。
以下是服务器代码:
public class Server {
    public static void main(String[] args) {
        int port = 666; //random port number
        try {
            ServerSocket ss = new ServerSocket(port);
            System.out.println("Waiting for a client....");

            System.out.println("Got a client :) ... Finally, someone saw me through all the cover!");
            System.out.println();
            while(true) {
                Socket socket = ss.accept();

                SSocket sSocket = new SSocket(socket);
                Thread t = new Thread(sSocket);
                t.start();
                System.out.println("Socket Stack Size-----"+socketMap.size());
            }
        }
        catch (Exception e) { }
    }
}

class SSocket implements Runnable {
    private Socket socket;

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

    @Override
    public void run() {
        try {
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();

            DataInputStream dIn = new DataInputStream(in);
            DataOutputStream dOut = new DataOutputStream(out);

            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....");    
            }
        }
        catch (Exception e) { }
    }
}

客户端代码如下:
public class Client {
    public static void main(String[] args) {
        int serverPort = 666;

        try {
            InetAddress inetAdd = InetAddress.getByName("127.0.0.1");
            Socket socket = new Socket(inetAdd, serverPort);

            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();

            DataInputStream dIn = new DataInputStream(in);
            DataOutputStream dOut = new DataOutputStream(out);

            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
            System.out.println();

            String line = null;
            while (true) {
                line = keyboard.readLine();
                System.out.println("Wrinting Something on the server");
                dOut.writeUTF(line);
                dOut.flush();

                line = dIn.readUTF();
                System.out.println("Line Sent back by the server---" + line);
            }
        }
        catch (Exception e) { }
    }
}
3个回答

5
当您的客户端连接到服务器时,服务器会为其创建一个Socket,在这里是Socket socket = ss.accept();,您的socket变量将持有该客户端。
现在,如果您只是在while循环中将客户端socket添加到arraylist中,您将拥有一个与服务器主动连接的客户端列表,如下所示:
接受后:
clients = new ArrayList<DataOutputStream>();
Socket socket = ss.accept();
os = new DataOutputStream(socket.getOutputStream());
clients.add(os);

现在,既然您已经有了所有 clients 列表中的客户端,您可以通过循环遍历它,或使用一些协议来定义读取后应将数据发送给哪个客户端。
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) { }
     }
 }

这将循环遍历数组列表中的所有可用客户端,并向所有客户端发送消息。您可以定义仅向某些客户端发送消息的方式。
例如: 维护一个名为ActiveClients数组列表,并通过一些GUI交互,定义要发送消息的所有客户端。 然后只需将这些客户端的输出流添加到ActiveClients中即可。
ActiveClients.add(clients.get(2));

或者如果您不需要它们,可以将它们删除。
ActiveClients.remove(clients.get(2));

现在只需要循环遍历这个 arraylist,就可以像上面那样发送数据。


1
你可以为每个客户端创建一个消息队列:
  1. 客户端A向服务器B发送带有地址Client C的消息“Hi”。
  2. 服务器B接收消息并将其添加到客户端C的消息队列中。
  3. 与客户端C通信的服务器B的线程检查消息队列,检索消息并将其发送给客户端C。
  4. 客户端C接收消息。

0
如果我没记错的话,您可能遇到了从服务器或SSocket类接收消息的问题。您的代码发生的情况是,当您从客户端向服务器发送消息时,服务器类会接收您的消息,并在客户端中回显该消息。但是,当您从服务器类发送消息时,在客户端类中不会收到任何消息。
要使此功能正常工作,您需要按以下方式修改代码:

SSocket类

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....");
}

你应该添加这些行:

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

用这个替换掉 while 循环,它就能正常工作了。
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....");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接