[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vb.net TCP listen best practise

Posted on 2012-08-12
7
Medium Priority
?
1,474 Views
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
        serverSocket.Start()

        clientSocket = serverSocket.AcceptTcpClient()

        requestCount = 0

        While (True)
            Try
                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
                networkStream.Flush()
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End While

        clientSocket.Close()
        serverSocket.Stop()

Open in new window

0
Comment
Question by:Gary
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1332 total points
ID: 38285758
Best practice is to receive the data and then process it somewhere else (on a separate thread) instead of processing it straight after receiving.
0
 
LVL 58

Author Comment

by:Gary
ID: 38285776
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
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1332 total points
ID: 38285832
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
LVL 58

Author Comment

by:Gary
ID: 38285921
Got it working already.
0
 
LVL 28

Expert Comment

by:Ark
ID: 38291117
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
0
 
LVL 58

Author Comment

by:Gary
ID: 38291614
How does that work? Code?
0
 
LVL 28

Accepted Solution

by:
Ark earned 668 total points
ID: 38295471
I got this source from somewhere in the Net (sorry, donno the author) and adopt to my needs
Using:
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
TcpMessenger.zip
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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