C# 处理线程和阻塞套接字

发布于 2024-10-29 07:40:41 字数 775 浏览 2 评论 0原文

在下面的线程中,将从客户端读取 UDP 数据包,直到布尔字段 Run 设置为 false。

如果在 Receive 方法阻塞时将 Run 设置为 false,则它将永远保持阻塞状态(除非客户端发送数据,这将使线程循环并再次检查 Run 条件)。

while (Run)
{
    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
    byte[] data = udpClient.Receive(ref remoteEndPoint); // blocking method

    // process received data
}

我通常通过在服务器上设置超时来解决该问题。它工作得很好,但对我来说似乎是一个不完整的解决方案。

udpClient.Client.ReceiveTimeout = 5000;

while (Run)
{
    try
    {
        IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
        byte[] data = udpClient.Receive(ref remoteEndPoint); // blocking method

        // process received data
    }
    catch(SocketException ex) {} // timeout reached
}

你会如何处理这个问题?还有更好的办法吗?

In the following thread, UDP packets are read from clients until the boolean field Run is set to false.

If Run is set to false while the Receive method is blocking, it stays blocked forever (unless a client sends data, which will make the thread loop and check for the Run condition again).

while (Run)
{
    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
    byte[] data = udpClient.Receive(ref remoteEndPoint); // blocking method

    // process received data
}

I usually get around the problem by setting a timeout on the server. It works fine, but seems to be a patchy solution to me.

udpClient.Client.ReceiveTimeout = 5000;

while (Run)
{
    try
    {
        IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
        byte[] data = udpClient.Receive(ref remoteEndPoint); // blocking method

        // process received data
    }
    catch(SocketException ex) {} // timeout reached
}

How would you handle this problem? Is there any better way?

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

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

发布评论

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

评论(2

调妓 2024-11-05 07:40:42

您可以执行以下操作:

private bool run;
public bool Run
{
    get
    {
        return run;
    }

    set
    {
        run = value;
        if(!run) 
        {
            udpClient.Close();
        }

    }   
}

这允许您在满足任何条件以停止连接侦听时关闭客户端。可能会引发异常,但我不认为它会是 SocketTimeoutException,因此您需要处理该异常。

You could do something like this:

private bool run;
public bool Run
{
    get
    {
        return run;
    }

    set
    {
        run = value;
        if(!run) 
        {
            udpClient.Close();
        }

    }   
}

This allows you to close the client once whatever condition is met to stop your connection from listening. An exception will likely be thrown, but I don't believe it will be a SocketTimeoutException, so you'll need to handle that.

审判长 2024-11-05 07:40:41

使用 UdpClient.Close()。这将终止阻塞的 Receive() 调用。准备好捕获 ObjectDisposeException,它向您的线程发出套接字已关闭的信号。

Use UdpClient.Close(). That will terminate the blocking Receive() call. Be prepared to catch the ObjectDisposedException, it signals your thread that the socket is closed.

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