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("10.91.12.240", "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>"
        streamWriter.WriteLine(request)
        streamWriter.Flush()
        Do
            numberOfBytesRead = ns.Read(myReadBuffer, 0, myReadBuffer.Length)
            myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
        Loop Until ns.DataAvailable

       response = myCompleteMessage.ToString

        client.Close()
        txtContTag.Text = response

-----------------------------------------

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

Regards,
Gnanesh
angnaneshAsked:
Who is Participating?
 
Luis PérezSoftware Architect in .NetCommented:
Here's a simple article showing TCP Client/Server communications in VB.net:
http://www.eggheadcafe.com/articles/20020323.asp

Hope that helps.
0
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.