/// <summary> /// 启动 /// </summary> private void Run() { if (isDisposed == true) { throw new ObjectDisposedException("ZYServer is Disposed"); } IPEndPoint myEnd = new IPEndPoint(IPAddress.Any, Port); if (!Host.Equals("any", StringComparison.CurrentCultureIgnoreCase)) { if (String.IsNullOrEmpty(Host)) { IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress s in p.AddressList) { if (!s.IsIPv6LinkLocal && s.AddressFamily != AddressFamily.InterNetworkV6) { myEnd = new IPEndPoint(s, Port); break; } } } else { try { myEnd = new IPEndPoint(IPAddress.Parse(Host), Port); } catch (FormatException) { IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress s in p.AddressList) { if (!s.IsIPv6LinkLocal) { myEnd = new IPEndPoint(s, Port); } } } } } sock = new Socket(myEnd.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); if (Environment.OSVersion.Platform.ToString().IndexOf("NT") >= 0) //WINDOWS NT平台 { //add by john at 2012-12-03 用户检测用户断开 心跳处理 uint dummy = 0; byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3]; BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0); BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy)); BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2); sock.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); //------------------------- } sock.Bind(myEnd); sock.Listen(20); SendTimeout = 1000; ReceiveTimeout = 1000; BuffManagers = new BufferManager(MaxConnectCout * MaxBufferSize, MaxBufferSize); BuffManagers.Inint(); SocketAsynPool = new SocketAsyncEventArgsPool(MaxConnectCout); for (int i = 0; i < MaxConnectCout; i++) { SocketAsyncEventArgs socketasyn = new SocketAsyncEventArgs(); socketasyn.Completed += new EventHandler <SocketAsyncEventArgs>(Asyn_Completed); SocketAsynPool.Push(socketasyn); } Accept(); }
private void Run() { if (this.isDisposed) { throw new ObjectDisposedException("ZYServer is Disposed"); } IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, this.Port); if (!this.Host.Equals("any", StringComparison.CurrentCultureIgnoreCase)) { if (string.IsNullOrEmpty(this.Host)) { IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] addressList = hostEntry.AddressList; for (int i = 0; i < addressList.Length; i++) { IPAddress iPAddress = addressList[i]; if (!iPAddress.IsIPv6LinkLocal && iPAddress.AddressFamily != AddressFamily.InterNetworkV6) { iPEndPoint = new IPEndPoint(iPAddress, this.Port); break; } } } else { try { iPEndPoint = new IPEndPoint(IPAddress.Parse(this.Host), this.Port); } catch (FormatException) { IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] addressList = hostEntry.AddressList; for (int i = 0; i < addressList.Length; i++) { IPAddress iPAddress = addressList[i]; if (!iPAddress.IsIPv6LinkLocal) { iPEndPoint = new IPEndPoint(iPAddress, this.Port); } } } } } this.sock = new Socket(iPEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); byte[] optionInValue = new byte[] { 1, 0, 0, 0, 48, 117, 0, 0, 208, 7, 0, 0 }; this.sock.IOControl(IOControlCode.KeepAliveValues, optionInValue, null); this.sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); this.sock.Bind(iPEndPoint); this.sock.Listen(20); checked { this.BuffManagers = new BufferManager(this.MaxConnectCout * this.MaxBufferSize, this.MaxBufferSize); this.BuffManagers.Inint(); this.SocketAsynPool = new SocketAsyncEventArgsPool(this.MaxConnectCout); for (int j = 0; j < this.MaxConnectCout; j++) { SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs(); socketAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(this.Asyn_Completed); this.SocketAsynPool.Push(socketAsyncEventArgs); } this.Accept(); } }
/// <summary> /// 启动 /// </summary> private void Run() { if (isDisposed == true) { throw new ObjectDisposedException("ZYServer is Disposed"); } myEnd = new IPEndPoint(IPAddress.Any, Port); if (!Host.Equals("any", StringComparison.CurrentCultureIgnoreCase)) { if (!String.IsNullOrEmpty(Host)) { IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress s in p.AddressList) { if (!s.IsIPv6LinkLocal) myEnd = new IPEndPoint(s, Port); } } else { try { myEnd = new IPEndPoint(IPAddress.Parse(Host), Port); } catch (FormatException) { IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress s in p.AddressList) { if (!s.IsIPv6LinkLocal) myEnd = new IPEndPoint(s, Port); } } } } sock = new Socket(myEnd.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); sock.Bind(myEnd); sock.Listen(20); SendTimeout = 1000; ReceiveTimeout = 1000; BuffManagers = new BufferManager(MaxConnectCout * MaxBufferSize, MaxBufferSize); BuffManagers.Inint(); SocketAsynPool = new SocketAsyncEventArgsPool(MaxConnectCout); for (int i = 0; i < MaxConnectCout; i++) { SocketAsyncEventArgs socketasyn = new SocketAsyncEventArgs(); SocketAsynPool.Push(socketasyn); } Accept(); }
/// <summary> /// 启动 /// </summary> private void Run() { if (isDisposed == true) { throw new ObjectDisposedException("ZYServer is Disposed"); } IPEndPoint myEnd = new IPEndPoint(IPAddress.Any, Port); if (!Host.Equals("any",StringComparison.CurrentCultureIgnoreCase)) { if (String.IsNullOrEmpty(Host)) { IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress s in p.AddressList) { if (!s.IsIPv6LinkLocal && s.AddressFamily != AddressFamily.InterNetworkV6) { myEnd = new IPEndPoint(s, Port); break; } } } else { try { myEnd = new IPEndPoint(IPAddress.Parse(Host), Port); } catch (FormatException) { IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress s in p.AddressList) { if (!s.IsIPv6LinkLocal) myEnd = new IPEndPoint(s, Port); } } } } sock = new Socket(myEnd.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); if (Environment.OSVersion.Platform.ToString().IndexOf("NT") >= 0) //WINDOWS NT平台 { //add by john at 2012-12-03 用户检测用户断开 心跳处理 uint dummy = 0; byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3]; BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0); BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy)); BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2); sock.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); //------------------------- } sock.Bind(myEnd); sock.Listen(20); SendTimeout = 1000; ReceiveTimeout = 1000; BuffManagers = new BufferManager(MaxConnectCout * MaxBufferSize, MaxBufferSize); BuffManagers.Inint(); SocketAsynPool = new SocketAsyncEventArgsPool(MaxConnectCout); for (int i = 0; i < MaxConnectCout; i++) { SocketAsyncEventArgs socketasyn = new SocketAsyncEventArgs(); socketasyn.Completed += new EventHandler<SocketAsyncEventArgs>(Asyn_Completed); SocketAsynPool.Push(socketasyn); } Accept(); }
/// <summary> /// 启动 /// </summary> private void Run() { if (isDisposed == true) { throw new ObjectDisposedException("ZYServer is Disposed"); } myEnd = new IPEndPoint(IPAddress.Any, Port); if (!Host.Equals("any", StringComparison.CurrentCultureIgnoreCase)) { if (!String.IsNullOrEmpty(Host)) { IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress s in p.AddressList) { if (!s.IsIPv6LinkLocal) { myEnd = new IPEndPoint(s, Port); } } } else { try { myEnd = new IPEndPoint(IPAddress.Parse(Host), Port); } catch (FormatException) { IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress s in p.AddressList) { if (!s.IsIPv6LinkLocal) { myEnd = new IPEndPoint(s, Port); } } } } } sock = new Socket(myEnd.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); sock.Bind(myEnd); sock.Listen(20); SendTimeout = 1000; ReceiveTimeout = 1000; BuffManagers = new BufferManager(MaxConnectCout * MaxBufferSize, MaxBufferSize); BuffManagers.Inint(); SocketAsynPool = new SocketAsyncEventArgsPool(MaxConnectCout); for (int i = 0; i < MaxConnectCout; i++) { SocketAsyncEventArgs socketasyn = new SocketAsyncEventArgs(); SocketAsynPool.Push(socketasyn); } Accept(); }