当前位置: 首页>>代码示例>>C#>>正文


C# UdpClient.EndReceive方法代码示例

本文整理汇总了C#中UdpClient.EndReceive方法的典型用法代码示例。如果您正苦于以下问题:C# UdpClient.EndReceive方法的具体用法?C# UdpClient.EndReceive怎么用?C# UdpClient.EndReceive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UdpClient的用法示例。


在下文中一共展示了UdpClient.EndReceive方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SendUdpNetworkPacket

    // Actually builds, sends, sets the received bytes and returns the whole packet
    private EDNSPacket SendUdpNetworkPacket(string sDNSIP, string sIPToResolve, int nPort, byte bType, bool bEDNS, byte[] bMachine_ID)
    {
        // Create empty EDNS packet
        EDNSPacket Packet = new EDNSPacket();

        try
        {
            IPEndPoint Endpoint = new IPEndPoint(IPAddress.Parse(sDNSIP), nPort);

            // Send the current machine_id host
            if (bEDNS)
                Packet.CreateEDNSPacketMachineID(sIPToResolve, bType, bMachine_ID);
            else
                Packet.CreateDNSPacket(sIPToResolve, bType);

            if (Packet.GetPacketLen() > 0)
            {
                // Create a udp client to send the packet
                UdpClient udpGo = new UdpClient();
                udpGo.Client.SendTimeout = 2000;
                udpGo.Client.ReceiveTimeout = 2000;
                udpGo.Send(Packet.GetPacket(), Packet.GetPacket().Length, Endpoint);

                //IPEndPoint object will allow us to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

                // Launch Asynchronous
                IAsyncResult iarResult = udpGo.BeginReceive(null, null);

                // Wait until complete
                bool bKill = false;
                DateTime dtTimeStart = DateTime.Now;
                while (iarResult.IsCompleted == false)
                {
                    // Sleep instead of cycling
                    System.Threading.Thread.Sleep(100);

                    // Watchdog, if doesn't return in 5 seconds get out
                    if (dtTimeStart.AddSeconds(5) < DateTime.Now)
                    {
                        bKill = true;
                        break;
                    }
                }

                // This can hang when not happy about a broken connection
                if (bKill)
                    udpGo.Close();
                else
                    Packet.SetReceivePacket(udpGo.EndReceive(iarResult, ref RemoteIpEndPoint));
            }
        }
        catch (Exception Ex)
        {
            // TODO: Log an exception?
        }

        // Always just return packet
        return Packet;
    }
开发者ID:mind0n,项目名称:hive,代码行数:61,代码来源:TimedEventManager.cs

示例2: Receive

 /// <summary>
 /// Asynchronously listens on the given client for a single packet.
 /// </summary>
 public static void Receive(UdpClient Client, ReceiveRawPacketHandler OnReceive)
 {
     // Good job Microsoft, for making this so easy O_O
     while (true)
     {
         try
         {
             Client.BeginReceive(delegate(IAsyncResult ar)
             {
                 lock (Client)
                 {
                     IPEndPoint end = new IPEndPoint(IPAddress.Any, 0);
                     byte[] data;
                     try
                     {
                         data = Client.EndReceive(ar, ref end);
                         OnReceive(end, data);
                     }
                     catch (SocketException se)
                     {
                         if (se.SocketErrorCode == SocketError.Shutdown)
                         {
                             return;
                         }
                         if (_CanIgnore(se))
                         {
                             Receive(Client, OnReceive);
                         }
                         else
                         {
                             throw se;
                         }
                     }
                     catch (ObjectDisposedException)
                     {
                         return;
                     }
                 }
             }, null);
             return;
         }
         catch (SocketException se)
         {
             if (!_CanIgnore(se))
             {
                 throw se;
             }
         }
         catch (ObjectDisposedException)
         {
             return;
         }
     }
 }
开发者ID:AshleighAdams,项目名称:UDPX,代码行数:57,代码来源:Interface.cs


注:本文中的UdpClient.EndReceive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。