telnet samples

main
syneffort 2 years ago
parent b469188f28
commit 59f0977eaf
  1. 16
      MySolution/ConsoleApp/Program.cs
  2. 16
      MySolution/ConsoleApp/TelnetSamples/ITelnetClinet.cs
  3. 95
      MySolution/ConsoleApp/TelnetSamples/SocketTelnetClient.cs
  4. 105
      MySolution/ConsoleApp/TelnetSamples/StreamTelnetClient.cs
  5. 44
      MySolution/ConsoleApp/TelnetSamples/TelnetConsole.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
@ -33,7 +35,17 @@ 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

@ -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();
}
}

@ -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;
}
}
}
}

@ -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;
}
}
}
}

@ -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();
}
}
}
}
Loading…
Cancel
Save