2016-03-16 30 views
1

我正在尝试编写一个telnet客户端,我们可以使用它来自动化我们通常发送给服务器的一堆命令。当我使用它连接到我们的服务器时,首先获得Telnet选项协商命令,然后是欢迎消息,然后是用户名:提示。我发送了正确的DONT命令,但服务器只是响应它们,并且不响应任何事情。以下是我的代码。希望有人能够对此有所了解,以及我做错了什么。让我知道是否还有其他我可以详细说明的内容。telnet选项协商问题

Imports System.Text 
Imports System.Net.Sockets 
Module Module1 
    Dim Full_Stop As String = "" 
    Dim TelnetClient As New TcpClient 
    Sub Main() 
     TelnetClient.Connect("telnet.server.com", 23) 'Connecting to the IP Given 
     Dim thr As New Threading.Thread(AddressOf Receive_thread) 'define the thread that will handle data received 
     thr.Start() ' start the thread 
     Dim SendData As String = "" 
     While SendData <> "quit" 'loop until we quit the program 
      SendData = Console.ReadLine 
      If SendData <> "quit" Then 
       Send_Sub(SendData) 'send a command 
      End If 
     End While 
     Full_Stop = "Stop" 'kill the thread 
     TelnetClient.Close() 
    End Sub 
    Sub Send_Sub(ByVal msg As String) 
     Dim byt_to_send() As Byte = System.Text.Encoding.ASCII.GetBytes(msg) 
     TelnetClient.Client.Send(byt_to_send, 0, byt_to_send.Length, SocketFlags.None) 
    End Sub 
    Sub Receive_thread() 
re: 
     If Full_Stop = "Stop" Then Exit Sub 'If you set Full_Stop string to "Stop" the thread will end 
     If TelnetClient.Client.Available > 0 Then 'Check if there is any Data to receive 
      Dim byt_to_receive(TelnetClient.Available - 1) As Byte 
      TelnetClient.Client.Receive(byt_to_receive, 0, byt_to_receive.Length, SocketFlags.None) 'receive the data 
      For lc = 0 To byt_to_receive.Length - 2 'go through the entire byte array 
       If byt_to_receive(lc) = 255 Then 'looking for commands 
        Select Case byt_to_receive(lc + 1) 
         Case 251 'if a WILL command is received 
          Dim byt_to_send() As Byte = {255, 254, lc + 2} 'send a DONT command 
          TelnetClient.Client.Send(byt_to_send, 0, byt_to_send.Length, SocketFlags.None) 
         Case 253 'if a DO command is received 
          Dim byt_to_send() As Byte = {255, 252, lc + 2} 'send a WONT command 
          TelnetClient.Client.Send(byt_to_send, 0, byt_to_send.Length, SocketFlags.None) 
        End Select 
       End If 
      Next 
      Dim String_From_Byte As String = System.Text.Encoding.ASCII.GetString(byt_to_receive) 
      Console.Write(String_From_Byte) 
     End If 
     GoTo re 'this will NOT allow the thread to End by sending it back to re: statement, unless the Full_Stop is "Stop" 
    End Sub 
End Module 
+0

你不能只检查了*第一*对端发送出去,然后命令假设接收缓冲区中的所有其他内容也是命令。处理完一个命令后,你需要再次检查'IAC'。您还必须记住TCP是* streaming *协议,消息之间没有界限。该流式传输部分还意味着,当您从对等方接收到的数据时,您可能无法收到您要求的所有数据,因此您收到的数据可能包含* partial *命令。您可能需要在TCP连接上添加另一层缓冲区。 –

+0

虽然用于实际处理telnet命令的帽子,并发回正确的东西。但是,您应该不要忘记,对等方也可以*发送'DONT'和'WONT'作为*请求*以及许多其他命令和协商请求。 –

+0

好的,我在receive_thread()中更改了我的代码以查看每个进入的字节并验证是否有任何IAC。我仍然只看到两个命令是回声,并会抑制前进,我已经认为它不是回声,并且不要阻止前进。服务器将我的命令回应给我(具有讽刺意味的是),但是继续回应我键入的任何内容,并且不处理任何内容。无法弄清楚我做错了什么。所有的IAC都由255字节开头,对吧? – Kevin

回答

0

固定!我发送vbnewline而不是发送{10,13}作为字节。

不是我一定要弄清楚为什么我得到了一堆跟我想要的文字垃圾...

+0

为获得最佳的互操作性,您应该发送13后跟10(即CR LF),而不是相反方向(LF CR)。 – blubberdiblub