VB.NET 使用 system.net.tcpclient 编写 telnet 客户端

发布于 2024-09-14 15:54:09 字数 1352 浏览 2 评论 0原文

当我连接到我的Solaris盒子时,这对我不起作用

服务器正在发回

??%

有人知道我做错了什么吗

Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text

    Public Class TelnetClient

        Private _hostname As String = "myserver"
        Private _username As String = "user"
        Private _password As String = "pass"

        Private _port As Integer = 23
        Private _client As TcpClient
        Private _data As String

        Private _sendbuffer(128) As Byte
        Private _readbuffer(128) As Byte
        Private _bytecount As Integer

        Private _stream As NetworkStream

        Private Sub Send(ByVal Text As String)
            _sendbuffer = Encoding.ASCII.GetBytes(Text)
            _stream.Write(_sendbuffer, 0, _sendbuffer.Length)
        End Sub

        Private Sub Read()
            _bytecount = _stream.Read(_readbuffer, 0, _readbuffer.Length)
            _data = Encoding.ASCII.GetString(_readbuffer)
        End Sub

        Public Sub Connect()

            _client = New TcpClient(_hostname, _port)

            _stream = _client.GetStream

            Send(_username)
            Read()

            MsgBox(_data)

            Send(_password)
            Read()

            _stream.Close()

            _client.Close()





        End Sub

    End Class

This isn't working for me when I connect to my solaris box

The server is sending back

??%

does anyone know what i'm doing wrong

Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text

    Public Class TelnetClient

        Private _hostname As String = "myserver"
        Private _username As String = "user"
        Private _password As String = "pass"

        Private _port As Integer = 23
        Private _client As TcpClient
        Private _data As String

        Private _sendbuffer(128) As Byte
        Private _readbuffer(128) As Byte
        Private _bytecount As Integer

        Private _stream As NetworkStream

        Private Sub Send(ByVal Text As String)
            _sendbuffer = Encoding.ASCII.GetBytes(Text)
            _stream.Write(_sendbuffer, 0, _sendbuffer.Length)
        End Sub

        Private Sub Read()
            _bytecount = _stream.Read(_readbuffer, 0, _readbuffer.Length)
            _data = Encoding.ASCII.GetString(_readbuffer)
        End Sub

        Public Sub Connect()

            _client = New TcpClient(_hostname, _port)

            _stream = _client.GetStream

            Send(_username)
            Read()

            MsgBox(_data)

            Send(_password)
            Read()

            _stream.Close()

            _client.Close()





        End Sub

    End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老子叫无熙 2024-09-21 15:54:09

您从服务器获得的 ??% 是 Telnet 选项协商的一部分。您需要先进行选项协商,然后才能进行任何其他通信。

The ??% that you are getting from the server is part of the Telnet options negotiation. You need to do the options negotiation before any other communication can take place.

两个我 2024-09-21 15:54:09

上面代码中的 Read() 方法正在解码整个 _readbuffer,而 _stream.Read() 可能只填充部分缓冲区。 _bytecount 会告诉您可以解码多少字节。

我可以建议使用 StreamReader 吗? StreamReader.ReadLine() 方法将阻塞,直到收到换行符并返回一个字符串。

The Read() method in the code above is decoding the entire _readbuffer when _stream.Read() may only fill part of the buffer. _bytecount will tell you how many bytes you can decode.

Can I suggest using a StreamReader. The StreamReader.ReadLine() method will block until a newline is received and give you a string back.

与风相奔跑 2024-09-21 15:54:09

您之所以得到这些,是因为您试图在收集原始数据之前对其进行转换。您需要在 telnet 功能之间的通信之间添加大约 2 秒的睡眠时间。

Public Sub Connect()

        _client = New TcpClient(_hostname, _port)

        _stream = _client.GetStream

        Threading.Thread.Sleep(2000)

        Send(_username)
        Threading.Thread.Sleep(2000)
        Read()

        MsgBox(_data)

        Send(_password)
        Threading.Thread.Sleep(2000)
        Read()

        _stream.Close()

        _client.Close()

You're getting those because you're trying to translate raw data before its collected. You need to add in a about a 2 second sleep between communication between telnet functions.

Public Sub Connect()

        _client = New TcpClient(_hostname, _port)

        _stream = _client.GetStream

        Threading.Thread.Sleep(2000)

        Send(_username)
        Threading.Thread.Sleep(2000)
        Read()

        MsgBox(_data)

        Send(_password)
        Threading.Thread.Sleep(2000)
        Read()

        _stream.Close()

        _client.Close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文