Пример #1
0
        private void Accept_Completed(object sender, SocketAsyncEventArgs e)
        {
            StateObject state = null;

            if (e.SocketError == SocketError.Success)
            {
                var io = _ioEventPool.Pop();
                state = (io.UserToken as StateObject);
                try
                {
                    System.Net.Sockets.Socket sock = e.AcceptSocket;
                    state.Handle = _acceptCount.CountAdd();
                    state.Socket = sock;
                    var option = new TcpKeepAlive
                    {
                        OnOff             = 1,
                        KeepAliveTime     = 5000,
                        KeepAliveInterval = 1000
                    };
                    state.Socket.IOControl(IOControlCode.KeepAliveValues, option.GetBytes(), null);
                    AddPeer(io);
                    OnAccepted(state);
                    BeginReceive(io);
                }
                catch (System.Exception ex)
                {
                    Trace.WriteLine("Accept Exception : " + ex.Message);
                    ClosePeer(state);
                }
                finally
                {
                    _allDone.Set();
                }
            }
        }