Results 1 to 5 of 5

Thread: [RESOLVED] Checking for an error before it happens [TcpListener]

  1. #1

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Resolved [RESOLVED] Checking for an error before it happens [TcpListener]

    Basically, in my Tcp Library I'm working on, whenever the system is told to "Stop", the OnClientConnected() Routine fires off, which is only raised under two circumstances:

    1. When a Client connects (obviously)
    2. When the server is told to shutdown


    Now, the first one is handled just fine. But the second one is not. In fact, it has caused me a great deal of headache because it keeps throwing exceptions on this line in particular:

    Code:
    Dim _tcpClient As System.Net.Sockets.TcpClient = _tcpListener.EndAcceptTcpClient(IR)
    When I hover over while debugging, I can see a few members that reveal exception data I could utilize; but unfortunately those members are private and I don't want to utilize reflection unless I have to since it could slow down performance where there may not be a real reason to.

    I have thought of writing in a Boolean check to see if the server received a shutdown signal and that would indicate that it's time to not accept anything, but I want to avoid that if possible.

    Is there any way to see if the TcpListener is no longer accepting connections and has had .Stop() called without reverting to catching the ObjectDisposedException that is raised? (because the Socket that is "accepted" is actually a disposed one)

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Checking for an error before it happens [TcpListener]

    The Active property is a protected member of the TcpListener class, and it gets set to false once Stop() is called. All you have to do is just inherit the TcpListener:
    Code:
    Public Class TcpListenerEx
        Inherits TcpListener
    
        '//properties
        Public Shadows ReadOnly Property Active() As Boolean
            Get
                Return MyBase.Active
            End Get
        End Property
    
        '//constructors
        Public Sub New(ByVal localEP As IPEndPoint)
            MyBase.New(localEP)
        End Sub
    
        Public Sub New(ByVal localaddr As IPAddress, _
                       ByVal port As Integer)
            MyBase.New(localaddr, port)
        End Sub
    
    End Class

  3. #3

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Re: Checking for an error before it happens [TcpListener]

    Quote Originally Posted by ForumAccount View Post
    The Active property is a protected member of the TcpListener class, and it gets set to false once Stop() is called. All you have to do is just inherit the TcpListener:
    Code:
    Public Class TcpListenerEx
        Inherits TcpListener
    
        '//properties
        Public Shadows ReadOnly Property Active() As Boolean
            Get
                Return MyBase.Active
            End Get
        End Property
    
        '//constructors
        Public Sub New(ByVal localEP As IPEndPoint)
            MyBase.New(localEP)
        End Sub
    
        Public Sub New(ByVal localaddr As IPAddress, _
                       ByVal port As Integer)
            MyBase.New(localaddr, port)
        End Sub
    
    End Class
    Two things I have to note:

    1) When quoting your message, I am surrounded by a bunch of BBC code.
    2) I thought about Inheritance, but now I'll have to rewrite everything to inherit off of this new one. I'll finagle around with it and see what happens.

    EDIT: No I won't. But this will make things really easy actually. Let's see if this works!
    Last edited by formlesstree4; Aug 10th, 2011 at 12:37 PM. Reason: I had a blunder moment...

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Checking for an error before it happens [TcpListener]

    @ #1 - Yeah I colorize my code. It's a personal OCD preference (has to match my IDE).

  5. #5

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Re: Checking for an error before it happens [TcpListener]

    It works, thanks ForumAccount!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width