程序在ServerSocket.accept上无响应-Java [英] Programme goes unresponsive at ServerSocket.accept - Java

查看:670
本文介绍了程序在ServerSocket.accept上无响应-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序仅侦听一次连接...如果没有客户端连接,则程序只会卡在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());

解决方案

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

您需要做的是创建一个新线程(或使用应用程序的主"线程,这与EDT不同),该线程位于等待客户端连接的接受上.连接后,它可以完成所需的任何工作,但是当您要更新GUI时,需要将代码包装起来以在Runnable中进行更新,然后通过SwingUtilities.invokeLater将其传递给EDT. /p>

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());

解决方案

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.

这篇关于程序在ServerSocket.accept上无响应-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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