简体   繁体   English

使用.net进行Telnet连接

[英]Telnet connection using .net

Our office currently uses telnet to query an external server. 我们的办公室目前使用telnet查询外部服务器。 The procedure is something like this. 程序是这样的。

  1. Connect - telnet opent 128........ 25000 连接 - telnet opent 128 ........ 25000
  2. Query - we paste the query and then hit alt + 019 查询 - 我们粘贴查询然后点击alt + 019
  3. Response - We receive the response as text in the telnet window 响应 - 我们在telnet窗口中以文本形式接收响应

So I'm trying to make this queries automatic using ac# app. 所以我正在尝试使用ac#app自动进行此查询。 My code is the following 我的代码如下

First the connection. 首先是连接。 (No exceptions) (没有例外)

    SocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    String szIPSelected = txtIPAddress.Text;
    String szPort = txtPort.Text;
    int alPort = System.Convert.ToInt16(szPort, 10);

    System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
    System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
    SocketClient.Connect(remoteEndPoint);

Then I send the query (No exceptions) 然后我发送查询(没有例外)

    string data ="some query";
    byte[] byData = System.Text.Encoding.ASCII.GetBytes(data);
    SocketClient.Send(byData);

Then I try to receive the response 然后我尝试收到回复

    byte[] buffer = new byte[10];
    Receive(SocketClient, buffer, 0, buffer.Length, 10000);
    string str = Encoding.ASCII.GetString(buffer, 0, buffer.Length);
    txtDataRx.Text = str;

public static void Receive(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
  int startTickCount = Environment.TickCount;
  int received = 0;  // how many bytes is already received
  do
  {
    if (Environment.TickCount > startTickCount + timeout)
      throw new Exception("Timeout.");
    try
    {
      received += socket.Receive(buffer, offset + received, size - received, SocketFlags.None);
    }
    catch (SocketException ex)
    {
      if (ex.SocketErrorCode == SocketError.WouldBlock ||
          ex.SocketErrorCode == SocketError.IOPending ||
          ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
      {
        // socket buffer is probably empty, wait and try again
        Thread.Sleep(30);
      }
      else
        throw ex;  // any serious error occurr
    }
  } while (received < size);
}

Every time I try to receive the response I get "an exsiting connetion has forcibly closed by the remote host" if open telnet and send the same query I get a response right away 每当我尝试接收响应时,我得到“一个现有的连接被远程主机强行关闭”,如果打开telnet并发送相同的查询,我立即收到回复

Any ideas, or suggestions? 有什么想法或建议吗?

基于您和我之间的评论交换,您似乎需要将Ascii代码19(0x13)附加到查询的末尾。

Generally speaking, problems of this sort are easily resolved by using a network analysis tool (sniffer) such as Wireshark . 一般来说,使用Wireshark等网络分析工具(嗅探器)可以轻松解决此类问题。

More specifically, the telnet protocol includes a negotiation step at the the start of the session. 更具体地,telnet协议包括在会话开始时的协商步骤。 I'm guessing that if you ignore this, the host is unhappy. 我猜你如果忽略这一点,主人就不高兴了。 Using Wireshark on the successful telnet connection will show you what you're missing. 在成功的telnet连接上使用Wireshark将向您显示您缺少的内容。

Here is an example of a telnet library along with a program that uses it to log into a Cisco router and download the IOS configuration. 下面是一个telnet库的示例,以及一个使用它登录Cisco路由器并下载IOS配置的程序。

http://www.xpresslearn.com/networking/code/csharp-telnet-client http://www.xpresslearn.com/networking/code/csharp-telnet-client

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM