Answer the question
In order to leave comments, you need to log in
How to connect to Telnet from C#?
I'm trying to connect to the Telnet server using the following code:
I must say right away that the code is not completed. I'm only concerned about the operation of the ReadTelnet method and TCPClient.
using System;
using System.Diagnostics;
using System.Xml.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
namespace Spark108.Asda
{
class ReloadApp
{
private static void Main(string[] args)
{
new ReloadApp(args);
}
public ReloadApp(string[] args)
{
Console.Title = "Перезагрузка ИП-1";
IPAddress host = IPAddress.Parse("127.0.0.1");
int port = 23;
string username = "admin";
string password = "admin";
string remoteHost = "127.0.0.1";
long timeout = 1000;
var xdoc = XDocument.Load("config.xml");
var rootElement = xdoc.Root;
foreach (var optionElement in rootElement.Elements("option"))
{
var nameAttr = optionElement.Attribute("name");
var valueAttr = optionElement.Attribute("value");
if (nameAttr == null || valueAttr == null)
continue;
switch (nameAttr.Value)
{
case "host":
if (!IPAddress.TryParse(valueAttr.Value, out host))
Console.WriteLine("Ошибка чтения параметра host.");
break;
case "username":
username = valueAttr.Value;
break;
case "password":
password = valueAttr.Value;
break;
case "remote_host":
remoteHost = valueAttr.Value;
break;
case "timeout":
if (!long.TryParse(valueAttr.Value, out timeout))
Console.WriteLine("Ошибка чтения параметра timeout.");
break;
case "port":
if (!int.TryParse(valueAttr.Value, out port))
Console.WriteLine("Ошибка чтения параметра port.");
break;
default: continue;
}
}
var endPoint = new IPEndPoint(host, port);
var client = new TcpClient();
client.Connect(endPoint);
var response = ReadTelnet(client, timeout, "login:");
Console.WriteLine(response);
Console.ReadLine();
}
private string ReadTelnet(TcpClient client, long timeout = 1000, string pattern = null)
{
for (long i = 0; i < timeout; i++)
{
NetworkStream stream = client.GetStream();
// if (client.ReceiveBufferSize <= 0 || !stream.CanRead || !stream.DataAvailable)
if (client.ReceiveBufferSize <= 0)
continue;
do
{
System.Threading.Thread.Sleep(10000);
byte[] buffer = new byte[client.ReceiveBufferSize];
int p = stream.Read(buffer, 0, client.ReceiveBufferSize);
string response = Encoding.ASCII.GetString(buffer).Trim();
Console.WriteLine(string.Format("{0}. {1}", i, response));
//for (var j = 0; j < buffer.Length; j++)
// Console.Write(buffer[j]);
if (response == "")
continue;
if (pattern != null)
{
Regex objToMatch = new Regex(pattern);
if (objToMatch.IsMatch(response))
{
return response;
}
}
return response;
}
while (stream.DataAvailable);
}
return null;
}
private string WriteTelnet(TcpClient client, string command, long timeout)
{
return ReadTelnet(client, timeout);
}
}
}
Answer the question
In order to leave comments, you need to log in
When I test on HK Server everything is fine, when I start testing on WinCE, I get the message "???" in response. and smiley. What's the catch?in response encoding
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question