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


C# UdpClient.BeginReceive方法代码示例

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


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

示例1: thread

        private void thread()
        {
            UdpClient ucl = new UdpClient(6000);
            IAsyncResult iar = ucl.BeginReceive(null, null);

            while (!exit)
            {
                if (!iar.AsyncWaitHandle.WaitOne(1000))
                    continue;

                IPEndPoint ep = new IPEndPoint(0, 0);
                byte[] data = ucl.EndReceive(iar, ref ep);
                iar = ucl.BeginReceive(null, 0);

                using (MemoryStream ms = new MemoryStream(data))
                {
                    BinaryReader br = new BinaryReader(ms);

                    while (ms.Position < ms.Length)
                    {
                        CanMessage m = CanMessage.DeserializeFrom(br);

                        if (MessageReceived != null)
                            MessageReceived(this, m);
                    }
                }
            }

            ucl.Close();
        }
开发者ID:BuFran,项目名称:canshark,代码行数:30,代码来源:CanSharkBoard.cs

示例2: Start

    /// <summary>
    ///   Start listening on udp port
    /// </summary>
    public override void Start()
    {
      Log.Debug("PortUdp.start");

      try
      {
        Log.Debug("Start waiting for a connection...");
        _listen = true;


        var ip = GetLocalIp();
        var ep = new IPEndPoint(ip, _port);
        var u = new UdpClient();
        u.EnableBroadcast = true;
        u.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        u.Client.Bind(ep);
        _state = new UdpState();
        _state.e = ep;
        _state.u = u;
        u.BeginReceive(ReceiveCallback, _state);

        base.Start();
      }
      catch (Exception e)
      {
        Log.Error("PortUdp.Start error", e);
      }
    }
开发者ID:vinmenn,项目名称:HBus.DotNet,代码行数:31,代码来源:PortUdp.cs

示例3: Client

 public Client()
 {
     IPEndPoint myEndPoint = new IPEndPoint(IPAddress, 8307);
     _udpClient = new UdpClient(myEndPoint);
     List<object> state = new List<object> { myEndPoint, _udpClient };
     _udpClient.BeginReceive(OnUdpDataReceived, state);
 }
开发者ID:Zeraan,项目名称:Xasteroids,代码行数:7,代码来源:Client.cs

示例4: ConnectionManager

        public ConnectionManager(int nPortListen)
        {
            listenPort = nPortListen;
            udpReceiver = new UdpClient(listenPort);

            udpReceiver.BeginReceive(new AsyncCallback(OnUdpReceived), udpReceiver);
        }
开发者ID:eaglezhao,项目名称:tracnghiemweb,代码行数:7,代码来源:ConnectionManager.cs

示例5: Discover

        public void Discover()
        {
            m_endpoints = new HashSet<IPEndPoint>();
            //m_discover = new UdpClient(new IPEndPoint(IPAddress.Broadcast, RemoteRunner.DiscoverPort));
            m_discover = new UdpClient();
            m_discover.EnableBroadcast = true;
            var endpoint = new IPEndPoint(IPAddress.Broadcast, RemoteRunner.DiscoverPort);

            for (int i = 0; i < NumBroadcast; i++)
            {
                m_discover.Send(RemoteRunner.DiscoverPackage, RemoteRunner.DiscoverPackage.Length, endpoint);
                m_discover.BeginReceive(DiscoverCallback, null);
                Thread.Sleep(BroadcastDelay);
            }

            m_discover.Close();
            m_clients = new List<TcpClient>();

            foreach (var addr in m_endpoints)
            {
                Console.WriteLine(addr);
                var cl = new TcpClient();
                cl.BeginConnect(addr.Address, RemoteRunner.ConnectionPort, ConnectCallback, cl);
            }
        }
开发者ID:CurlyBrackets,项目名称:DynamicLibrary,代码行数:25,代码来源:RemoteExecutor.cs

示例6: SearchSniffer

        public SearchSniffer()
        {
            IPAddress[] LocalAddresses = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            ArrayList temp = new ArrayList();
            foreach (IPAddress i in LocalAddresses) temp.Add(i);
            temp.Add(IPAddress.Loopback);
            LocalAddresses = (IPAddress[])temp.ToArray(typeof(IPAddress));

            for (int id = 0; id < LocalAddresses.Length; ++id)
            {
                try
                {
                    var localEndPoint = new IPEndPoint(LocalAddresses[id], 1900);
                    UdpClient ssdpSession = new UdpClient(localEndPoint);
                    ssdpSession.MulticastLoopback = false;
                    ssdpSession.EnableBroadcast = true;
                    if (localEndPoint.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ssdpSession.JoinMulticastGroup(UpnpMulticastV4Addr, LocalAddresses[id]);
                    }

                    uint IOC_IN = 0x80000000;
                    uint IOC_VENDOR = 0x18000000;
                    uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
                    ssdpSession.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);

                    ssdpSession.BeginReceive(new AsyncCallback(OnReceiveSink), new object[] { ssdpSession, localEndPoint });
                    SSDPSessions[ssdpSession] = ssdpSession;
                }
                catch (Exception) { }
            }
        }
