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


C# UdpClient.BeginReceive方法代码示例

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


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

示例1: Backend

    public Backend(string ipAddress, int port)
    {
        // endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
        // this.EnableTimedTriggers();

        endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
        client = new UdpClient();
        client.Connect(endpoint);
        client.BeginReceive(new AsyncCallback(OnMessageRecieved), null);
    }
开发者ID:awatemonosan,项目名称:unity-essentials,代码行数:10,代码来源:Backend.cs

示例2: StartListen

    void StartListen()
    {
        // multicast receive setup
        remote_end = new IPEndPoint (IPAddress.Any, port);
        udp_client = new UdpClient (remote_end);
        udp_client.JoinMulticastGroup (group_address);

        // async callback for multicast
        udp_client.BeginReceive (new AsyncCallback (ReceiveAnnounceCallback), null);
    }
开发者ID:bosung90,项目名称:MadScientist,代码行数:10,代码来源:CMMulticastListener.cs

示例3: Main

    static void Main(string[] args)
    {
        int receiverPort = 2000;
        UdpClient receiver = new UdpClient(receiverPort);

        Console.WriteLine("port: " + receiverPort);

        receiver.BeginReceive(alinandata, receiver);

        Console.ReadKey();
    }
开发者ID:xdebron,项目名称:socketlistener,代码行数:11,代码来源:Program.cs

示例4: Init

    public void Init(string nothing)
    {
        remoteEndPoint = new IPEndPoint(IPAddress.Parse(remoteAddress), remotePort);
        timestamps = new Dictionary<string, Timestamp>();
        receivedMsgBuffer = new StringBuilder();
        completeMsgBuffer = new List<string>();

        try
        {
            TCPBuffer = new byte[1024];

            sendingUDPClient = new UdpClient();
            sendingUDPClient.ExclusiveAddressUse = false;
            sendingUDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            sendingUDPClient.Connect(remoteAddress, remotePort);

            receivingUDPClient = new UdpClient();
            receivingUDPClient.ExclusiveAddressUse = false;
            receivingUDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            receivingUDPClient.Client.Bind((IPEndPoint)sendingUDPClient.Client.LocalEndPoint);
            receivingUDPClient.Connect(remoteAddress, remotePort);
            receivingUDPClient.BeginReceive(new AsyncCallback(OnMessageUDP), null);

            // Send initial message to register the address and port at the server
            string hello = "Hello Server";
            byte[] bytes = Encoding.UTF8.GetBytes(hello);

            try
            {
                sendingUDPClient.Send(bytes, bytes.Length);
            }
            catch (Exception err)
            {
        #if !UNITY_EDITOR
                    Application.ExternalCall(onErrorCallback, "An error occurred while sending the initial message to the server: " + err.Message);
        #else
                    UnityEngine.Debug.LogError("An error occurred while sending the initial message to the server: " + err.Message);
        #endif
            }

            TCPClient = new TcpClient();
            TCPClient.NoDelay = true;
            TCPClient.Connect(remoteEndPoint);
            TCPClient.GetStream().BeginRead(TCPBuffer, 0, 1024, new AsyncCallback(OnMessageTCP), null);
        }
        catch (Exception err)
        {
        #if !UNITY_EDITOR
                Application.ExternalCall(onErrorCallback, err.ToString());
        #else
                UnityEngine.Debug.LogError("Init(): " + err.ToString());
        #endif
        }
    }
开发者ID:juniordiscart,项目名称:thesis,代码行数:54,代码来源:NativeCLient.cs

示例5: DiscoveryManager

 public DiscoveryManager()
 {
     ActivityServices = new List<ServiceInfo>();
     DiscoveryType = DiscoveryType.WsDiscovery;
     #if ANDROID
     _messageId = Guid.NewGuid().ToString();
     _udpClient = new UdpClient(WsDiscoveryPort);
     _udpClient.JoinMulticastGroup(IPAddress.Parse(WsDiscoveryIPAddress));
     _udpClient.BeginReceive(HandleRequest, _udpClient);
     #endif
 }
开发者ID:NikolajLund,项目名称:NooSphere,代码行数:11,代码来源:DiscoveryManager.cs

示例6: RecieveCandidates

    void RecieveCandidates()
    {
        IPEndPoint remote_end = new IPEndPoint (IPAddress.Any, startup_port);
        UdpClient udp_client = new UdpClient (remote_end);
        udp_client.JoinMulticastGroup (group_address);

        UdpState s = new UdpState();
        s.e = remote_end;
        s.u = udp_client;

        // async callback for multicast
        udp_client.BeginReceive (new AsyncCallback (ServerLookup), s);
    }
开发者ID:philipcass,项目名称:NetworkingDemo,代码行数:13,代码来源:ElectDiscover.cs

