You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp.TelnetSamples
{
internal class AsyncTelnetConsole
{
private static IAsyncTelnetClient _client;
public AsyncTelnetConsole(IAsyncTelnetClient client)
{
_client = client;
}
public void Start(string ip, int port = 23)
{
try
{
_client.MessageCallback += On_Receive;
_client.ErrorCallback += On_ErrorCallback;
_client.Connect(ip, port);
while (true)
{
string command = Console.ReadLine().ToLower();
if (command == "quit" || command == "exit")
break;
else if (command == "disconnect" || command == "disconn")
_client.Close();
else if (command == "connect" || command == "conn")
_client.Connect(ip, port);
else
_client.SendCommand(command);
}
}
catch (Exception ex)
{
Console.WriteLine($"[ERR] {ex.Message}");
}
}
private void On_ErrorCallback(object? sender, Exception e)
{
Console.WriteLine(e.Message);
}
private void On_Receive(object? sender, string e)
{
Console.Write(e);
}
}
}