开发者ID:RyanMelenaNoesis,项目名称:ElveVenstarColorTouchDriver,代码行数:32,代码来源:SearchSniffer.cs

示例7: UDPListener

        public UDPListener(int port)
        {
            Port = port;
            queue = new Queue<byte[]>();
            ClosingEvent = new ManualResetEvent(false);
            callbackLock = new object();

            // try to open the port 10 times, else fail
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    receivingUdpClient = new UdpClient(port);
                    break;
                }
                catch (Exception)
                {
                    // Failed in ten tries, throw the exception and give up
                    if (i >= 9)
                        throw;

                    Thread.Sleep(5);
                }
            }
            RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            // setup first async event
            AsyncCallback callBack = new AsyncCallback(ReceiveCallback);
            receivingUdpClient.BeginReceive(callBack, null);
        }
开发者ID:tklebanoff,项目名称:kinect2osc,代码行数:30,代码来源:UDPListener.cs

示例8: beginReceivingUdp

 public void beginReceivingUdp(int port)
 {
     client = new UdpClient(port);
     sourceIp = new IPEndPoint(IPAddress.Any, port);
     bool isComplete = true;
     IAsyncResult udpCallback = null;
     try
     {
         while (true)
         {
             if (isComplete)
             {
                 udpCallback = client.BeginReceive(new AsyncCallback(callback), null);
                 isComplete = false;
             }
             else
             {
                 if (udpCallback.IsCompleted == true) { isComplete = true; }
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("W10.001.01 Exception during receiving of udp, Exception[" + ex + "]");
     }
 }
开发者ID:fierc3,项目名称:SysLogViewer,代码行数:26,代码来源:UDPReciever.cs

示例9: StartServer

        public void StartServer()
        {
            //var client = new MiNetClient(new IPEndPoint(Dns.GetHostEntry("pe.mineplex.com").AddressList[0], 19132), "TheGrey");
            _serverTargetEndpoint = new IPEndPoint(Dns.GetHostEntry("yodamine.com").AddressList[0], 19135);
            //_serverTargetEndpoint = new IPEndPoint(IPAddress.Parse("147.75.194.126"), 19132);
            _serverListenerEndPoint = Endpoint = new IPEndPoint(IPAddress.Any, 19134);

            _listener = new UdpClient(Endpoint);
            _listener.Client.ReceiveBufferSize = int.MaxValue;
            _listener.Client.SendBufferSize = int.MaxValue;
            _listener.DontFragment = false;
            _listener.EnableBroadcast = false;

            // SIO_UDP_CONNRESET (opcode setting: I, T==3)
            // Windows:  Controls whether UDP PORT_UNREACHABLE messages are reported.
            // - Set to TRUE to enable reporting.
            // - Set to FALSE to disable reporting.

            uint IOC_IN = 0x80000000;
            uint IOC_VENDOR = 0x18000000;
            uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;

            _listener.Client.IOControl((int) SIO_UDP_CONNRESET, new byte[] {Convert.ToByte(false)}, null);
            _listener.BeginReceive(ReceiveCallback, _listener);
        }
开发者ID:CRBairdUSA,项目名称:MiNET,代码行数:25,代码来源:MiNetFtlServer.cs

示例10: UPnPSearchSniffer

        public UPnPSearchSniffer()
        {
            IPAddress[] LocalAddresses = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            ArrayList temp = new ArrayList();
            foreach (IPAddress i in LocalAddresses) temp.Add(i);
            temp.Add(IPAddress.Loopback);
            LocalAddresses = (IPAddress[])temp.ToArray(typeof(IPAddress));

            for (int id = 0; id < LocalAddresses.Length; ++id)
            {
                try
                {
                    UdpClient ssdpSession = new UdpClient(new IPEndPoint(LocalAddresses[id], 0));
                    ssdpSession.EnableBroadcast = true;

                    if (!Utils.IsMono())
                    {
                        uint IOC_IN = 0x80000000;
                        uint IOC_VENDOR = 0x18000000;
                        uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
                        ssdpSession.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
                    }

                    ssdpSession.BeginReceive(new AsyncCallback(OnReceiveSink), ssdpSession);
                    SSDPSessions[ssdpSession] = ssdpSession;
                }
                catch (Exception) { }
            }
        }
开发者ID:nothingmn,项目名称:UPnP-for-C---Intel-,代码行数:29,代码来源:UPnPSearchSniffer.cs

示例11: UDPThread

		/// <summary>
		/// 构造一个新的消息对象,并绑定到指定的端口和IP上。
		/// </summary>
		/// <param name="ip">绑定的IP</param>
		/// <param name="port">绑定的端口</param>
		internal UDPThread(IPMClient ipmClient)
		{
			IsInitialized = false;
			try
			{
				client = new UdpClient(new IPEndPoint(ipmClient.Config.BindedIP, ipmClient.Config.Port));
			}
			catch (Exception)
			{
				OnNetworkError(new EventArgs());
				return;
			}
			SendList = new List<PackedNetworkMessage>();
			client.EnableBroadcast = true;
			_ipmClient = ipmClient;

			CheckQueueTimeInterval = 2000;
			MaxResendTimes = 5;
			new System.Threading.Thread(new System.Threading.ThreadStart(CheckUnConfirmedQueue)) { IsBackground = true }.Start();


			IsInitialized = true;

			//开始监听
			client.BeginReceive(EndReceiveDataAsync, null);
		}
开发者ID:iraychen,项目名称:IpMsg.Net,代码行数:31,代码来源:UDPThread.cs

示例12: ServerUDPTunnel

 public ServerUDPTunnel(ServerPlayer player)
 {
     // enstablish an Connection
     this.player = player;
     udp = new UdpClient(new IPEndPoint(IPAddress.Any, Server.instance.UDPStartPort + (int)player.id));
     udp.BeginReceive(onReceive, null);
 }
开发者ID:andrefsantos,项目名称:gta-iv-multiplayer,代码行数:7,代码来源:ServerUDPTunnel.cs

示例13: UdpGetInfoByIPPort

        //udp通讯类byLeo
        #region
        public string[] UdpGetInfoByIPPort(string ipAddress, int ipPort, string cmdstr, int idelay)
        {
            if (basefun.IsCorrenctIP(ipAddress) == false) return null;
            if (idelay == 0) idelay = 100;
            int timeout = idelay;
            byte[] bytOutBuffer = getByteBy16s(cmdstr);
            IPEndPoint RemoteIpEndPoint = SetIPEndPoint(ipAddress, ipPort);
            if (RemoteIpEndPoint == null) return null;
            byte[] bytReceiveBuffer;
            using (UdpClient udpClient = new UdpClient())
            {
                udpClient.Send(bytOutBuffer, bytOutBuffer.Length, RemoteIpEndPoint);
                IPEndPoint from = new IPEndPoint(IPAddress.Any, 0);
                IAsyncResult result = udpClient.BeginReceive(null, this);
                result.AsyncWaitHandle.WaitOne(timeout, false);
                if (!result.IsCompleted)
                {
                    //throw SharpTimeoutException.Create(ipAddress, timeout);
                    udpClient.Close();
                    return null;
                }

                bytReceiveBuffer = udpClient.EndReceive(result, ref from);
                string udpInfo = get16sByByte(bytReceiveBuffer);
                udpInfo = SpecialRestore(udpInfo, "dddb", "db");
                udpInfo = SpecialRestore(udpInfo, "dcdb", "c0");
                //取返回值16进制数组,并解析
                string[] infos = AnalysisEateryResults(udpInfo);
                udpClient.Close();
                return infos;
            }

        }
开发者ID:thisisvoa,项目名称:GranityApp2.5,代码行数:35,代码来源:ComClass.cs

示例14: Initialize

        public void Initialize()
        {
            tcpServer = new TcpListener(IPAddress.Any, 1901);

            tcpServer.BeginAcceptTcpClient()

            sender = new UdpClient();

            sender.DontFragment = true;

            sender.JoinMulticastGroup(remoteEndPoint.Address);

            listener = new UdpClient();

            listener.ExclusiveAddressUse = false;

            listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            listener.ExclusiveAddressUse = false;

            listener.Client.Bind(anyEndPoint);

            listener.DontFragment = true;

            listener.JoinMulticastGroup(remoteEndPoint.Address);

            listener.BeginReceive(ReceiveCallback, null);
        }
开发者ID:DisruptionTheory,项目名称:Iris,代码行数:28,代码来源:IrisNetworkManager.cs

示例15: EndPointReflector

 protected EndPointReflector()
 {
     log.Debug("Create an EndPointReflector");
     keepGoing = true;
     udpClient = new UdpClient(endPointReflectorPort, AddressFamily.InterNetwork);
     log.DebugFormat("Start listening for an incoming request on port {0}", ((IPEndPoint) udpClient.Client.LocalEndPoint).Port);
     udpClient.BeginReceive(ReceiveCallback, null);
 }
开发者ID:swclyde,项目名称:USU_CS5200_SP14_BSvsZP,代码行数:8,代码来源:EndPointReflector.cs


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