Beispiel #1
0
        /// <summary>
        /// 关闭客户端Socket
        /// </summary>
        /// <param name="args">异步套接字事件 对象</param>
        private void CloseClientSocket(SocketAsyncEventArgs args)
        {
            AsyncUserToken token = args.UserToken as AsyncUserToken; // 获取关联用户信息

            try
            {
                lock (_Clients) _Clients.Remove(token);  // 锁定线程并移除用户对象
                ClientNumberChange?.Invoke(-1, token);   // 根据对象清理掉那个用户
                try
                {
                    token.Socket.Shutdown(SocketShutdown.Send); // 关闭套接字连接
                }
                catch (Exception ex)
                {
                    log.AddLog(LogType.Error, $"在关闭客户端Socket时出错!\r\n客户端地址:{token.IPAddress}\r\n错误信息:{ex.Message}");
                }
                token.Socket.Close();
                Interlocked.Decrement(ref _ClientCount); // 递减计数器跟踪连接到服务器的客户端总数
                _MaxNumberAcceptedClients.Release();

                args.UserToken = new AsyncUserToken(); // 释放异步套接字事件,以便重用
                _Pool.Push(args);
            }
            catch (Exception ex)
            {
                log.AddLog(LogType.Error, $"在关闭客户端Socket2时出错!\r\n客户端地址:{token.IPAddress}\r\n错误信息:{ex.Message}");
            }
        }
Beispiel #2
0
        /// <summary>
        /// 停止并清空服务器
        /// </summary>
        internal void Stop()
        {
            foreach (AsyncUserToken token in _Clients)
            {
                try
                {
                    token.Socket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception ex)
                {
                    log.AddLog(LogType.Error, $"在断开与客户端的连接时出错!\r\n客户端地址:{token.IPAddress}\r\n错误信息:{ex.Message}");
                }
            }
            try
            {
                _ListenSocket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception ex)
            {
                log.AddLog(LogType.Error, $"在关闭服务器监听时出错!\r\n错误信息:{ex.Message}");
            }
            _ListenSocket.Close();
            Int32 Count = _Clients.Count;

            lock (_Clients) _Clients.Clear();
            ClientNumberChange?.Invoke(-Count, null);
        }
Beispiel #3
0
        /// <summary>
        /// 线程响应
        /// </summary>
        /// <param name="args"></param>
        private void ProcessAccept(SocketAsyncEventArgs args)
        {
            try
            {
                Interlocked.Increment(ref _ClientCount);
                SocketAsyncEventArgs readEventArgs = _Pool.Pop();
                AsyncUserToken       userToken     = (AsyncUserToken)readEventArgs.UserToken;
                userToken.Socket      = args.AcceptSocket;
                userToken.ConnectTime = DateTime.Now;
                userToken.Remote      = args.AcceptSocket.RemoteEndPoint;
                userToken.IPAddress   = ((IPEndPoint)args.AcceptSocket.RemoteEndPoint).Address;

                lock (_Clients) _Clients.Add(userToken);  // 锁定线程添加对象

                ClientNumberChange?.Invoke(1, userToken); // 事件处理
                if (!args.AcceptSocket.ReceiveAsync(readEventArgs))
                {
                    ProcessReceive(readEventArgs);
                }
            }
            catch (Exception ex)
            {
                AsyncUserToken token = args.UserToken as AsyncUserToken;
                log.AddLog(LogType.Error, $"在线程响应时出错!\r\n客户端地址:{token.IPAddress}\r\n错误信息:{ex.Message}");
            }
            if (args.SocketError == SocketError.OperationAborted)
            {
                return;
            }
            StartAccept(args);
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        public void Stop()
        {
            foreach (AsyncUserToken token in clients)
            {
                try
                {
                    token.Socket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception) { }
            }
            try
            {
                _listenSocket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception) { }

            _listenSocket.Close();
            int count = clients.Count;

            lock (clients) { clients.Clear(); }

            ClientNumberChange?.Invoke(-count, null);
        }