Alenhj Alenhj - 6 months ago 45 Question

Telnet TcpClient Attempt

First of all, Thanks to everyone you has helped me try to figure out Tcpclients. (Though there is still a lot I don't understand)

I can across a thread in the MSDN.Microsoft Forms about an Tcpclient GUI freezing up. Have little to no success on my front of trying to Telnet to an network switch and knowing what the problem with the code was I gave it a try.
I about ran circles around my work area when I received an response back from the switch is was only "

" but is was the same amount of characters that I sent out.

So right now my questions are:
Did it actually do through?
Did it submit the command?
What is the best was to send more commands?

The part that freezes is when it tries to read the response from the switch but since I don't actually need any response back I can do away with that part of the code.

Any feed back, suggestion, examples, or online reading material(I really hate the telnet documentation by the way.) would be greatly appreciated.

Code from form:

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()
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
If Not networkStream.CanRead Then
Console.WriteLine("cannot not write data to this stream")
If Not networkStream.CanWrite Then
Console.WriteLine("cannot read data from this stream")
End If
End If
End If
End Sub

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

I know I have to rework this code but right now it is more of getting a success.

Thank you.


IT WORKS!!! Sorry...Redbull

I found out my problem was a timing issue as when as not having my textbox large enough to see the received data from the switch.

Here is a Sample of what worked for me.

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


        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


    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

This is mostly for individuals like myself that are learning something new. Also, this is the only code I used. I did not make a Server Application since the Switch already had an Telnet Server on it.