This commit is contained in:
2023-10-05 17:56:04 +09:00
parent bce6521e43
commit a3a0cc201b
7 changed files with 279 additions and 244 deletions

View File

@@ -12,17 +12,18 @@ namespace ConsoleApp.TelnetSamples
{
internal class AsyncSocketTelnetClient : IAsyncTelnetClient
{
private readonly CancellationTokenSource CTS = new CancellationTokenSource();
private CancellationTokenSource _cts;
public event EventHandler<string> MessageCallback;
public event EventHandler<Exception> ErrorCallback;
private Socket _socket;
private Socket _socket;
public async void Connect(string ip, int port = 23)
{
try
{
Close();
//Close();
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.ReceiveTimeout = 1000;
@@ -31,28 +32,37 @@ namespace ConsoleApp.TelnetSamples
IPEndPoint iPEndPoint = new IPEndPoint(ipAddress, port);
await _socket.ConnectAsync(iPEndPoint);
Task.Run(ReadAsync);
_cts = new CancellationTokenSource();
Task.Run(ReadAsync, _cts.Token);
}
catch (Exception)
catch (Exception ex)
{
throw;
}
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
private async void ReadAsync()
{
try
{
StringBuilder sb = new StringBuilder();
byte[] readBuffer = new byte[1024];
while (true)
{
int bytesRead = await _socket.ReceiveAsync(readBuffer);
if (bytesRead < 1)
if (_cts != null && _cts.IsCancellationRequested)
break;
string data = Encoding.ASCII.GetString(readBuffer, 0, bytesRead);
int bytesRead = _cts != null && _cts.IsCancellationRequested ? 0 : await _socket.ReceiveAsync(readBuffer);
if (bytesRead < 1)
{
if (sb.Length > 0 && this.MessageCallback != null)
this.MessageCallback(this, sb.ToString());
break;
}
string data = Encoding.ASCII.GetString(readBuffer, 0, bytesRead);
sb.Append(data);
if (!data.EndsWith("\r\n>"))
@@ -69,25 +79,31 @@ namespace ConsoleApp.TelnetSamples
sb.Clear();
}
}
catch (Exception)
{
throw;
}
}
catch (Exception ex)
{
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
public async void SendCommand(string command)
{
try
{
command = command.Replace(" ", "");
if (string.IsNullOrEmpty(command))
return;
command += "\r\n";
byte[] sendBytes = Encoding.ASCII.GetBytes(command);
await _socket.SendAsync(sendBytes);
}
catch (Exception)
{
throw;
}
}
catch (Exception ex)
{
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
public void Close()
{
@@ -96,14 +112,17 @@ namespace ConsoleApp.TelnetSamples
if (_socket == null)
return;
_cts.Cancel();
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
_socket.Dispose();
}
catch (Exception)
{
throw;
}
}
catch (Exception ex)
{
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
}
}

View File

@@ -7,11 +7,12 @@ using System.Threading.Tasks;
namespace ConsoleApp.TelnetSamples
{
internal class AsyncStreamTelnetClient : IAsyncTelnetClient
internal class AsyncStreamTelnetClient : IAsyncTelnetClient
{
public event EventHandler<string> MessageCallback;
public event EventHandler<Exception> ErrorCallback;
private TcpClient _client;
private TcpClient _client;
private NetworkStream _stream;
public async void Connect(string ip, int port = 23)

View File

@@ -20,23 +20,33 @@ namespace ConsoleApp.TelnetSamples
try
{
_client.MessageCallback += On_Receive;
_client.ErrorCallback += On_ErrorCallback;
_client.Connect(ip, port);
while (true)
{
string command = Console.ReadLine();
string command = Console.ReadLine().ToLower();
if (command == "quit" || command == "exit")
break;
_client.SendCommand(command);
else if (command == "disconnect" || command == "disconn")
_client.Close();
else if (command == "connect" || command == "conn")
_client.Connect(ip, port);
else
_client.SendCommand(command);
}
}
catch (Exception)
catch (Exception ex)
{
throw;
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);

View File

@@ -9,6 +9,7 @@ namespace ConsoleApp.TelnetSamples
public interface IAsyncTelnetClient
{
public event EventHandler<string> MessageCallback;
public event EventHandler<Exception> ErrorCallback;
void Connect(string ip, int port = 23);
void SendCommand(string command);

View File

@@ -24,11 +24,15 @@ namespace ConsoleApp.TelnetSamples
while (true)
{
string command = Console.ReadLine();
string command = Console.ReadLine().ToLower();
if (command == "quit" || command == "exit")
break;
Console.Write(_client.SendCommand(command));
else if (command == "disconnect" || command == "disconn")
_client.Close();
else if (command == "connect" || command == "conn")
_client.Connect(ip, port);
else
Console.Write(_client.SendCommand(command));
}
}
catch (Exception ex)