Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3976766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:50:38+00:00 2026-05-20T04:50:38+00:00

Basically, I have a server application and associated agent process for communicating heart-beat data

  • 0

Basically, I have a server application and associated agent process for communicating heart-beat data to the server. The server application implements a TCP socket class (TCPDevice), which is called from a WinForm. I am aware of the restriction on updating the UI from processes running on separate threads, and that the preferred technique for acheiving this is via the use of Delegates in conjunction with the Invoke method. Having had very little need previously for using Delegates (apart from of course std windows events and the BackGroundWorker control), I am at a loss as to how to do this in the context of my application, and would appreciate some assistance, although it has occurred to me that I could use the backgroundworker for marshalling updates to the UI.

Eventually the application will need to update a grid of devices via feeds from the defined agents, however for the purpose of this exercise simply updating a status bar on the UI will suffice. The section of the code which I believe is relevant to propogating the code to the form is contained in the OnDataReceived method.

Below is a code excerpt from the protoype app which should put the above into appropriate context:

Code from the form:

Private _device As TcpDevice

Private Sub btnListen_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnListen.Click
        _device = New TcpDevice(uxIPaddress.Text, uxHost.Text, CInt(uxPort.Text))
        _device.Listen()
        Me.btnListen.Enabled = False
End Sub

Code from TCPDevice Class

Public Class TcpDevice : Implements CNIMonitor.Core.Domain.IDevice

    Private _ipAddress As String
    Private _hostName As String
    Private _port As Integer
    Private _status As Integer
    Private _previousStatus As Integer
    Private _listener As TcpListener
    Private _localAdd As System.Net.IPAddress


    Private _activeConnection As TcpClient
    Private _receiveBuffer(0) As Byte
    Private _sendBuffer As Byte
    Private _message As String
    Private _dataRead As Integer 'The value returned from the getstream.endread method


    Public Sub New(ByVal ipAddress As String, ByVal HostName As String, ByVal port As String)
        _ipAddress = ipAddress
        _hostName = HostName
        _port = port
    End Sub

    Public Sub Connect() Implements CNIMonitor.Core.Domain.IDevice.Connect

    End Sub

    Public Sub Disconnect() Implements CNIMonitor.Core.Domain.IDevice.Disconnect

        _activeConnection.Close()
        _listener.Stop()

        _activeConnection = Nothing

    End Sub

    Public Property HostName() As String Implements CNIMonitor.Core.Domain.IDevice.HostName
        Get
            HostName = _hostName
        End Get
        Set(ByVal value As String)
            _hostName = value
        End Set
    End Property

    Public Property IPAddress() As String Implements CNIMonitor.Core.Domain.IDevice.IPAddress
        Get
            IPAddress = _ipAddress
        End Get
        Set(ByVal value As String)
            _ipAddress = value
        End Set
    End Property

    Public Sub Listen() Implements CNIMonitor.Core.Domain.IDevice.Listen
        Try
            'Server component listens on the specified tcpip port
            _localAdd = System.Net.IPAddress.Parse(_ipAddress)
            _listener = New TcpListener(_localAdd, _port)
            _listener.Start()
            _listener.BeginAcceptTcpClient(AddressOf OnClientConnect, _listener)
            'Update the status message                
            Debug.WriteLine("Listening for host " + _ipAddress + " on port " + _port.ToString + ".")
        Catch ex As SocketException
            Debug.WriteLine("Failed listening on " + _ipAddress + " over port " & vbCrLf & " .")
        End Try
    End Sub

    Private Sub OnClientConnect(ByVal ar As IAsyncResult)

        Debug.WriteLine("Received connection from " + ar.AsyncState.ToString)
        'Get the connection object                
        _activeConnection = _listener.EndAcceptTcpClient(ar)
        'on Client connect                
        Debug.WriteLine("Client" + _ipAddress + " connected over port " & vbCrLf & " ." & _port.ToString)
        'Bind the event handler for dealing with incoming data                
        _activeConnection.GetStream.BeginRead(_receiveBuffer, 0, _receiveBuffer.Length, AddressOf onDataReceived, _
            Nothing)
    End Sub

    Private Sub onDataReceived(ByVal ar As IAsyncResult)
        Dim receiveLength As Integer = 0
        ReDim _receiveBuffer(_activeConnection.ReceiveBufferSize - 1)
        Try
            Debug.WriteLine("Receiving from " + _ipAddress + ".")
            ' Complete the BeginReceive() asynchronous call by EndReceive() method                
            ' which will return the number of bytes written to the stream                 
            ' by the client                
            receiveLength = _activeConnection.GetStream.EndRead(ar)
            _message = Encoding.ASCII.GetString(_receiveBuffer, 0, receiveLength)
            _activeConnection.GetStream.BeginRead(_receiveBuffer, 0, _receiveBuffer.Length, AddressOf onDataReceived, Nothing)

            'NOW PASS THE INFORMATION RECEIVED BACK TO THE FORM

        Catch
            Debug.WriteLine("Transmission complete")
        End Try
    End Sub



    Public Property Port() As Integer Implements CNIMonitor.Core.Domain.IDevice.Port
        Get
            Port = _port
        End Get
        Set(ByVal value As Integer)
            _port = value
        End Set
    End Property

    Public Property PreviousStatus() As Integer Implements CNIMonitor.Core.Domain.IDevice.PreviousStatus
        Get
            PreviousStatus = _previousStatus
        End Get
        Set(ByVal value As Integer)
            _previousStatus = value
        End Set
    End Property

    Public Property Status() As Integer Implements CNIMonitor.Core.Domain.IDevice.Status
        Get
            Status = _status
        End Get
        Set(ByVal value As Integer)
            _status = value
        End Set
    End Property


End Class
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:50:38+00:00Added an answer on May 20, 2026 at 4:50 am

    With a little further investigation managed to get the code to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a secure TCP server in .NET. This was basically as simple
I have a History Table in SQL Server that basically tracks an item through
Basically I have the following class: class StateMachine { ... StateMethod stateA(); StateMethod stateB();
I have a server application that receives information over a network and processes it.
I have a server application that spins up and monitors about 8 separate processes
I have an application running in IIS which connects to a SQL Server 2008
I basically have a page which shows a processing screen which has been flushed
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
I am looking in to ways to enable a site to basically have something
I would like to do the following. Basically have a stored procedure call another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.