我创建了一个带套接字的Java服务器,如何打印到所有套接字? [英] I've created a Java server with sockets, just how do print to ALL sockets?

查看:130
本文介绍了我创建了一个带套接字的Java服务器,如何打印到所有套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一段时间了,我希望多个客户端同时接收多个输入。
有一个问题,我希望服务器向所有客户打印Hi,如果一个客户说' print2all你好。

我知道如何处理它来打印它,只是打印给所有客户是问题。

I've been trying this for a while, and I want multiple clients to recieve multiple inputs simultaneously.
There is one problem, I want the server to print "Hi" to all clients if one client says 'print2all Hi'.
I know how to process it to print it, just to print to ALL clients is the problem.

这就是我的意思到目前为止。

服务器

Here's what I have so far.
Server

try{
    try{
        server = new ServerSocket(25565);
    } catch (Exception e){
        e.printStackTrace();
    }
    while (isListening){
        new SocketThread(server.accept()).start();
    }
    server.close();
} catch (Exception e){
    e.printStackTrace();
}

SocketThread

try {
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    String inputLine, outputLine;
    Processor kkp = new Processor();
    out.println("Hi!");

    while ((inputLine = in.readLine()) != null) {
        outputLine = kkp.Proccess(inputLine,this.socket);
        out.println(outputLine);
    }
    out.close();
    in.close();
    socket.close();

} catch (IOException e) {
    e.printStackTrace();
}

客户

            Processor p = new Processor();
            socket = new Socket("localhost",25565);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));                
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String fromUser;
            out.println("print2all Hi")            
            socket.close();


推荐答案

首先,您需要跟踪所有连接的客户端:

First you need to keep track of all connected clients:

final List<SocketThread> clients = new ArrayList<>();

while (isListening){
    SocketThread client = new SocketThread(server.accept()).start();
    clients.add(client);
}

如果一个客户收到,请输入此列表print2all Hi 它只是遍历所有客户端并向每个客户端发送消息。要做到这一点,你很可能必须在 SocketThread 上公开一些访问客户端套接字的方法。这意味着您必须将 out 变量更改为字段。

Having such list if one client receives "print2all Hi" it simply iterates over all clients and sends message to each of them. To do this you'll most likely have to expose some method on SocketThread that will access client socket. This means you'll have to change out variable to field.

替代方法是保留客户列表插座。但这严重打破了封装。如果直接暴露套接字,您可能会遇到令人讨厌的IO /线程安全问题。最好将它们隐藏在某些API后面(例如 SocketThread 方法)并在内部正确进行同步。

Alternative approach is to keep a list of client sockets. But this breaks encapsulation badly. Also you might run into nasty IO/thread-safety issues if sockets are exposed directly. Better hide them behind some API (like SocketThread method) and do the synchronization properly inside.

这篇关于我创建了一个带套接字的Java服务器,如何打印到所有套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