监听多个套接字(InputStreamReader)

发布于 2024-12-08 10:28:00 字数 759 浏览 3 评论 0原文

我在课堂上设计的一个小游戏遇到了问题。 问题是我有两个客户端连接到服务器。 (client1 和 client2)他们各自运行一个游戏,最终关闭窗口。由于游戏窗口是一个 JDialog,因此当它关闭时,它会通过套接字向服务器发送一条消息,告诉它已经完成。我希望服务器知道两个客户端中哪一个先完成。他们通过套接字的 OutputStream 上的 PrintWriter 进行报告。 我所做的是:

    in1 = new BufferedReader(new InputStreamReader(client.getInputStream()));
    in2 = new BufferedReader(new InputStreamReader(client2.getInputStream()));
    try {
        in1.readLine();
    } catch (IOException ex) {
        Logger.getLogger(gameServer.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        in2.readLine();
    } catch (IOException ex) {
        Logger.getLogger(gameServer.class.getName()).log(Level.SEVERE, null, ex);
    }

问题是它等待第一个输入,然后才开始监听第二个输入。我怎样才能让它同时收听两者?或者以其他方式解决我的问题。 谢谢!

I'm having a problem with a little game I'm designing in my class.
The problem is that I got two clients connected to a server. (client1 and client2) They are each running a game, which in the end, closes the window. As the game window is a JDialog, it will then, when it's closed, send a message, through a socket, to the server, telling it that it's done. I want the server to know which of the two clients were completed first. They are reporting through a PrintWriter on the sockets' OutputStream.
What I did was this:

    in1 = new BufferedReader(new InputStreamReader(client.getInputStream()));
    in2 = new BufferedReader(new InputStreamReader(client2.getInputStream()));
    try {
        in1.readLine();
    } catch (IOException ex) {
        Logger.getLogger(gameServer.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        in2.readLine();
    } catch (IOException ex) {
        Logger.getLogger(gameServer.class.getName()).log(Level.SEVERE, null, ex);
    }

Problem is that it waits for the first input, before it even starts listening on the second. How can I make it listen on both at the same time? Or solve my problem some other way.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-12-15 10:28:00

服务器连接应该像这样工作:

Server gameServer = new Server();

ServerSocket server;
try {
    server = new ServerSocket(10100);
    // .. server setting should be done here
} catch (IOException e) {
    System.out.println("Could not start server!");
    return ;
}

while (true) {
    Socket client = null;
    try {
        client = server.accept();
        gameServer.handleConnection(client);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

在 hanleConnection() 中,您启动一​​个新线程并在创建的线程中运行该客户端的通信。然后服务器可以接受新连接(在旧线程中)。

public class Server {
    private ExecutorService executor = Executors.newCachedThreadPool();

    public void handleConnection(Socket client) throws IOException {    
        PlayerConnection newPlayer = new PlayerConnection(this, client);
        this.executor.execute(newPlayer);
    }

    // add methods to handle requests from PlayerConnection
}

PlayerConnection 类:

public class PlayerConnection implements Runnable {

    private Server parent;

    private Socket socket;
    private DataOutputStream out;
    private DataInputStream in;

    protected PlayerConnection(Server parent, Socket socket) throws IOException {
        try {
            socket.setSoTimeout(0);
            socket.setKeepAlive(true);
        } catch (SocketException e) {}

        this.parent = parent;
        this.socket = socket;

        this.out    = new DataOutputStream(socket.getOutputStream());;
        this.in     = new DataInputStream(socket.getInputStream());
    }

    @Override
    public void run() {                 
        while(!this.socket.isClosed()) {                        
            try {
                int nextEvent = this.in.readInt();

                switch (nextEvent) {
                    // handle event and inform Server
                }
            } catch (IOException e) {}
        }

        try {
            this.closeConnection();
        } catch (IOException e) {}
    }
}

Server connection should work like this:

Server gameServer = new Server();

ServerSocket server;
try {
    server = new ServerSocket(10100);
    // .. server setting should be done here
} catch (IOException e) {
    System.out.println("Could not start server!");
    return ;
}

while (true) {
    Socket client = null;
    try {
        client = server.accept();
        gameServer.handleConnection(client);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

In hanleConnection() you start a new thread and run the communication for this client in the created thread. Then the server can accept a new connection (in the old thread).

public class Server {
    private ExecutorService executor = Executors.newCachedThreadPool();

    public void handleConnection(Socket client) throws IOException {    
        PlayerConnection newPlayer = new PlayerConnection(this, client);
        this.executor.execute(newPlayer);
    }

    // add methods to handle requests from PlayerConnection
}

The PlayerConnection class:

public class PlayerConnection implements Runnable {

    private Server parent;

    private Socket socket;
    private DataOutputStream out;
    private DataInputStream in;

    protected PlayerConnection(Server parent, Socket socket) throws IOException {
        try {
            socket.setSoTimeout(0);
            socket.setKeepAlive(true);
        } catch (SocketException e) {}

        this.parent = parent;
        this.socket = socket;

        this.out    = new DataOutputStream(socket.getOutputStream());;
        this.in     = new DataInputStream(socket.getInputStream());
    }

    @Override
    public void run() {                 
        while(!this.socket.isClosed()) {                        
            try {
                int nextEvent = this.in.readInt();

                switch (nextEvent) {
                    // handle event and inform Server
                }
            } catch (IOException e) {}
        }

        try {
            this.closeConnection();
        } catch (IOException e) {}
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文