/// <summary> /// 绑定 /// </summary> /// <param name="port"></param> /// <param name="host"></param> /// <returns></returns> public bool Bind(int port = 0, string host = null) { try { host = NetAddress.GetLocalIP(); if (string.IsNullOrEmpty(host)) { socket.Bind(new IPEndPoint(IPAddress.Any, port)); } else { socket.Bind(new IPEndPoint(IPAddress.Parse(host), port)); } return(true); } catch { return(false); } }
/// <summary> /// 绑定 /// </summary> /// <param name="port"></param> /// <returns></returns> public bool Bind(int port = 0) { try { string host = NetAddress.GetLocalIP(); IPAddress address = IPAddress.Any; if (!string.IsNullOrEmpty(host)) { address = IPAddress.Parse(host); } Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(new IPEndPoint(address, port)); socket.Listen(100); Thread threadReceive = new Thread(new ParameterizedThreadStart(StartListen)); threadReceive.IsBackground = true; threadReceive.Start(socket); LocalPoint = (IPEndPoint)socket.LocalEndPoint; return(true); } catch { return(false); } }