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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:59:48+00:00 2026-05-18T08:59:48+00:00

I have the following class in a Windows Service that is experiencing some vary

  • 0

I have the following class in a Windows Service that is experiencing some vary strange shutdown behaviors. The server is shutting down from time-to-time with only this message in the event logs and no messages in the trace logs, “The Broadcaster service terminated unexpectedly. It has done this 1 time(s).”

Public Class ServerSocket
    Implements IServerSocket

    Public Event ClientConnected(ByVal sender As Object, ByVal e As EventArgs(Of IClientSocket)) Implements IServerSocket.ClientConnected

    Private _socket As Socket
    Private ReadOnly _settings As IBroadcasterServiceSettingsSection
    Private ReadOnly _traceSource As ITraceSource

    Public Sub New()
        Me.New(BroadcasterServiceSettingsSection.GetSection, BroadcasterTraceSource.Instance)
    End Sub

    Public Sub New(ByVal settings As IBroadcasterServiceSettingsSection, ByVal traceSource As ITraceSource)
        _settings = settings
        _traceSource = traceSource
    End Sub

    Public Sub Listen() Implements IServerSocket.Listen
        Dim endPoint As New IPEndPoint(System.Net.IPAddress.Parse(_settings.BroadcasterIPAddress), _settings.BroadcasterPortNumber)
        Try
            _socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1)
            _socket.Bind(endPoint)
            _socket.Listen(SocketOptionName.MaxConnections)
            _socket.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), Nothing)
            _traceSource.TraceInformation("ServerSocket listening for new clients.")
        Catch ex As Exception
            _traceSource.TraceCritical("ServerSocket caughtException trying to wait for a new client.")
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' First attempts to shutdown the socket to clean up any remaining data left to send or receive. Then closes
    ''' the socket to release all connections and clean up unmanaged resources. See also <seealso cref="System.Net.Sockets.Socket.Shutdown">Socket.Shutdown</seealso>
    ''' and <seealso cref="System.Net.Sockets.Socket.Close">Socket.Close</seealso>
    ''' </summary>

    Public Sub Close() Implements IServerSocket.Close
        Try
            _socket.Shutdown(SocketShutdown.Both)
        Catch ex As Exception
            _traceSource.TraceEvent(TraceEventType.Error, "Shutting down Server Socket caused an exception.", ex.Message, ex.StackTrace)
        End Try

        Try
            _socket.Close()
        Catch ex As Exception
            _traceSource.TraceEvent(TraceEventType.Error, "Closing the Server Socket caused an exception.", ex.Message, ex.StackTrace)
        End Try

        _traceSource.TraceEvent(TraceEventType.Information, "ServerSocket closed.")
    End Sub

    Private Sub AcceptCallback(ByVal ar As IAsyncResult)
        Dim s As Socket = Nothing

        Try
            s = _socket.EndAccept(ar)
        Catch ex As Exception
            _traceSource.TraceInformation("ServerSocket caught exception trying to get new socket for client.", ex.Message, ex.StackTrace)
        End Try

        Try
            ' call the begin accept as soon as possible so that I can get the next incoming client 
            _socket.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), Nothing)
        Catch ex As Exception
            _traceSource.TraceEvent(TraceEventType.Critical, "ServerSocket caughtException trying to wait for a new client.", ex.Message, ex.StackTrace)
        End Try

        Try
            If s IsNot Nothing Then
                Dim clientSocket As IClientSocket = New ClientSocket(s)
                OnClientConnected(New EventArgs(Of IClientSocket)(clientSocket))
            End If
        Catch ex As Exception
            _traceSource.TraceEvent(TraceEventType.Critical, "9/23 Review: " + ex.ToString())
        End Try
    End Sub

    Private Sub OnClientConnected(ByVal e As EventArgs(Of IClientSocket))
        RaiseEvent ClientConnected(Me, e)
    End Sub
End Class

One thing about this class that stands out to me is that immediately after _socket.EndAccept is called _socket.BeginAccept is called and after that the work with the “Client Socket” is done. I can’t put my finger on it but this just doesn’t smell right. Should the socket that is being used to listen for new connections be kept as a field? If not, how would you call shutdown on it later? This is a very long running (weeks/months) process.

  • 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-18T08:59:49+00:00Added an answer on May 18, 2026 at 8:59 am

    The way the async Accept works is that you typically issue BeginAccept immediately after accepting one connection, so you are ready for another incoming connection attempt. I think the flow here is quite typical – when you get the callback for the first incoming connection, you issue EndAccept to complete it and then issue another BeginAccept to keep the listening socket ready for the next.

    You will use the socket s for subsequent I/O on the first incoming connection, so you do need to keep that around. the logic that does that is the setup of clientSocket using s as a parameter.

    _socket is the one your code uses to listen for all incoming connections.

    There’s a detailed description of how this is all supposed to be done here.

    I can’t see any problems here with the socket handling logic. I suggest you attach a debugger to your service and try to work out what is the context at the time of exit.

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

Sidebar

Related Questions

I have a windows service that references an assembly which contains the following class.
I have a windows service that creates an instance of UdpClient class and listen
I have the following class written by someone else that I'm trying to understand
I have a windows service that has a lot of work to do simultaneously.
I have a Windows service hosting a WCF service that broadcasts information to several
I have a windows service which is using a method from a class library
I have the following code in a class that is called from a web
I have a workflow inside a Windows Service that is a loop that performs
I have a windows service which is using a method from a class library
I have a windows service that I have been writing in Vb.Net. As part

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.