Files
dotNetStudyWithGPT/MySolution/ConsoleApp/TelnetSamples/AsyncSocketTelnetClient.cs

130 lines
2.9 KiB
C#
Raw Permalink Normal View History

2023-05-12 17:55:51 +09:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp.TelnetSamples
{
internal class AsyncSocketTelnetClient : IAsyncTelnetClient
{
2023-10-05 17:56:04 +09:00
private CancellationTokenSource _cts;
2023-05-12 17:55:51 +09:00
public event EventHandler<string> MessageCallback;
2023-10-05 17:56:04 +09:00
public event EventHandler<Exception> ErrorCallback;
2023-05-12 17:55:51 +09:00
2023-10-05 17:56:04 +09:00
private Socket _socket;
2023-05-12 17:55:51 +09:00
public async void Connect(string ip, int port = 23)
{
try
{
2023-10-05 17:56:04 +09:00
//Close();
2023-05-12 17:55:51 +09:00
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.ReceiveTimeout = 1000;
IPAddress ipAddress = Dns.GetHostAddresses(ip)[0];
IPEndPoint iPEndPoint = new IPEndPoint(ipAddress, port);
await _socket.ConnectAsync(iPEndPoint);
2023-10-05 17:56:04 +09:00
_cts = new CancellationTokenSource();
Task.Run(ReadAsync, _cts.Token);
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
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
2023-05-12 17:55:51 +09:00
}
private async void ReadAsync()
{
try
{
StringBuilder sb = new StringBuilder();
byte[] readBuffer = new byte[1024];
while (true)
{
2023-10-06 14:17:20 +09:00
//Thread.Sleep(100);
if (_cts.IsCancellationRequested)
2023-05-12 17:55:51 +09:00
break;
2023-10-06 14:17:20 +09:00
int bytesRead = await _socket.ReceiveAsync(readBuffer);
2023-10-05 17:56:04 +09:00
if (bytesRead < 1)
{
if (sb.Length > 0 && this.MessageCallback != null)
this.MessageCallback(this, sb.ToString());
break;
}
string data = Encoding.ASCII.GetString(readBuffer, 0, bytesRead);
2023-05-12 17:55:51 +09:00
sb.Append(data);
if (!data.EndsWith("\r\n>"))
continue;
// 에코 삭제
string read = sb.ToString();
int rnIdx = read.IndexOf("\r\n");
read = read.Substring(rnIdx + 1);
if (this.MessageCallback != null)
this.MessageCallback(this, read);
sb.Clear();
}
}
2023-10-05 17:56:04 +09:00
catch (Exception ex)
{
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
2023-05-12 17:55:51 +09:00
public async void SendCommand(string command)
{
try
{
2023-10-05 17:56:04 +09:00
command = command.Replace(" ", "");
if (string.IsNullOrEmpty(command))
return;
2023-05-12 17:55:51 +09:00
command += "\r\n";
byte[] sendBytes = Encoding.ASCII.GetBytes(command);
await _socket.SendAsync(sendBytes);
}
2023-10-05 17:56:04 +09:00
catch (Exception ex)
{
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
2023-05-12 17:55:51 +09:00
public void Close()
{
try
{
if (_socket == null)
return;
2023-10-05 17:56:04 +09:00
_cts.Cancel();
2023-05-12 17:55:51 +09:00
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
_socket.Dispose();
}
2023-10-05 17:56:04 +09:00
catch (Exception ex)
{
if (this.ErrorCallback != null)
this.ErrorCallback(this, ex);
}
}
2023-05-12 17:55:51 +09:00
}
}