cmd.exe中的TELNET命令与c#中的Socket编程的区别

本文关键字:中的 Socket 编程 区别 exe TELNET 命令 cmd | 更新日期: 2023-09-27 17:59:59

我有一台机器,通过传递ip地址和端口号,使用cmd.exe中的TELNET命令连接到该机器。连接成功后,我会传递一些命令来完成任务。但每次打开命令提示符并执行上面提到的任务并不是客户想要的。他们希望流程自动化。凭借在c#中使用Socket程序的一些经验,我试图通过使用ip地址和端口号的Socket程序进行连接,并传递命令来完成任务,但套接字编程没有完成任务。

所以我的问题是,是否可以使用c#中的Socket编程在cmd.exe中执行telnet命令完成的相同任务,如果可以,如何执行?

请帮帮我。提前谢谢。

这是我在c#中的代码,用来连接到机器,但它无法完成所需的任务。它无法执行cmd.exe中TELNET命令正在执行的任务。.

            int intport;
            int.TryParse(port1, out intport);
            TcpClient client = new TcpClient();
            textfileSaveLocation = "some location";
            try
            {
                using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
                {
                    writer.WriteLine("Before Connection !");
                }
                client.Connect(new IPEndPoint(IPAddress.Parse(ip), intport));
                Thread.Sleep(5000);

                using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
                {
                    writer.WriteLine("Connected To Machine !");
                }
            }
            catch (Exception ex)
            {
                // Log the error here.
                client.Close();
                using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
                {
                    writer.WriteLine("Connection Error !");
                }
                continue;
            }
            try
            {
                using (NetworkStream stream = client.GetStream())
                {
                    byte[] username = Encoding.ASCII.GetBytes(user + "'r'n");
                    stream.Write(username, 0, username.Length);
                    Thread.Sleep(5000);
                    using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
                    {
                        writer.WriteLine("UserName Enetered !");
                    }
                    byte[] password = Encoding.ASCII.GetBytes(pass + "'r'n");
                    stream.Write(password, 0, password.Length);
                    Thread.Sleep(5000);
                    using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
                    {
                        writer.WriteLine("Password Passed !");
                    }
                    byte[] terminal = Encoding.ASCII.GetBytes(port2 + "'r'n");
                    stream.Write(terminal, 0, terminal.Length);
                    Thread.Sleep(5000);
                    using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
                    {
                        writer.WriteLine("Terminal Value Entered !");
                    }

                client.Close();

cmd.exe中的TELNET命令与c#中的Socket编程的区别

是的,这是可能的。

谷歌搜索关键词:C#Telnet客户端

第一个结果:http://www.codeproject.com/Articles/19071/Quick-tool-A-minimalistic-Telnet-library

可以使用TcpClient:编写一个简单的客户端

var client = new TcpClient("localhost", 25);
const string crlf = "'r'n";
var networkStream = client.GetStream();
var streamReader = new StreamReader(client.GetStream());
// Receive data from server
var serverResponse = streamReader.ReadLine();
Console.WriteLine("Server: " + serverResponse);
// Send data to server
const string data = "Command to server " + crlf;
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);
networkStream.Write(buffer, 0, buffer.Length);
networkStream.Close();
client.Close();

让我们复活这个死去的(旧的)问题。

相反,我正在寻找一种使用SuperSocket进行套接字编程的方法。我试过了,它可以从cmd进行telnet。