|
-
Aug 10th, 2011, 12:08 PM
#1
[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:
- When a Client connects (obviously)
- 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)
-
Aug 10th, 2011, 12:30 PM
#2
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
-
Aug 10th, 2011, 12:33 PM
#3
Re: Checking for an error before it happens [TcpListener]
 Originally Posted by ForumAccount
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...
-
Aug 10th, 2011, 12:46 PM
#4
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).
-
Aug 10th, 2011, 01:46 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|