如何同时处理两个进程但仍然保持响应的 Java 图形界面。

huangapple go评论53阅读模式
英文:

How to handle 2 process at once but still responsive gui java

问题

I understand your request. Here's the translated code:

这是服务器部分:

public void server(int port) {
    try {
        try (ServerSocket server = new ServerSocket(port)) {
            serverStatus = "Server已经开始在端口" + port + "上运行";

            while (true) {
                // 等待客户端连接
                serverStatus = "等待客户端连接...";
                Socket robot = server.accept();
                serverStatus = "已连接";

                // 将客户端连接添加到连接列表
                robotSockets.add(robot);

                // 创建线程来处理每个客户端连接
                Thread robotThread = new Thread(new ClientHandler(robot));
                robotThread.start();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这是客户端部分:

public void client(Consumer<String> onMessageReceived) {
    Thread clientSideThread = new Thread(() -> {
        try {
            InputStream inputStream = socket.getInputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                String message = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);
                onMessageReceived.accept(message);
            }

        } catch (IOException e) {
            e.printStackTrace();
            if (!socket.isClosed()) {
                this.statusKoneksi = "连接已断开";
            }
        }
    });
    clientSideThread.start();
}

如何使程序既可以作为服务器等待客户端连接,同时也可以作为客户端连接到服务器,而且仍然能够响应性地工作呢?

英文:

So i making a program that have gui and can be client/server at same time but my program only process one progress for example as a server wait for client to connect and wait for it but after that my program is freeze

This is server 

public void server(int port) {
        try {
            try (ServerSocket server = new ServerSocket(port)) {
                serverStatus = (&quot;Server telah dimulai di port &quot; + port);

                while (true) {
                    // Menunggu koneksi dari client
                    serverStatus = (&quot;Menunggu koneksi dari client...&quot;);
                    Socket robot = server.accept();
                    serverStatus = (&quot;Terhubung&quot;);
               

                    // Menambahkan koneksi client ke daftar koneksi
                    robotSockets.add(robot);

                    // Membuat thread untuk menangani setiap koneksi client
                    Thread robotThread = new Thread(new ClientHandler(robot));
                    robotThread.start();
                }
            }

Here for client

public void client(Consumer&lt;String&gt; onMessageReceived) {
        Thread clientSideThread = new Thread(() -&gt; {
            try {
                InputStream inputStream = socket.getInputStream();
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    String message = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);
                    onMessageReceived.accept(message);
                }

            } catch (IOException e) {
                e.printStackTrace();
                if (!socket.isClosed()) {
                    this.statusKoneksi = &quot;Terputus&quot;;
                }
            }
        });
        clientSideThread.start();
    }

So how to make this program can handle be server and client but still working responsive?, as server wait client to connect and at same time as client can connect to server?

答案1

得分: 0

可以使用不同的线程。

一个线程用于监听套接字连接,另一个线程执行其他GUI任务。

英文:

You can use different threads.

One thread is listen for the socket connection, another is doing other GUI tasks.

huangapple
  • 本文由 发表于 2023年4月17日 14:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定