Telnet connection using .net

柔情痞子 提交于 2019-12-06 06:33:43

问题


Our office currently uses telnet to query an external server. The procedure is something like this.

  1. Connect - telnet opent 128........ 25000
  2. Query - we paste the query and then hit alt + 019
  3. Response - We receive the response as text in the telnet window

So I’m trying to make this queries automatic using a c# 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

Any ideas, or suggestions?


回答1:


Based on the comment exchange between you and I, it would seem that you need to append Ascii code 19 (0x13) to the end of your query.




回答2:


Generally speaking, problems of this sort are easily resolved by using a network analysis tool (sniffer) such as Wireshark.

More specifically, the telnet protocol includes a negotiation step at the the start of the session. 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.




回答3:


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.

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



来源:https://stackoverflow.com/questions/2906475/telnet-connection-using-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!