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 6844797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:25:20+00:00 2026-05-27T00:25:20+00:00

I am attempted to write a small asynchronous socket library that I can use

  • 0

I am attempted to write a small asynchronous socket library that I can use to create a client/server application. I can get all of the code to run fine if I leave it in the form, however if I try to move it out into its own class, I cannot figure out how to update the form with connection status, things like that. Below is code, shortened a bit just to make this easier to read and type.

Form Code:

Class Form1
Dim Network as NetworkModule

Public Sub Button1_Click(Sender, e) Handles Button1.Click
    Network = New NetworkModule("127.0.0.1", 1234)
End Sub
End Class 'Form1

NetworkModule class:

Class NetworkModule
    Private mSocket as Socket

    Public Sub New(IP as string, Port as Integer)
        Dim remoteEP as New IPEndpoint(IP.Parse(IP), Port)
        mSocket = New Socket(internetwork, stream, tcp)
        mSocket.BeginConnect(remoteEP, New AsyncCallback(AddressOf onConnect), mSocket)
        Notify("Connection to " & remoteEP.ToString)  'This one works
     End Sub 'New

     Private Sub onConnect(ar as IAsyncResult)
         mSocket = CType(ar.AsyncState, Socket)
         mSocket.EndConnect(ar)
         Notify("Connected")  'This one never shows
     End Sub 'onConnect

    Private Delegate Sub _Notify(Msg as String)
    Private Sub Notify(Msg as String)
        If Form1.txtLog.InvokeRequired Then
            Form1.txtLog.Invoke(New _Notify(AddressOf Notify), Msg)
            Exit Sub
        End if
        Form1.txtLog.Text &= Msg & vbcrlf
    End Sub 'Notify
End Class 'NetworkModule

There is actually more to that class, but I never get anything after the first message goes out. I’m not sure where to go from here. I’ve tried lots of different methods that I’ve found on the google searches, some from here, some not. Anybody have any ideas?

  • 1 1 Answer
  • 0 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-27T00:25:20+00:00Added an answer on May 27, 2026 at 12:25 am

    Here’s how I would rewrite it:

    Class Form1
        Private Network as NetworkModule
        Public NotifyDelegate As NetworkModule.NotifyDelegate
    
        Public Sub New()
            NotifyDelegate = New NetworkModule.NotifyDelegate(AddressOf Notify)
        End Sub
    
        Public Sub Button1_Click(Sender, e) Handles Button1.Click
            Network = New NetworkModule("127.0.0.1", 1234, Me)
        End Sub
    
        Public Sub Notify(Msg As String)
            txtLog.Text &= Msg & vbCrLf
        End Sub
    End Class 'Form1
    

    NetworkModule Class (partial):

    Class NetworkModule
        Public Delegate Sub NotifyDelegate(Msg as String)
    
        Private Sub Notify(Msg as String)
            If m_Form.InvokeRequired Then
                m_Form.Invoke(Form1.NotifyDelegate, Msg)
            Else
                m_Form.Notify(Msg)
            End If
        End Sub 'Notify
    
        Private mSocket as Socket
    
        Private m_Form As Form1
    
        Public Sub New(IP as string, Port as Integer, oForm As Form1)
            m_Form = oForm
            Dim remoteEP as New IPEndpoint(IP.Parse(IP), Port)
            mSocket = New Socket(internetwork, stream, tcp)
            mSocket.BeginConnect(remoteEP, New AsyncCallback(AddressOf onConnect), mSocket)
            Notify("Connection to " & remoteEP.ToString)  'This one works
         End Sub 'New
    

    Update with Interface approach

    A better mechanism than passing the form itself is to implement an interface. To do this, first create the interface definition (note that the delegate has moved to the Interface for convenience):

    Public Interface INotify
        Sub Notify(Msg As String)
        Delegate Sub NotifyDelegate(Msg As String)
    End Interface
    

    Then Implement the interface in the Form. Note that the form now determines whether or not Invoke is required. This allows the INotify interface to be used in non-UI scenarios, such as logging to disk or the event log.

    Public Class Form1
        Implements INotify
    
        Public Sub Notify(Msg As String)
            txtLog.Text &= Msg & vbCrLf
        End Sub
    
        Private Sub INotify_Notify(Msg As String) Implements INotify.Notify
            If Me.InvokeRequired Then
                Me.Invoke(New INotify.NotifyDelegate(AddressOf Notify), Msg)
            Else
                Me.Notify(Msg)
            End If
        End Sub 'Notify
    
        Private Network As NetworkModule
    
        Public Sub Button1_Click(Sender, e) Handles Button1.Click
            Network = New NetworkModule("127.0.0.1", 1234, Me)
        End Sub
    End Class 'Form1
    

    Finally, store a reference to the INotify interface instead of the Form in NetworkModule (note the NetworkModule no longer needs to know or care that an Invoke may be required):

    Public Class NetworkModule
        Public Delegate Sub NotifyDelegate(Msg As String)
    
        Private m_Notifier As INotify
    
        Private Sub Notify(Msg As String)
            m_Notifier.Notify(Msg)
        End Sub 'Notify
    
        Public Sub New(IP As String, Port As Integer, oNotifier As INotify)
            m_Notifier = oNotifier
            ' The addition code here
        End Sub 'New
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several small open-source projects that I wrote. All my attempts to find
I've attempted to write a brief utility functor that takes two std::pair items and
I've attempted to write a PHP class that gets the uptime of my Linux
When I attempt to open a file to write, I get an error: Ada.IO_Exceptions.Name_Error
I was practicing regular expressions and attempted to write a regex which will detect
Has anybody ever seen/attempted to write a service locator pattern which uses a Guice
I'm trying to write a simple SNPP (Simple Network Paging Protocol) client using sockets.
I'm using python 3 for a small extra credit assignment to write an RSA
I'm trying to use sqlcmd on a windows machine running SQL Server 2005 to
I have written a small util in an app that syncs the time from

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.