Files

56 lines
1.5 KiB
C#
Raw Permalink Normal View History

2023-05-12 17:55:51 +09:00
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;
2023-10-05 17:56:04 +09:00
_client.ErrorCallback += On_ErrorCallback;
2023-05-12 17:55:51 +09:00
_client.Connect(ip, port);
while (true)
{
2023-10-05 17:56:04 +09:00
string command = Console.ReadLine().ToLower();
2023-05-12 17:55:51 +09:00
if (command == "quit" || command == "exit")
break;
2023-10-05 17:56:04 +09:00
else if (command == "disconnect" || command == "disconn")
_client.Close();
else if (command == "connect" || command == "conn")
_client.Connect(ip, port);
else
_client.SendCommand(command);
2023-05-12 17:55:51 +09:00
}
}
2023-10-05 17:56:04 +09:00
catch (Exception ex)
2023-05-12 17:55:51 +09:00
{
2023-10-05 17:56:04 +09:00
Console.WriteLine($"[ERR] {ex.Message}");
2023-05-12 17:55:51 +09:00
}
}
2023-10-05 17:56:04 +09:00
private void On_ErrorCallback(object? sender, Exception e)
{
Console.WriteLine(e.Message);
}
2023-05-12 17:55:51 +09:00
private void On_Receive(object? sender, string e)
{
Console.Write(e);
}
}
}