套接字未正确关闭

发布于 2024-09-07 14:36:46 字数 2074 浏览 5 评论 0原文

我正在尝试创建一个 UDP 侦听器,它将在单独的线程上侦听。第一次它工作正常,但是当我停止连接然后再次启动它时,它会出现错误。

listenerRunnable = new Runnable() {
        public void run() {
            //This thread will listen keep listening to the UDP traffic and put it to the log source string
            try {
                sock = new DatagramSocket(portNumber);
            } catch (SocketException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            while(keepListening) {
                try {
                    pack = new DatagramPacket(recievedData, BUFFERSIZE);
                    sock.receive(pack);

                    String data = new String(pack.getData(), 0, pack.getLength());
                    addToLog(data);
                    System.out.println(data);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                sock.close();
            }
        }
    };

/**
 * Function to start the listening thread.
 */
public void startListening(int portNum) {
    keepListening = true;
    portNumber = portNum;

    listenerThread = new Thread(listenerRunnable);

    logSource_buffer = "";
    logSourcehtml_buffer = "";
    logSourcehtml_temp = "";
    ipListIndex_beg = 0;
    ipListIndex_end = -1;

    if(!listenerThread.isAlive()) {
        listenerThread.start();
    }
}

/**
 * stops the listening thead.  When the listening thread sees that keepListening is set to false
 * it will reach the end of its loop, close the socket, and the thread will die.
 */
public void stopListening() {
    keepListening = false;
}

它给了我以下错误:

logUpdatingThread 已进入同步块!!! java.net.SocketException:无法识别的 Windows 套接字错误:0:无法绑定 在 java.net.PlainDatagramSocketImpl.bind0(本机方法) 在 java.net.PlainDatagramSocketImpl.bind(来源未知)

,它指向 sock.recieve(pack) 行; 似乎由于某种原因,套接字没有关闭,因为我认为,它在 sock.recieve(pack) 处等待,并且永远不会退出 while 循环来关闭套接字。我该如何解决这个问题呢?

谢谢

I am trying to create a UDP listener that will listen on a separate thread. It works fine the first time but when I stop the connection and then start it again it gives me errors.

listenerRunnable = new Runnable() {
        public void run() {
            //This thread will listen keep listening to the UDP traffic and put it to the log source string
            try {
                sock = new DatagramSocket(portNumber);
            } catch (SocketException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            while(keepListening) {
                try {
                    pack = new DatagramPacket(recievedData, BUFFERSIZE);
                    sock.receive(pack);

                    String data = new String(pack.getData(), 0, pack.getLength());
                    addToLog(data);
                    System.out.println(data);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                sock.close();
            }
        }
    };

/**
 * Function to start the listening thread.
 */
public void startListening(int portNum) {
    keepListening = true;
    portNumber = portNum;

    listenerThread = new Thread(listenerRunnable);

    logSource_buffer = "";
    logSourcehtml_buffer = "";
    logSourcehtml_temp = "";
    ipListIndex_beg = 0;
    ipListIndex_end = -1;

    if(!listenerThread.isAlive()) {
        listenerThread.start();
    }
}

/**
 * stops the listening thead.  When the listening thread sees that keepListening is set to false
 * it will reach the end of its loop, close the socket, and the thread will die.
 */
public void stopListening() {
    keepListening = false;
}

It gives me the following error:

logUpdatingThread has entered synchronized block!!!
java.net.SocketException: Unrecognized Windows Sockets error: 0: Cannot bind
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.PlainDatagramSocketImpl.bind(Unknown Source)

which points to the line with sock.recieve(pack);
It seems like for some reason the socket isn't closing because, I think, its waiting at sock.recieve(pack) and never gets out of the while loop to close the socket. How would I get around this though?

Thanks

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

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

发布评论

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

评论(2

ㄟ。诗瑗 2024-09-14 14:36:46

正如 Peter Tillemans 所说,您应该设置一个接收超时,这样您就不会永远坐在那里尝试 receive() 。

另外,保留 new Thread(listenerRunnable) 返回的 Thread 对象,以便您的 stopListening() 方法可以等待线程终止:

public void stopListening() {
    keepListening = false;
    listenerThread.join();
}

As Peter Tillemans said, you should set a receive timeout so that you're not sitting there trying to receive() for ever.

Also, keep hold of the Thread object returned by new Thread(listenerRunnable) so that your stopListening() method can wait for the thread to die:

public void stopListening() {
    keepListening = false;
    listenerThread.join();
}
同展鸳鸯锦 2024-09-14 14:36:46

您必须在调用 receive 之前添加 setSoTimeout(timeout) 。这将定期抛出 SocketTimeoutExceptions,但保持数据报套接字打开。这将允许您定期检查循环变量。

此外,您应该将循环移到第一个 try-catch 块内,并添加一个 finally 块来关闭套接字。

喜欢 :

        try {
            sock = new DatagramSocket(portNumber);
            sock.setSoTimeout(250);
            while(keepListening) {
                try {
                    pack = new DatagramPacket(recievedData, BUFFERSIZE);
                    sock.receive(pack);

                    String data = new String(pack.getData(), 0, pack.getLength());
                    addToLog(data);
                    System.out.println(data);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (SocketException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } finally {
            sock.close();
        }

You'll have to add a setSoTimeout(timeout) before calling receive. This will regularly throw SocketTimeoutExceptions, but keeping the Datagram socket open. This will allow you to regularly check the loop variable.

Additionally you should move the loop inside the first try-catch block and add a finally block to close the socket.

like :

        try {
            sock = new DatagramSocket(portNumber);
            sock.setSoTimeout(250);
            while(keepListening) {
                try {
                    pack = new DatagramPacket(recievedData, BUFFERSIZE);
                    sock.receive(pack);

                    String data = new String(pack.getData(), 0, pack.getLength());
                    addToLog(data);
                    System.out.println(data);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (SocketException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } finally {
            sock.close();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文