TCPClient - send data and receive data

Posted on 2012-09-20
Last Modified: 2012-11-20
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.

Question by:angnanesh
    1 Comment
    LVL 25

    Accepted Solution

    Here's a simple article showing TCP Client/Server communications in

    Hope that helps.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now