# tcpclient connection

**URL:** <https://forums.thehomestack.io/t/tcpclient-connection/285>\
**Category:** vb.Net\
**Created:** [September 16, 2007, 7:55pm UTC](https://forums.thehomestack.io/t/tcpclient-connection/285 "2007-09-16T19:55:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forums.thehomestack.io/uploads/default/original/1X/161426793bc0d8bdf0d1a6692b3e44446861cc72.jpeg) [@system](https://forums.thehomestack.io/u/system)\
**Post date:** [September 16, 2007, 7:55pm UTC](https://forums.thehomestack.io/t/tcpclient-connection/285/1 "2007-09-16T19:55:37Z")

</div>

Here is a code snippet for a tcpclient connection:

```auto

Sub Main()
        Dim host As String = "192.168.1.50"
        Dim TCPClient As New TcpClient

        'Connect to telnet
        TCPClient.Connect(host, 23)

        'Get our networkstream
        Dim netstream As NetworkStream = TCPClient.GetStream()
        Dim buffer(1024) As Byte

        'Get the ASCII encoder to convert the text back and forth on the stream (bytes <--> strings)
        Dim encoding As New System.Text.ASCIIEncoding
        Dim hasLoggedUser, hasLoggedPass As Boolean

        Do While True
            'Begin reading data into buffer (this statement is blocking)
            netstream.Read(buffer, 0, buffer.Length)

            'Clean up for displaying
            Dim bufferedstring As String = encoding.GetString(buffer)
            bufferedstring = Replace(bufferedstring, vbCr, "")
            bufferedstring = Replace(bufferedstring, vbLf, "")
            bufferedstring = Replace(bufferedstring, Chr(0).ToString(), "")
            Console.WriteLine(" RECV: {0}", bufferedstring)

            If hasLoggedUser = False And InStr(encoding.GetString(buffer), "login:") Then
            'Send User
                Dim sendBuffer() As Byte = encoding.GetBytes("my user" & vbCr)
                netstream.Write(sendBuffer, 0, sendBuffer.Length)
                Console.WriteLine("SENT: {0}", encoding.GetString(sendBuffer))
                hasLoggedUser = True

            ElseIf hasLoggedPass = False And InStr(encoding.GetString(buffer), "Password:") Then
            'Send Pass
                Dim sendBuffer() As Byte = encoding.GetBytes("my password" & vbCr)
                netstream.Write(sendBuffer, 0, sendBuffer.Length)
                Console.WriteLine(" SENT: {0}", encoding.GetString(sendBuffer))
                hasLoggedPass = True

            ElseIf hasLoggedPass And hasLoggedUser Then
            'here is where you can get the prompt and do your cd and whatever. you can do it all at once but i don't recommend it. You sould wait for a response.
                Console.WriteLine("All done...")
                Exit Do
            End If

        Loop
        Console.Read()

    End Sub

```
