多人 Java 游戏 - 通过 GUI 启动服务器时程序冻结 [英] Multiplayer Java game - program freeze when starting Server via GUI

查看:52
本文介绍了多人 Java 游戏 - 通过 GUI 启动服务器时程序冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Java GUI 多人游戏.我有一个 GUI,用户可以在其中输入端口号并单击启动服务器",这将启动游戏服务器并调出另一个 GUI 框架.但是当单击按钮时我的程序会冻结.使用这种方式启动服务器是否可以,或者我如何编码以便启动服务器并等待玩家连接并同时显示另一个 GUI 框架(在单独的类中编写)?提前致谢.

I'm writing a Java GUI multiplayer game. I have a GUI where user can enter port number and click on "start server" which will initiate game server and bring up another GUI frame. But my program freezes when the button is clicked. Is it okay to start server using this way or how can I code so that server will be started and waiting for players to be connected and at the same time display another GUI frame (written in a separate class)? Thanks in advance.

// part of GUI code

start = new JButton ("Start Game Server");
        start.addActionListener (new ActionListener() {

            public void actionPerformed (ActionEvent event) {
                DEFAULT_PORT = Integer.parseInt(port.getText());
                fgServer.run();
                fgServerFrame = new FishingGameServerFrame();
                //frame.dispose();
            }
        });

--

// server code

public class FishingGameServer {

    private static int DEFAULT_PORT = 0;

    public void run()
    {

        int port = DEFAULT_PORT;

        port = Integer.parseInt(FishingGameConnectServerFrame.portNumber());
        System.out.println("port #: " + port);

        //setup server socket
        ServerSocket reception_socket = null;

        try {
            reception_socket = new ServerSocket (port);
            System.out.println("Started server on port " + port);
        }
        catch (IOException e) {
            //to get text in GUI frame
            System.out.println("Cannot create server");
            System.exit(0);
        }

        for (;;) {
            Socket client_socket = null;

            try {
                client_socket = reception_socket.accept();
                System.out.println("Accepting requests from:" + client_socket.getInetAddress());
            }
            catch (IOException i) {
                System.out.println ("Problem accepting client socket");
            }

            new FishingGameThreadedServer(client_socket);
        }
    }

    public static void main (String[] args) {

    new FishingGameServer().run();
    }

推荐答案

你调用了fgServer.run();,最终调用了client_socket =receipt_socket.accept(); 在无限循环中.

You call fgServer.run();, which eventually calls client_socket = reception_socket.accept(); within an infinite loop.

这是通过阻塞(一次在永无止境的 for-loop 中,一次在使用 accept 时)阻止事件调度线程能够运行,它无法处理事件队列,它负责处理绘制请求等.

This is preventing the Event Dispatching Thread from been able to run, by blocking (once within the neverending for-loop and once when using accept) it can not process the Event Queue, which is responsible for, amongst other things, processing paint requests.

Swing 是单线程环境,它也不是线程安全的.这意味着:

Swing is a single threaded environment, it is also not thread safe. This means:

  • 您不应在 EDT 的上下文中执行任何长时间运行或阻塞的操作,并且
  • 与 UI 的所有更新和交互都必须在 EDT 的上下文中进行

查看Swing 中的并发了解更多详情

您可以改用 ThreadSwingWorker 提供了更轻松地将更新发布回 EDT 的功能...

You could use a Thread instead or a SwingWorker which provides functionality to more easily publish updates back to the EDT...

这篇关于多人 Java 游戏 - 通过 GUI 启动服务器时程序冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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