程序在 ServerSocket.accept 处无响应 - Java

发布于 2024-08-19 22:13:25 字数 1354 浏览 6 评论 0原文

我的程序只监听一个连接一次...如果没有客户端连接,程序就会卡在 clientSocket = serverSocket.accept() 处。我的意思是我什至不能通过关闭窗户来打断它。我无法单击框架中的任何按钮等。

我在其他程序中以相同的方式使用了此代码,但它工作得很好(我可以单击文本字段和按钮以及其中的内容并键入值,对于这个,它只是冻结在那里,直到客户端连接,甚至无法退出)。

public void runServer() {
    try {
        serverSocket = new ServerSocket(PORT_NUMBER, 20);
        clientSocket = serverSocket.accept();
        taDisplay.append("Client connected!");
        lblPlayingTo.setText("Playing to: " + objective);

        socketIn = new DataInputStream(clientSocket.getInputStream());
        socketOut = new DataOutputStream(clientSocket.getOutputStream());

        socketOut.writeUTF(serverName);
        clientName = socketIn.readUTF();
        lblEastScore.setText(clientName + ": " + eastScore.getScore());  

    } catch (IOException e) {
        System.out.println(e);
        taDisplay.append("Could not listen on port: " + PORT_NUMBER + ".\n");
    }
}

我已经删除了除此之外的所有代码(如下),但我仍然遇到相同的“冻结”问题

    setTitle(title);
    setSize(sizeW, sizeH);
    setVisible(visibility);
    setResizable(resizability);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addKeyListener(new MyKeyAdapter());
    addMouseListener(new MyMouseAdapter());

    //Container
    c = getContentPane();
    c.setLayout(new BorderLayout());

My programme listens for just one connection once... the programme just gets stuck at clientSocket = serverSocket.accept() if no client connects. I mean I can't even interrupt it by closing my window. I can't click any of my buttons in the frame etc.

I've used this code the same way in my other programmes but it's worked fine (I can click text fields and buttons and stuff and type values in them, for this one, it just freezes there until a client connects, can't even exit).

public void runServer() {
    try {
        serverSocket = new ServerSocket(PORT_NUMBER, 20);
        clientSocket = serverSocket.accept();
        taDisplay.append("Client connected!");
        lblPlayingTo.setText("Playing to: " + objective);

        socketIn = new DataInputStream(clientSocket.getInputStream());
        socketOut = new DataOutputStream(clientSocket.getOutputStream());

        socketOut.writeUTF(serverName);
        clientName = socketIn.readUTF();
        lblEastScore.setText(clientName + ": " + eastScore.getScore());  

    } catch (IOException e) {
        System.out.println(e);
        taDisplay.append("Could not listen on port: " + PORT_NUMBER + ".\n");
    }
}

I've removed all my code except this (below) but I still get the same 'freezing' problem

    setTitle(title);
    setSize(sizeW, sizeH);
    setVisible(visibility);
    setResizable(resizability);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addKeyListener(new MyKeyAdapter());
    addMouseListener(new MyMouseAdapter());

    //Container
    c = getContentPane();
    c.setLayout(new BorderLayout());

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

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

发布评论

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

评论(1

沉默的熊 2024-08-26 22:13:25

听起来您正在 GUI 的事件调度线程 (EDT) 上执行 serverSocket.accept() 调用。 Swing 依赖 EDT 来呈现和处理用户交互 - 如果您执行“接受”之类的阻塞调用,您将不会在 GUI 上看到任何更新。

您需要做的是创建一个新线程(或使用应用程序的“主”线程,这与 EDT 不同),该线程位于接受端等待客户端连接。连接后,它可以执行所需的任何工作,但是当您想要更新 GUI 时,您需要将代码包装在 Runnable 中以进行更新,并通过以下方式将其传递给 EDT SwingUtilities.invokeLater

It sounds like you're doing your serverSocket.accept() call on the GUI's Event Dispatch Thread (EDT). Swing relies on the EDT for rendering and handling user interaction - if you do a blocking call like "accept", you won't see any updates on the GUI.

What you'll need to do is create a new thread (or use the application's "main" thread, which is different to the EDT) that sits on the accept waiting for the client to connect. After the connection it can do any of the work it needs to, but when you want to update the GUI, you need to wrap the code to do the update in a Runnable and pass it to the EDT via SwingUtilities.invokeLater.

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