Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用.net的Telnet连接_C#_Sockets_Telnet - Fatal编程技术网

C# 使用.net的Telnet连接

C# 使用.net的Telnet连接,c#,sockets,telnet,C#,Sockets,Telnet,我们办公室目前使用telnet查询外部服务器。程序是这样的 连接-telnet opent 128。。。。。。。。25000 查询-粘贴查询,然后点击alt+019 响应-我们在telnet窗口中以文本形式接收响应 因此,我尝试使用c#应用程序自动执行此查询。我的代码如下 首先是连接。(无例外) 然后我发送查询(无例外) 然后我试着接受回应 byte[] buffer = new byte[10]; Receive(SocketClient, buffer, 0, buffer.

我们办公室目前使用telnet查询外部服务器。程序是这样的

  • 连接-telnet opent 128。。。。。。。。25000
  • 查询-粘贴查询,然后点击alt+019
  • 响应-我们在telnet窗口中以文本形式接收响应
  • 因此,我尝试使用c#应用程序自动执行此查询。我的代码如下

    首先是连接。(无例外)

    然后我发送查询(无例外)

    然后我试着接受回应

        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);
    }
    
    byte[]buffer=新字节[10];
    接收(SocketClient,buffer,0,buffer.Length,10000);
    string str=Encoding.ASCII.GetString(buffer,0,buffer.Length);
    Text=str;
    公共静态无效接收(套接字套接字,字节[]缓冲区,整数偏移量,整数大小,整数超时)
    {
    int startTickCount=Environment.TickCount;
    int received=0;//已接收多少字节
    做
    {
    if(Environment.TickCount>startTickCount+超时)
    抛出新异常(“超时”);
    尝试
    {
    received+=socket.Receive(缓冲区、偏移量+接收、大小-接收、SocketFlags.None);
    }
    捕获(SocketException例外)
    {
    if(ex.SocketErrorCode==SocketError.WouldBlock||
    ex.SocketErrorCode==SocketError.IOPending||
    ex.SocketErrorCode==SocketError.NoBufferSpaceAvailable)
    {
    //套接字缓冲区可能为空,请稍候,然后重试
    睡眠(30);
    }
    其他的
    throw ex;//发生任何严重错误
    }
    }while(接收<大小);
    }
    
    每次尝试接收响应时,我都会收到“远程主机已强制关闭现有连接”,如果打开telnet并发送相同的查询,我会立即收到响应


    有什么想法或建议吗?

    一般来说,这类问题可以通过使用网络分析工具(嗅探器)轻松解决,例如


    更具体地说,telnet协议包括会话开始时的协商步骤。我猜如果你忽视了这一点,主人会不高兴。在成功的telnet连接上使用Wireshark将显示您缺少的内容。

    根据您和我之间的评论交换,您似乎需要附加Ascii代码19(0x13)查询结束。

    这里是一个telnet库的示例,以及一个使用它登录到Cisco路由器并下载IOS配置的程序


    您是否在自动查询中发送Alt+019?与您的问题无关,但您应该使用“throw”而不是“throw ex”来重新显示异常“throw”将保持stacktrace的完整性,这不是你的问题,只是一些有用的信息。我不知道如何发送Alt+019,如果你在记事本中点击它,它不会打印任何内容,它只是在telnet中工作,我一直试图在谷歌中找到Alt+019的一些参考资料,但没有运气。@alejandrobog,添加字节19作为缓冲区中的最后一个字节。Ascii 19是一个控制字符,因此它可能对端点服务器具有重要意义。谢谢你的声音,我将在几分钟后再试。谢谢你的帮助。@Chris Taylor,我加上字节19,它就工作了!请把它作为一个答案,这样我可以给你信用。谢谢
        string data ="some query";
        byte[] byData = System.Text.Encoding.ASCII.GetBytes(data);
        SocketClient.Send(byData);
    
        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);
    }