重用线程和 UdpClient 在同一端口上发送和接收

本文关键字:线程 UdpClient | 更新日期: 2023-09-27 18:36:20

我正在尝试重用线程和UdpClient在同一端口上发送和接收,但是当我调用方法时。

private void RecivedCallback(IAsyncResult ar)
{
    try
    {
        if (ar == cuurentsynResult)
        {
            UdpClient c = (UdpClient)((UdpState)(ar.AsyncState)).c;
            IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
            Byte[] buffer = c.EndReceive(ar, ref e);
            if (buffer.Length > 0)
                System.Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer));
            UdpState s = new UdpState(e, c);
            cuurentsynResult = udpclient.BeginReceive(new AsyncCallback(RecivedCallback), s);                    
        }
    }
    catch (Exception ex)
    {
        string str = ex.Message;
    }
}

我面临的例外是..远程主机强行关闭了现有连接那么我该如何解决这个问题,请发送您的意见或答案

索努·兰詹

重用线程和 UdpClient 在同一端口上发送和接收

在 UDP 套接字上,该错误可能意味着您收到了"端口无法访问的 ICMP "消息,以响应您发送的数据报。

如何从中恢复将特定于应用程序。