telnet期权谈判问题

时间:2016-03-16 13:08:07

标签: vb.net sockets telnet

我正在尝试编写一个telnet客户端,我们可以使用它来自动执行我们通常发送给服务器的一堆命令。当我使用它连接到我们的服务器时,我首先获得Telnet选项协商命令,然后是欢迎消息,然后是用户名:提示符。我发送了正确的DONT命令,但服务器只是回应它们并且没有响应任何东西。以下是我的代码。希望有人能够理解这一点以及我做错了什么。如果还有其他任何我可以详细说明,请告诉我。

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

1 个答案:

答案 0 :(得分:0)

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

不是我必须弄清楚为什么我会得到一堆垃圾与我想要的文字......