示例7: Main

	static int Main (string [] args)
	{
		int port = 8001;
		UdpClient udpClient = new UdpClient (port);
		IPAddress ip = IPAddress.Parse ("224.0.0.2");
		udpClient.JoinMulticastGroup (ip, IPAddress.Any);
		udpClient.MulticastLoopback = true;
		udpClient.Ttl = 1;
		udpClient.BeginReceive (ReceiveNotification, udpClient);
		udpClient.Send (new byte [1] { 255 }, 1, new IPEndPoint (ip, port));
		System.Threading.Thread.Sleep (1000);
		udpClient.DropMulticastGroup (ip);
		if (!_receivedNotification)
			return 1;
		return 0;
	}
开发者ID:mono,项目名称:gert,代码行数:16,代码来源:test.cs

示例8: ReceiveMessages

    public static void ReceiveMessages()
    {
        // Receive a message and write it to the console.
          e = new IPEndPoint(IPAddress.Any, 19000);
          u = new UdpClient(e);

          Console.WriteLine("listening for messages");
          u.BeginReceive(new AsyncCallback(ReceiveCallback), null);

          // Do some work while we wait for a message. For this example,
          // we'll just sleep
          while (true)
          {
        Thread.Sleep(100);
          }
    }
开发者ID:njoubert,项目名称:spooky,代码行数:16,代码来源:001_hello.cs

示例9: udpRestart

    public void udpRestart()
    {
        if (_udpClient != null)
        {
            _udpClient.Close();
        }

        _stringsToParse = new List<byte[]>();

        _anyIP = new IPEndPoint(IPAddress.Any, TrackerProperties.Instance.listenPort);

        _udpClient = new UdpClient(_anyIP);

        _udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);

        Debug.Log("[UDPListener] Receiving in port: " + TrackerProperties.Instance.listenPort);
    }
开发者ID:mauriciosousa,项目名称:creepytracker,代码行数:17,代码来源:UdpListener.cs

示例10: Start

    void Start()
    {
        foreach (PositionUpdateBehavior pub in GameObject.FindObjectsOfType<PositionUpdateBehavior>())
        {
            updateBehaviors.Add(pub);
        }

        client = new UdpClient(port);

        try
        {
            client.BeginReceive(new AsyncCallback(AcceptCallback), null);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.StackTrace);
        }
    }
开发者ID:sweenr,项目名称:ped-sim,代码行数:18,代码来源:PositionUpdateServer.cs

示例11: ListenServer

    /********************/
    /****** CLIENT ******/
    /********************/
    public void ListenServer()
    {
        // open a listening port on a random port
        // to receive a response back from server
        // using 0 doesn't seem to work reliably
        // so we'll just do it ourselves

        int myPort = UnityEngine.Random.Range(15001,16000);

        IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, myPort);
        UdpClient uc1 = new UdpClient(ep1);
        UdpState us1 = new UdpState();
        us1.e = ep1;
        us1.u = uc1;
        uc1.BeginReceive(new AsyncCallback(ListenServerCallback), us1);
        broadcastClient = uc1;
        broadcastEndPoint = ep1;

        Debug.Log("Broadcast listener opened on port " + broadcastEndPoint.Port.ToString());
    }
开发者ID:bernarde-bgi,项目名称:bgirpsg,代码行数:23,代码来源:networkController.cs

示例12: ListenForClients

    /********************/
    /****** SERVER ******/
    /********************/
    public void ListenForClients(string g)
    {
        // open a listening port on known port 15000
        // to listen for any clients

        IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, 15000);
        UdpClient uc1 = new UdpClient(ep1);
        UdpState us1 = new UdpState();
        us1.e = ep1;
        us1.u = uc1;
        uc1.BeginReceive(new AsyncCallback(ListenForClientsCallback), us1);

        multiGameName = g;

        waitingResponse = true;

        Debug.Log("Server listening port opened");
    }
开发者ID:bernarde-bgi,项目名称:bgirpsg,代码行数:21,代码来源:networkController.cs

示例13: 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

示例14: 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

示例15: ListenForClients

    //specific to LAN
    private void ListenForClients(int _ListenPort)
    {
        System.Diagnostics.Debug.Assert(m_BroadcastListeningClientLAN == null);

        // open a listening port on known port
        // to listen for any clients

        IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, _ListenPort);
        UdpClient uc1 = new UdpClient(ep1);
        UdpClientState ucs1 = new UdpClientState(ep1, uc1);

        uc1.BeginReceive(new System.AsyncCallback(ListenForClientsCallback), ucs1);

        m_BroadcastListeningClientLAN = ucs1;

        Debug.Log("Server listening for clients on port: " + _ListenPort);
    }
开发者ID:mhoulier,项目名称:unity3d_template,代码行数:18,代码来源:NetworkServer.cs


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