Beispiel #1
0
        private void StartListen()
        {
            try
            {
                _socket.BeginAccept(asyncResult =>
                {
                    try
                    {
                        Socket newSocket = _socket.EndAccept(asyncResult);
                        if (_isListen)
                        {
                            StartListen();
                        }

                        SocketConnection newClient = new SocketConnection(newSocket, this)
                        {
                            HandleRecMsg      = HandleRecMsg == null ? null : new Action <byte[], SocketConnection, SocketServer>(HandleRecMsg),
                            HandleClientClose = HandleClientClose == null ? null : new Action <SocketConnection, SocketServer>(HandleClientClose),
                            HandleSendMsg     = HandleSendMsg == null ? null : new Action <byte[], SocketConnection, SocketServer>(HandleSendMsg),
                            HandleException   = HandleException == null ? null : new Action <Exception>(HandleException)
                        };

                        newClient.StartRecMsg();
                        ClientList.AddLast(newClient);

                        HandleNewClientConnected?.Invoke(this, newClient);
                    }
                    catch (Exception ex)
                    {
                        HandleException?.Invoke(ex);
                    }
                }, null);
            }
            catch (Exception ex)
            {
                HandleException?.Invoke(ex);
            }
        }