Solved TCP listen best practise

Posted on 2012-08-12
Last Modified: 2012-08-29
I'm using the below code to listen for data coming from a linux server and it's working fine.
But how do I handle multiple data sending i.e. once one data stream has been received I need to be listening for further data streams.  These could come one after another or even at the same time

        Dim strHostName As String
        strHostName = System.Net.Dns.GetHostName()
        Dim PORT As Integer = 50000
        Dim serverSocket As New TcpListener(IPAddress.Any, PORT)

        Dim requestCount As Integer
        Dim clientSocket As TcpClient

        clientSocket = serverSocket.AcceptTcpClient()

        requestCount = 0

        While (True)
                requestCount = requestCount + 1
                Dim networkStream As NetworkStream = clientSocket.GetStream()
                Dim bytesFrom(10024) As Byte
                networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)
                Exit While
            Catch ex As Exception
            End Try
        End While


Open in new window

Question by:Gary
    LVL 83

    Assisted Solution

    Best practice is to receive the data and then process it somewhere else (on a separate thread) instead of processing it straight after receiving.
    LVL 58

    Author Comment

    How do I do that, the listener is running in a background process at the mo, when finished it just restarts.  I'm not sure how to make the listener continue on without closing and restarting it
    LVL 83

    Assisted Solution

    LVL 58

    Author Comment

    Got it working already.
    LVL 27

    Expert Comment

    Best practice is to receive the data and then process it somewhere else (on a separate thread)
    I would prefer reverse approach - using async sockets operations - process data in main thread and listening asynchronously
    LVL 58

    Author Comment

    How does that work? Code?
    LVL 27

    Accepted Solution

    I got this source from somewhere in the Net (sorry, donno the author) and adopt to my needs
    On server:
    Private WithEvents _server As TcpMessenger.MessageServer
        Public Sub Listen(Optional ByVal port As Integer = 0)
            If Not _server Is Nothing Then _server.Dispose()
            _server = New TcpMessenger.MessageServer(port, _encoding)
            _server.DataType = TcpMessenger.MessageType.DataPacket
        End Sub
        Public Sub StopListener()
            If Not _server Is Nothing Then _server.Dispose()
        End Sub
        Public Sub SendDataToClient(ByVal _host As TcpMessenger.HostInfo, ByVal header As String, ByVal data() As Byte)
            _server.Send(_host, header, data)
        End Sub

    On client:
    Private WithEvents _messenger As TcpMessenger.MessageClient
    _messenger = New TcpMessenger.MessageClient

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now