Esempio n. 1
0
        public bool Open(TcpOption socketSettings, int nPort, string strBindIp)
        {
            this.socketSettings         = socketSettings;
            this.ioEventArgsPool        = new ThreadSafeStack <SocketAsyncEventArgs>();
            this.acceptEventArgsPool    = new ThreadSafeStack <SocketAsyncEventArgs>();
            this.maxConnectionsEnforcer = new Semaphore(this.socketSettings.MaxConnections, this.socketSettings.MaxConnections);

            if (this.bufferManager == null)
            {
                this.bufferManager = new BufferManager(this.socketSettings.BufferSize * this.socketSettings.NumOfSaeaForRecSend, this.socketSettings.BufferSize);
                Initialize();
            }

            //-- 解析IP地址
            IPEndPoint localEndPoint;

            if (strBindIp != null)
            {
                localEndPoint = new IPEndPoint(IPAddress.Parse(strBindIp), nPort);
            }
            else
            {
                localEndPoint = new IPEndPoint(IPAddress.Any, nPort);
            }

            //-- 创建Socket
            listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            listenSocket.Bind(localEndPoint);
            listenSocket.Listen(nPort);
            _isStart = true;

            //-- 开始Accept请求
            PostAccept();

            return(true);
        }
Esempio n. 2
0
 public bool Open(TcpOption socketSettings, int nPort)
 {
     return(Open(socketSettings, nPort, null));
 }