From 59f0977eafac88a215b6f03e9ef9ad7da10b1e6c Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 11 May 2023 18:10:26 +0900 Subject: [PATCH] telnet samples --- MySolution/ConsoleApp/Program.cs | 26 +++-- .../ConsoleApp/TelnetSamples/ITelnetClinet.cs | 16 +++ .../TelnetSamples/SocketTelnetClient.cs | 95 ++++++++++++++++ .../TelnetSamples/StreamTelnetClient.cs | 105 ++++++++++++++++++ .../ConsoleApp/TelnetSamples/TelnetConsole.cs | 44 ++++++++ 5 files changed, 279 insertions(+), 7 deletions(-) create mode 100644 MySolution/ConsoleApp/TelnetSamples/ITelnetClinet.cs create mode 100644 MySolution/ConsoleApp/TelnetSamples/SocketTelnetClient.cs create mode 100644 MySolution/ConsoleApp/TelnetSamples/StreamTelnetClient.cs create mode 100644 MySolution/ConsoleApp/TelnetSamples/TelnetConsole.cs diff --git a/MySolution/ConsoleApp/Program.cs b/MySolution/ConsoleApp/Program.cs index 53dc347..08ceb3e 100644 --- a/MySolution/ConsoleApp/Program.cs +++ b/MySolution/ConsoleApp/Program.cs @@ -1,8 +1,10 @@ -using Samples.Calculate; +using ConsoleApp.TelnetSamples; +using Samples.Calculate; using Samples.DI; using Samples.Event; using Samples.MessagePrinter; using Samples.StringLength; +using Samples.Telnet; namespace ConsoleApp; class Program @@ -10,22 +12,22 @@ class Program static void Main(string[] args) { #region Delegate - + // CalculateSample.Sample(); // MessagePrinterSample.Sample(); - + #endregion #region Callback - + // StringLengthSample.Sample(); #endregion #region Event - + // EventSample.Sample(); #endregion @@ -33,8 +35,18 @@ class Program #region Dependency Injection - DISample.Sample(); - + // DISample.Sample(); + + #endregion + + + #region Telnet + + //TelnetConsole console = new TelnetConsole(new StreamTelnetClient()); + TelnetConsole console = new TelnetConsole(new SocketTelnetClient()); + + console.Start("200.200.200.123"); + #endregion } diff --git a/MySolution/ConsoleApp/TelnetSamples/ITelnetClinet.cs b/MySolution/ConsoleApp/TelnetSamples/ITelnetClinet.cs new file mode 100644 index 0000000..7a06909 --- /dev/null +++ b/MySolution/ConsoleApp/TelnetSamples/ITelnetClinet.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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 new file mode 100644 index 0000000..21fe5cf --- /dev/null +++ b/MySolution/ConsoleApp/TelnetSamples/SocketTelnetClient.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.TelnetSamples +{ + public class SocketTelnetClient : ITelnetClient + { + private Socket _socket; + + public string Connect(string ip, int port = 23) + { + try + { + Close(); + + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + _socket.ReceiveTimeout = 1000; + + IPAddress ipAddress = IPAddress.Parse(ip); + IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, port); + _socket.Connect(ipEndPoint); + + return Read(); + } + catch (Exception) + { + throw; + } + } + + public string Read() + { + StringBuilder sb = new StringBuilder(); + byte[] readBuffer = new byte[1024]; + while (true) + { + int bytesRead = _socket.Receive(readBuffer); + if (bytesRead < 1) + break; + + string data = Encoding.ASCII.GetString(readBuffer, 0, bytesRead); + sb.Append(data); + + if (data.EndsWith("\r\n>")) + break; + + Thread.Sleep(50); + } + + // 에코 삭제 + string read = sb.ToString(); + int rnIdx = read.IndexOf("\r\n"); + + return read.Substring(rnIdx + 1); + } + + public string SendCommand(string command) + { + try + { + command += "\r\n"; + byte[] sendBytes = Encoding.ASCII.GetBytes(command); + _socket.Send(sendBytes); + + return Read(); + } + 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/StreamTelnetClient.cs b/MySolution/ConsoleApp/TelnetSamples/StreamTelnetClient.cs new file mode 100644 index 0000000..12a9d92 --- /dev/null +++ b/MySolution/ConsoleApp/TelnetSamples/StreamTelnetClient.cs @@ -0,0 +1,105 @@ +using System; +using System.Net.Sockets; +using System.IO; +using System.Text; +using ConsoleApp.TelnetSamples; + +namespace Samples.Telnet +{ + public class StreamTelnetClient : ITelnetClient + { + private TcpClient _client; + private NetworkStream _stream; + private StreamReader _reader; + private StreamWriter _writer; + + public string Connect(string ip, int port = 23) + { + try + { + Close(); + + _client = new TcpClient(ip, port); + + // 스트림 생성 + _stream = _client.GetStream(); + _reader = new StreamReader(_stream); + _writer = new StreamWriter(_stream); + _writer.AutoFlush = true; + + return Read(); + } + catch (Exception) + { + throw; + } + } + + public string Read() + { + try + { + StringBuilder sb = new StringBuilder(); + byte[] readBuffer = new byte[1024]; + while (true) + { + int bytesRead = _reader.BaseStream.Read(readBuffer, 0, readBuffer.Length); + if (bytesRead < 1) + break; + + string data = Encoding.ASCII.GetString(readBuffer, 0, bytesRead); + sb.Append(data); + + if (data.EndsWith("\r\n>")) + break; + } + + // 에코 삭제 + string read = sb.ToString(); + int rnIdx = read.IndexOf("\r\n"); + + return read.Substring(rnIdx + 1); + } + catch (Exception) + { + throw; + } + } + + public string SendCommand(string command) + { + try + { + _writer.WriteLine(command); + + return Read(); + } + 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; + } + } + } +} \ No newline at end of file diff --git a/MySolution/ConsoleApp/TelnetSamples/TelnetConsole.cs b/MySolution/ConsoleApp/TelnetSamples/TelnetConsole.cs new file mode 100644 index 0000000..92658b8 --- /dev/null +++ b/MySolution/ConsoleApp/TelnetSamples/TelnetConsole.cs @@ -0,0 +1,44 @@ +using Samples.Telnet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.TelnetSamples +{ + internal class TelnetConsole + { + private static ITelnetClient _client; + + public TelnetConsole(ITelnetClient client) + { + _client = client; + } + + public void Start(string ip, int port = 23) + { + try + { + Console.Write(_client.Connect(ip, port)); + + while (true) + { + string command = Console.ReadLine(); + if (command == "quit" || command == "exit") + break; + + Console.Write(_client.SendCommand(command)); + } + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + finally + { + _client.Close(); + } + } + } +}