TCPClient - send data and receive data

Hi Experts Exchange,

I am using VB.Net. I need to send data to tcpclient and get the response data from the same. It should work from a button. Following command coded in Button click event

        Dim response As String
        Dim client As New TcpClient()
        client.Connect("", "8400")
        Dim ns As NetworkStream = client.GetStream()
        Dim streamWriter As New StreamWriter(ns)
        Dim request As String
        Dim numberOfBytesRead As Integer = 0
        Dim myReadBuffer(1024) As Byte
        Dim myCompleteMessage As StringBuilder = New StringBuilder()

        request = "<xml>" _
                    & "<equipment-id>ENTRY</equipment-id> " _
                    & "<request-type>get-tag</request-type> " _
                    & "<request-code>1</request-code>" _
                    & "<ack>0001</ack>" _
                    & "</xml>"
            numberOfBytesRead = ns.Read(myReadBuffer, 0, myReadBuffer.Length)
            myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
        Loop Until ns.DataAvailable

       response = myCompleteMessage.ToString

        txtContTag.Text = response


It is not working, I am writing this network coding first time, please help with process and coding pattern.

Who is Participating?
Luis PérezSoftware Architect in .NetCommented:
Here's a simple article showing TCP Client/Server communications in

Hope that helps.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.