/// <summary> /// 侦听 /// </summary> /// <param name="LocalIp">本地IP</param> /// <param name="Port">本地端口</param> public void Listen(IPAddress LocalIp, int Port) { try { IPEndPoint ipEnd = new IPEndPoint(LocalIp, Port); rUDP = new RUDPSocket( ); this.Listened = true;//侦听 this.ListenPort = Port; rUDP.Listen(Port); if (IsAsync)//如果采用异步通信 { rUDP.BeginReceive(new AsyncCallback(ReadCallback), null); } else//如果是同步通信 { thdUdp = new Thread(new ThreadStart(GetUDPData)); thdUdp.Start(); } } catch (Exception e) { if (Sock_Error != null) { Sock_Error(this, new SockEventArgs(e.Source + "," + e.Message)); } } }
/// <summary> /// 侦听 /// </summary> /// <param name="Port">侦听端口</param> public void Listen(int Port) { try { if (Port < 1 || Port > 65534) { System.Random r = new System.Random(); resetPort: Port = r.Next(2000, 65534); try { rUDP = new RUDPSocket( ); } catch { goto resetPort; } } else { rUDP = new RUDPSocket(); } Listened = true;//侦听 ListenPort = Port; rUDP.Listen(Port); if (IsAsync)//如果采用异步通信 { rUDP.BeginReceive(new AsyncCallback(ReadCallback), null); } else//如果是同步通信 { thdUdp = new Thread(new ThreadStart(GetUDPData)); thdUdp.Start(); } } catch (Exception e) { if (Sock_Error != null) { Sock_Error(this, new SockEventArgs(e.Source + "," + e.Message)); } } //LocalEndPoint = (IPEndPoint)rUDP.lClient.LocalEndPoint; }