[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

How do I explore the error information in a .NET TCP Connect async callback?

So, I wrote a TCP client in VB.NET using the asynchronous functions.  When I try to connect to an inactive server I get an exception in the "Connection" callback saying the connection was refused by the remote PC, which is fine.

However, I'm trying to *handle* this circumstance and having trouble.  When I but a breakpoint/watch on the IAsyncResult returned by the callback, I can see an error code (see Figure 1).  But that "ErrorCode" does not seem to be a member of the returned IAsyncResult object, perhaps a base class ConnectAsyncResult object?   (Please see first attached figure, it explains it better).

So I try to basically "cast" the incoming object into a ConnectAsyncResult and I get an error "not accessible because it is 'Friend'.  SURELY there is a way to get at this error info, if I can watch it, I just can't seem to get the syntax right.

Thanks for any help.

watch.pngfriend.png
0
RonMexico
Asked:
RonMexico
  • 4
  • 3
  • 2
1 Solution
 
Sammy AgeilCommented:
I am not a VB guy but I think you should handle your exception in a try catch block
Private Sub ConnectCallback (byval Ar as IAsyncResult)
Try
'here is your code as is
Catch ex As Exception 'HERE you can find out the type of exception and handle it

End Try
End Sub
0
 
RonMexicoAuthor Commented:
I'd rather handle this particular condition directly, instead of using catch-all exception handling.  I prefer to use exception handling for off-normal ("exceptional") things.  The server being offline is a very normal condition.

But preferences aside, in any case there must be a way to get that ErrorCode info (since I can watch it) so I'd like to know the proper syntax.  I'll learn a lot from that answer since frankly I'm confused even about the relationship between ConnectAsyncResult and IAsyncResult.
0
 
Sammy AgeilCommented:
I'd rather handle this particular condition directly, instead of using catch-all exception handling
Who said anything about a catch all handling?
The server being offline is a very normal condition.
No, not really, if the application is expecting the server to be online, then the server being offline is an exception to the norm.

Good luck.
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
RonMexicoAuthor Commented:
You have no idea if my application can "expect" my server to be online.
0
 
RonMexicoAuthor Commented:
Okay... sorry I appreciate any thoughts.  

What I'd like to know is how to read that ConnectAsyncResult.ErrorCode programmatically.
0
 
Jaime OlivaresCommented:
The ErrorCode property belongs to an internal class of the socket library. You shouldn't try to read it, although you can do it through reflection.
As mentioned above, your best chance is to trap the SocketException, which contains the error code.
0
 
Jaime OlivaresCommented:
The reflection way (unrecommended) would be:

    Dim objType As Type = ar.GetType()
    Dim pInfo As System.Reflection.PropertyInfo = objType.GetProperty("ErrorCode")
    Dim PropValue As Object = pInfo.GetValue(obj, Reflection.BindingFlags.GetProperty, Nothing, Nothing, Nothing)
0
 
RonMexicoAuthor Commented:
Thank you very much,

"The ErrorCode property belongs to an internal class of the socket library. You shouldn't try to read it"

Would you mind expanding on this a little?  Why is it available to the debugger (as evidenced by it showing up in the watch) and not to my code?  (Explaining by reference to, say, an MSDN article is fine.)
0
 
Jaime OlivaresCommented:
You are looking at the internal implementation of Microsoft, but as it is declared as an internal (aka Friendly) class, it is not intended to be used directly by the developers.
Although you can reach it by using reflection, there is not guarantee that this internal implementation will change in the future.
Formally, you should stick to the public classes only. In this case, by trapping the SocketException.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now