user23
user23

Reputation: 425

Multiplayer Java game - program freeze when starting Server via 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();
    }

Upvotes: 0

Views: 392

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

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

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 is a single threaded environment, it is also not thread safe. This means:

  • You should never perform any long running or blocking operations within the context of the EDT and
  • All updates and interactions with the UI must be made from within the context of the EDT

Take a look at Concurrency in Swing for more details

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

Upvotes: 1

Related Questions