• Welcome to Valhalla Legends Archive.
 

System.Net.Sockets (Asychronous)

Started by NicoQwertyu, April 25, 2007, 07:11 PM

Previous topic - Next topic

NicoQwertyu

When trying to close an open connection, manually (Socket.Shutdown() Socket.Close()), wierd exceptions will get thrown in my callback subs (usually in the callback for receive, but sometimes other areas). Am I suppost to put a try/catch statement everywhere I try to use the socket? That just seems messy. :-/

TIA.

MyndFyre

Try/catch is the appropriate way to do this as Socket calls do not return error codes.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.


Grok

As a side note, Try/Catch is appropriate for nearly everything you write, to gracefully handle unexpected situations without having to crash the user.  It's not a replacement for data validation, logic, and program structure, so don't let us Catch you Trying to use it those ways!   :P

MyndFyre

Quote from: Grok on April 26, 2007, 09:09 AM
As a side note, Try/Catch is appropriate for nearly everything you write, to gracefully handle unexpected situations without having to crash the user.  It's not a replacement for data validation, logic, and program structure, so don't let us Catch you Trying to use it those ways!   :P

Going on that, published Best Practices indicate that you shouldn't just "catch (Exception ex)" - you should try to be specific about the exceptions you catch according to the ones that can be raised.

Having said so, there are certainly places where it's appropriate to do so.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

NicoQwertyu

So what would be the most appropriate block of code (take this very generally, not just for this example):


Private Sub DoStuff(ByVal variableOne As String, ByVal variableTwo As String)
        Try
            If variableOne Is Nothing Or variableTwo Is Nothing Then
                '-- Display error code
                Exit Sub
            End If

            '-- DoSomeStuff
            '--
            '--
            '--

            SomeSocketMethod()
        Catch ex As Exception
            '-- Handle Exception
        End Try
    End Sub


Or:


Private Sub DoStuff(ByVal variableOne As String, ByVal variableTwo As String)
        If variableOne Is Nothing Or variableTwo Is Nothing Then
            '-- Display error code
            Exit Sub
        End If

        '-- DoSomeStuff
        '--
        '--
        '--
        Try
            SomeSocketMethod()
        Catch ex As Exception
            '-- Handle Exception
        End Try
    End Sub

MyndFyre

I would stick with the latter.  Sanitizing/validating input is a very good practice to have in order to prevent security holes from cropping up.  Check your design, too, to see if it would be appropriate to throw an ArgumentException (or derived class).
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Imperceptus

I've wondered if you can nest these? Try/catch into a procdure that has another try/catch it?
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

squeegee

Isn't there a GetLastError function somewhere you can call to get some data on it?

Imperceptus

#9
get data on ex? This might be wrong, but have you tried stack trace. Ive found it useful in asp.net and just learned another way to use in in vb.net.  it might have what you need.
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.