Ejemplo n.º 1
0
        /// <summary>
        /// Main worker method.
        /// </summary>
        private void Run()
        {
            _server = new TcpListener(IPAddress.Any, IDENTPORT);
            _server.Start();

            try
            {
                while (IsRunning)
                {
                    if (_server.Pending())
                    {
                        _server.BeginAcceptTcpClient(new AsyncCallback(OnAcceptClient), null);
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
            catch (Exception ex)
            {
                IdentError?.Invoke(this, new IdentErrorEventArgs(ex));
            }
            finally
            {
                try
                {
                    _server.Stop();
                }
                catch (Exception) { }

                IsRunning = false;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Processes an incoming client connection.
        /// </summary>
        /// <param name="result">A variable containing state information when the client connected.</param>
        private void OnAcceptClient(IAsyncResult result)
        {
            try
            {
                using (TcpClient client = _server.EndAcceptTcpClient(result))
                {
                    client.ReceiveTimeout = Timeout;
                    client.SendTimeout    = Timeout;

                    using (StreamReader input = new StreamReader(client.GetStream()))
                    {
                        using (StreamWriter output = new StreamWriter(client.GetStream())
                        {
                            NewLine = LINETERMINATOR
                        })
                        {
                            string request = input.ReadLine();
                            if (!string.IsNullOrEmpty(request))
                            {
                                request = request.Trim();

                                //not actually on Unix, but who cares
                                string response = string.Format("{0} : USERID : UNIX : {1}", request, _userName);

                                output.WriteLine(response);
                                output.Flush();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                IdentError?.Invoke(this, new IdentErrorEventArgs(ex));
            }
        }