/// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> /// <param name="disposing"><c>true</c> to release /// both managed and unmanaged resources; <c>false</c> /// to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { try { // 关闭文件传输服务器 //this.m_fileTcpServer.Stop(); //if (this.m_fileTcpServer != null) //{ // this.m_fileTcpServer = null; //} // 关闭word工作线程 this.m_mswordServer.Stop(); if (this.m_mswordServer != null) { this.m_mswordServer = null; } // 关闭服务器 this.Stop(); if (this.m_serverSocket != null) { this.m_serverSocket = null; } } catch (SocketException) { //TODO RaiseOtherException(null); } } disposed = true; } }
/// <summary> /// 异步Socket TCP服务器 /// </summary> /// <param name="localIPAddress">监听的IP地址</param> /// <param name="listenPort">监听的端口</param> /// <param name="maxClientCount">最大客户端数量</param> public AsyncSocketServer(IPAddress localIPAddress, int listenPort, int maxClientCount) { //Console.WriteLine("IP " + localIPAddress + ", PORT " + listenPort); this.Address = localIPAddress; this.Port = listenPort; this.Encoding = Encoding.Default; this.m_maxClientCount = maxClientCount; this.m_clientList = new List<AsyncSocketState>(); this.m_serverSocket = new Socket(localIPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); /* * 参数 * ioControlCode * 一个 IOControlCode 值,它指定要执行的操作的控制代码。 * optionInValue * Byte 类型的数组,包含操作要求的输入数据。 * optionOutValue * Byte 类型的数组,包含由操作返回的输出数据。 */ this.m_serverSocket.IOControl(IOControlCode.DataToRead, null, BitConverter.GetBytes(0)); this.m_serverSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); // 心跳包http://www.cnblogs.com/csMapx/archive/2011/09/04/2166515.html //uint dummy = 0; //byte[] inOptionValues = new byte[System.Runtime.InteropServices.Marshal.SizeOf(dummy) * 3]; //BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);//是否启用Keep-Alive //BitConverter.GetBytes((uint)120000).CopyTo(inOptionValues, System.Runtime.InteropServices.Marshal.SizeOf(dummy));//多长时间开始第一次探测 //BitConverter.GetBytes((uint)120000).CopyTo(inOptionValues, System.Runtime.InteropServices.Marshal.SizeOf(dummy) * 2);//探测时间间隔 //this.m_serverSocket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); //this.m_serverSocket.Bind(new IPEndPoint(this.Address, this.Port)); // 绑定服务器和端口 //this.m_serverSocket.Listen(1024); // 相当于如下代码 //byte[] buffer = new byte[12]; //BitConverter.GetBytes(1).CopyTo(buffer, 0); // 是否启用Keep-Alive //BitConverter.GetBytes(1000).CopyTo(buffer, 4); // 多长时间开始第一次探测 //BitConverter.GetBytes(1000).CopyTo(buffer, 8); // 探测时间间隔 /// ClientSocket.IOControl(IOControlCode.KeepAliveValues, buffer, null); /// 因此心跳包的设置我们长采用如下代码 /// http://blog.csdn.net/educast/article/details/7597829 /// http://www.cnblogs.com/csMapx/archive/2011/09/04/2166515.html /////////////// // 创建日志文件[2015-6-16 21:10] /////////////// this.m_log = new LogerHelper(); // 创建一个日志对象,默认设置每天更新一个日志文件 /////////////// // 开启文件传输的TCP服务器 /////////////// this.m_fileTcpServer = new FileSocketServer(); this.m_fileTcpServer.BeginListening(IPAddress.Any, FILE_TCP_SERVRT_PORT); /////////////// // 开始生成文件的服务 /////////////// this.m_mswordServer = new MSWordTools(); }