Example #1
0
        /// <summary>
        /// tcp服务端构造函数
        /// </summary>
        ///<param name="serverAddress">服务端IP地址,针对有多个网卡时通过配置指定</param>
        /// <param name="serverPort">tcp监听端口号</param>
        /// <param name="numOfMaxConnections">能接收的最大连接数</param>
        /// <param name="unitBufferSize">单个收或发缓冲区的大小</param>
        public TcpServerBase(IPAddress serverAddress, int serverPort, int numOfMaxConnections, int unitBufferSize)
        {
            try
            {                
                //会话数为零
                SessionStep = 0;

                //已连接的socket
                numOfConnectedSockets = 0;

                //最大连接数量
                maxConnections = numOfMaxConnections;

                //每个套接字的收发缓冲区大小
                bufferSize = unitBufferSize;

                // allocate buffers such that the maximum number of sockets can have one outstanding read and 
                //write posted to the socket simultaneously  
                bufferManager = new BufferManager(bufferSize * numOfMaxConnections * opsToPreAlloc, bufferSize);

                //可重用的SocketAsyncEventArgs对象池
                readWritePool = new SocketAsyncEventArgsPool(numOfMaxConnections);

                //控制最大连接数量的信号量
                maxNumOfAcceptedClients = new Semaphore(numOfMaxConnections, numOfMaxConnections);

                //初始化服务端参数值
                InitData();

                //服务端监听的套接字地址与端口
                ipAddress = serverAddress;

                listenPort = serverPort;

                //更改tcp参数的配置
                _InOptionValues = GetInOptionValues();

                //初始化Socket集合
                AsyncEventHashtable = new Hashtable();
            }
            catch (Exception ex)
            {
                Logger.Error(ex,null);
            }
        }
Example #2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <param name="udpPort"></param>
        /// <param name="numOfMaxConnections"></param>
        /// <param name="unitBufferSize"></param>
        public UdpServerBase(IPAddress ipAddress, int udpPort,int numOfMaxConnections,int unitBufferSize)
        {
            try
            {                
                //监听所有IP地址的相同端口
                IPAddress serverAddress = IPAddress.Parse("0.0.0.0");

                this.localPoint = new IPEndPoint(serverAddress, udpPort);

                //最大连接数量
                maxConnections = numOfMaxConnections;

                //每个套接字的收发缓冲区大小
                bufferSize = unitBufferSize;

                // allocate buffers such that the maximum number of sockets can have one outstanding read and 
                //write posted to the socket simultaneously  
                bufferManager = new BufferManager(bufferSize * numOfMaxConnections * opsToPreAlloc, bufferSize);

                //可重用的SocketAsyncEventArgs对象池
                readWritePool = new SocketAsyncEventArgsPool(numOfMaxConnections);

                //控制最大并发数量的信号量
                maxNumOfAcceptedClients = new Semaphore(numOfMaxConnections, numOfMaxConnections);

                //初始化服务端参数值
                InitData();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, null);
            }
        }