diff --git a/MySolution/ConsoleApp/Program.cs b/MySolution/ConsoleApp/Program.cs index 08ceb3e..280ecce 100644 --- a/MySolution/ConsoleApp/Program.cs +++ b/MySolution/ConsoleApp/Program.cs @@ -11,43 +11,53 @@ class Program { static void Main(string[] args) { - #region Delegate + #region Delegate - // CalculateSample.Sample(); - // MessagePrinterSample.Sample(); + // CalculateSample.Sample(); + // MessagePrinterSample.Sample(); - #endregion + #endregion - #region Callback + #region Callback - // StringLengthSample.Sample(); + // StringLengthSample.Sample(); - #endregion + #endregion - #region Event + #region Event - // EventSample.Sample(); + // EventSample.Sample(); - #endregion + #endregion - #region Dependency Injection + #region Dependency Injection - // DISample.Sample(); + // DISample.Sample(); - #endregion + #endregion - #region Telnet + #region Telnet - //TelnetConsole console = new TelnetConsole(new StreamTelnetClient()); - TelnetConsole console = new TelnetConsole(new SocketTelnetClient()); + //TelnetConsole console = new TelnetConsole(new StreamTelnetClient()); + //TelnetConsole console = new TelnetConsole(new SocketTelnetClient()); + + //console.Start("200.200.200.123"); + + #endregion + + + #region Async Telnet + + // AsyncTelnetConsole console = new AsyncTelnetConsole(new AsyncStreamTelnetClient()); + AsyncTelnetConsole console = new AsyncTelnetConsole(new AsyncSocketTelnetClient()); console.Start("200.200.200.123"); - #endregion + #endregion - } + } } diff --git a/MySolution/ConsoleApp/TelnetSamples/AsyncSocketTelnetClient.cs b/MySolution/ConsoleApp/TelnetSamples/AsyncSocketTelnetClient.cs new file mode 100644 index 0000000..27dbb57 --- /dev/null +++ b/MySolution/ConsoleApp/TelnetSamples/AsyncSocketTelnetClient.cs @@ -0,0 +1,109 @@ +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 + { + private readonly CancellationTokenSource CTS = new CancellationTokenSource(); + + public event EventHandler MessageCallback; + + private Socket _socket; + + public async void Connect(string ip, int port = 23) + { + try + { + Close(); + + _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); + + Task.Run(ReadAsync); + } + catch (Exception) + { + throw; + } + } + + 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) + break; + + string data = Encoding.ASCII.GetString(readBuffer, 0, bytesRead); + 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(); + } + } + catch (Exception) + { + throw; + } + } + + public async void SendCommand(string command) + { + try + { + command += "\r\n"; + byte[] sendBytes = Encoding.ASCII.GetBytes(command); + await _socket.SendAsync(sendBytes); + } + catch (Exception) + { + throw; + } + } + + public void Close() + { + try + { + if (_socket == null) + return; + + _socket.Shutdown(SocketShutdown.Both); + _socket.Close(); + _socket.Dispose(); + } + catch (Exception) + { + throw; + } + } + } +} diff --git a/MySolution/ConsoleApp/TelnetSamples/AsyncStreamTelnetClient.cs b/MySolution/ConsoleApp/TelnetSamples/AsyncStreamTelnetClient.cs new file mode 100644 index 0000000..cbb0b78 --- /dev/null +++ b/MySolution/ConsoleApp/TelnetSamples/AsyncStreamTelnetClient.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.TelnetSamples +{ + internal class AsyncStreamTelnetClient : IAsyncTelnetClient + { + public event EventHandler MessageCallback; + + private TcpClient _client; + private NetworkStream _stream; + + public async void Connect(string ip, int port = 23) + { + try + { + Close(); + + _client = new TcpClient(); + await _client.ConnectAsync(ip, port); + _stream = _client.GetStream(); + + Task.Run(ReadAsync); + } + catch (Exception) + { + throw; + } + } + + private async void ReadAsync() + { + try + { + StringBuilder sb = new StringBuilder(); + byte[] readBuffer = new byte[1024]; + while (true) + { + int bytesRead = await this._stream.ReadAsync(readBuffer); + if (bytesRead < 1) + break; + + string data = Encoding.ASCII.GetString(readBuffer, 0, bytesRead); + 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(); + } + } + catch (Exception) + { + throw; + } + } + + public async void SendCommand(string command) + { + try + { + command += "\r\n"; + byte[] sendBytes = Encoding.ASCII.GetBytes(command); + await _stream.WriteAsync(sendBytes); + } + catch (Exception) + { + throw; + } + } + + public void Close() + { + try + { + if (_client == null) + return; + + if (_stream != null) + { + _stream.Flush(); + _stream.Close(); + } + + + + _client.Close(); + _client.Dispose(); + } + catch (Exception) + { + throw; + } + } + } +} diff --git a/MySolution/ConsoleApp/TelnetSamples/AsyncTelnetConsole.cs b/MySolution/ConsoleApp/TelnetSamples/AsyncTelnetConsole.cs new file mode 100644 index 0000000..f677cd2 --- /dev/null +++ b/MySolution/ConsoleApp/TelnetSamples/AsyncTelnetConsole.cs @@ -0,0 +1,45 @@ +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.Connect(ip, port); + + while (true) + { + string command = Console.ReadLine(); + if (command == "quit" || command == "exit") + break; + + _client.SendCommand(command); + } + } + catch (Exception) + { + throw; + } + } + + private void On_Receive(object? sender, string e) + { + Console.Write(e); + } + } +} diff --git a/MySolution/ConsoleApp/TelnetSamples/IAsyncTelnetClient.cs b/MySolution/ConsoleApp/TelnetSamples/IAsyncTelnetClient.cs new file mode 100644 index 0000000..e40ee0f --- /dev/null +++ b/MySolution/ConsoleApp/TelnetSamples/IAsyncTelnetClient.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.TelnetSamples +{ + public interface IAsyncTelnetClient + { + public event EventHandler MessageCallback; + + void Connect(string ip, int port = 23); + void SendCommand(string command); + void Close(); + } +} diff --git a/MySolution/ConsoleApp/TelnetSamples/ITelnetClinet.cs b/MySolution/ConsoleApp/TelnetSamples/ITelnetClinet.cs index 7a06909..259462f 100644 --- a/MySolution/ConsoleApp/TelnetSamples/ITelnetClinet.cs +++ b/MySolution/ConsoleApp/TelnetSamples/ITelnetClinet.cs @@ -9,7 +9,6 @@ namespace ConsoleApp.TelnetSamples public interface ITelnetClient { string Connect(string ip, int port = 23); - string Read(); string SendCommand(string command); void Close(); } diff --git a/MySolution/ConsoleApp/TelnetSamples/SocketTelnetClient.cs b/MySolution/ConsoleApp/TelnetSamples/SocketTelnetClient.cs index 21fe5cf..c563e0c 100644 --- a/MySolution/ConsoleApp/TelnetSamples/SocketTelnetClient.cs +++ b/MySolution/ConsoleApp/TelnetSamples/SocketTelnetClient.cs @@ -21,7 +21,7 @@ namespace ConsoleApp.TelnetSamples _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.ReceiveTimeout = 1000; - IPAddress ipAddress = IPAddress.Parse(ip); + IPAddress ipAddress = Dns.GetHostAddresses(ip)[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, port); _socket.Connect(ipEndPoint); @@ -33,7 +33,7 @@ namespace ConsoleApp.TelnetSamples } } - public string Read() + private string Read() { StringBuilder sb = new StringBuilder(); byte[] readBuffer = new byte[1024]; @@ -48,8 +48,6 @@ namespace ConsoleApp.TelnetSamples if (data.EndsWith("\r\n>")) break; - - Thread.Sleep(50); } // 에코 삭제 diff --git a/MySolution/ConsoleApp/TelnetSamples/StreamTelnetClient.cs b/MySolution/ConsoleApp/TelnetSamples/StreamTelnetClient.cs index 12a9d92..bc34462 100644 --- a/MySolution/ConsoleApp/TelnetSamples/StreamTelnetClient.cs +++ b/MySolution/ConsoleApp/TelnetSamples/StreamTelnetClient.cs @@ -35,7 +35,7 @@ namespace Samples.Telnet } } - public string Read() + private string Read() { try {