Telnet TcpClient 尝试

Telnet TcpClient Attempt

首先,感谢大家帮助我弄清楚 Tcpclients。 (虽然还有很多不懂)

我可以在 MSDN.Microsoft 表单中跨过一个关于 Tcpclient GUI 冻结的线程。在我尝试 Telnet 到网络交换机并知道代码的问题是什么时,我几乎没有成功,我试了一下。 当我从交换机收到回复时,我在工作区周围转了 运行 圈,只是“******”,但与我发出的字符数量相同。

所以现在我的问题是: 真的做到了吗? 它提交命令了吗? 什么是最好的是发送更多的命令?

冻结的部分是当它试图从开关读取响应时,但由于我实际上不需要任何返回响应,所以我可以删除那部分代码。

任何反馈、建议、示例或在线阅读material(顺便说一句,我真的很讨厌 telnet 文档。)将不胜感激。

来自表单的代码:

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

Public Class Form1
    Dim tagid As String
    Dim tcpClient As New System.Net.Sockets.TcpClient
    Dim networkStream As NetworkStream

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        tcpClient.Connect("IP address", 23)
        networkStream = tcpClient.GetStream()
        MessageBox.Show("connected")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If networkStream.CanWrite And networkStream.CanRead Then
            Dim sendBytes("String".Length) As Byte
            sendBytes = Encoding.ASCII.GetBytes("String")

            'I don't really need this part....I think.

            Dim x As Integer = 1

            Do While x = 1
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte

                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                Dim returndata As String = Encoding.ASCII.GetString(bytes)

                TextBox1.Text = returndata
                tagid = TextBox1.Text
                TextBox1.Refresh()
            Loop
        Else
            If Not networkStream.CanRead Then
                Console.WriteLine("cannot not write data to this stream")
                tcpClient.Close()
            Else
                If Not networkStream.CanWrite Then
                    Console.WriteLine("cannot read data from this stream")
                    tcpClient.Close()
                End If
            End If
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        networkStream.Close()
        tcpClient.Close()
    End Sub
End Class

我知道我必须重新编写这段代码,但现在更重要的是获得成功。

谢谢。

有效!!! 抱歉...红牛

我发现我的问题是时间问题,因为我的文本框不够大,无法看到从交换机接收到的数据。

这是对我有用的示例。

Imports System.Net.Sockets

Imports System.Text

Public Class Form1

Private oTCPStream As Net.Sockets.NetworkStream

Private oTCP As New Net.Sockets.TcpClient()

Private bytWriting As [Byte]()

Private bytReading As Byte()

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Try

        TextBox1.Text = ""

        oTCP.SendTimeout = 1500

        oTCP.Connect("IP Address", "23")

        oTCPStream = oTCP.GetStream

        TextBox1.Text = TextBox1.Text & ReadData() & vbCrLf 'Reads data from switch/server and displays it in a textbox

        WriteData("command" & vbCrLf) 'This line can repeated as many times as you like. You just need to adjust the time the application waits before starting the next line of code.

        System.Threading.Thread.Sleep(500) 'Pauses the application before starting the next line of code.

        TextBox1.Text = TextBox1.Text & ReadData() & vbCrLf
oTCPStream.Close() 'Closed the NetworkStream

        oTCP.Close()'Closed the TcpClient/Socket

        MsgBox("connection ok")

    Catch Err As Exception

        MsgBox(Err.ToString)

    End Try

End Sub

Private Function ReadData() As String

    Dim sData As String

    ReDim bytReading(oTCP.ReceiveBufferSize)

    oTCPStream.Read(bytReading, 0, oTCP.ReceiveBufferSize)

    sData = Trim(System.Text.Encoding.ASCII.GetString(bytReading))

    ReadData = sData

End Function

Private Sub WriteData(ByVal sData As String)

    bytWriting = System.Text.Encoding.ASCII.GetBytes(sData)

    oTCPStream.Write(bytWriting, 0, bytWriting.Length)

End Sub
End Class

这主要是为像我这样正在学习新东西的人准备的。 此外,这是我使用的唯一代码。我 没有 创建服务器应用程序,因为 Switch 上已经有一个 Telnet 服务器。