Beispiel #1
0
        private static async Task StartTcpClient(string ip, int port)
        {
            if (_isConnected)
            {
                return;
            }

            _isConnected = true;
            using (TcpClient client = new TcpClient())
            {
                try
                {
                    client.ConnectAsync(ip, port).Wait(3000);
                }
                catch (Exception)
                {
                    _trace.TraceWarning($"Unable to connect, try again later.");
                    _isConnected = false;
                    return;
                }
                using (NetworkStream stream = client.GetStream())
                {
                    _trace.TraceInformation("Username:"******"Failed to send username to server, stopping.");
                        _isConnected = false;
                        return;
                    }

                    while (true)
                    {
                        // read the data to send to the server
                        _trace.TraceInformation("What to send?");
                        string line = Console.ReadLine();

                        // send the text to the server
                        if (!await _writer.WriteLineAsync(stream, line))
                        {
                            break;
                        }

                        // read the response of the server
                        string response = await _reader.ReadLineAsync(stream);

                        if (response == null)
                        {
                            break;
                        }

                        _trace.TraceVerbose($"Response from server {response}");
                    }
                }
            }
            _isConnected = false;
        }
Beispiel #2
0
        private async Task HandleConnection(TcpClient client)
        {
            _trace.TraceInformation($"New connection from {client.Client.RemoteEndPoint}");
            client.ReceiveTimeout = 30;
            client.SendTimeout    = 30;

            try
            {
                using (NetworkStream stream = client.GetStream())
                {
                    while (true)
                    {
                        // read the data from the client
                        string line = await _reader.ReadLineAsync(stream, false);

                        // if the data was not read during the retry process then close this client
                        if (line == null)
                        {
                            break;
                        }

                        bool commandFound = ProcessPossibleCommand(client, line);

                        string userName = _clientStore.GetUserName(client);
                        _trace.TraceInformation($"{userName}: {line}");

                        // echo back
                        if (!commandFound)
                        {
                            await _writer.WriteLineAsync(stream, line);
                        }
                    }
                }
            }
            finally
            {
                _trace.TraceVerbose("Client connection closed");
                client.Close();
                string userName = _clientStore.RemoveClient(client);
                _trace.TraceInformation($"{userName} disconnected");
            }
        }