-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppClient.cs
51 lines (39 loc) · 1.51 KB
/
AppClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Net.Sockets;
namespace TelnetClientServer;
public class AppClient
{
private const string LogPrefix = $"[ {nameof(AppClient)} ]";
private readonly string _hostname;
private readonly int _port;
private readonly TcpClient _tcpClient;
public AppClient(string hostname, int port)
{
_hostname = hostname;
_port = port;
_tcpClient = new TcpClient();
Console.WriteLine($"{LogPrefix} Created to {hostname}:{port}.");
}
public void Start()
{
Console.WriteLine($"{LogPrefix} Starting and connecting.");
try
{
_tcpClient.Connect(_hostname, _port);
new PipeStream(_tcpClient.GetStream(), Console.OpenStandardOutput(), nameof(_tcpClient), nameof(Console.OpenStandardOutput)).BeginRead();
new PipeStream(Console.OpenStandardInput(), _tcpClient.GetStream(), nameof(Console.OpenStandardInput), nameof(_tcpClient)).BeginRead();
var originalConsoleOut = Console.Out;
Console.SetOut(TextWriter.Null);
while (_tcpClient.Connected && (!Console.KeyAvailable || Console.ReadKey(true).Key != ConsoleKey.Escape))
{
Thread.Sleep(1);
}
Console.SetOut(originalConsoleOut);
}
catch (Exception exception)
{
Console.WriteLine($"{LogPrefix} Error when try to connect: {exception.Message}");
}
Console.WriteLine();
Console.WriteLine($"{LogPrefix} Terminated.");
}
}