Main Site Documentation

Catch SocketExceptions


#1

So… I have done a logon system that connects to a service via Network. It works great until the service is either shutdown or networkcable is disconnected.

i know im getting SocketExceptions. However i cant seem to catch them in anyway.


    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (24) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::send [IP: 0000] ####
    #### System.Net.Sockets.Socket::Send [IP: 0018] ####
    #### System.Net.Sockets.NetworkStream::Write [IP: 0051] ####
    #### System.Net.InputNetworkStreamWrapper::Write [IP: 000a] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 007d] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### Gadgeteer.Networking.HttpRequest::HandleRequestSync [IP: 01a8] ####
    #### SocketException ErrorCode = 10054
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = 10054
    #### SocketException ErrorCode = 10054
    #### SocketException ErrorCode = 10054
    #### Exception System.Net.WebException - 0x00000000 (24) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00c8] ####
    #### Gadgeteer.Networking.HttpRequest::HandleRequestSync [IP: 01a8] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll

Is there anyway to catch these exceptions or in anyway.
Trying to display a error message once the connection is unavailable.
Any tips would be nice and extreamly helpful.


#2

@ omborddata - Can you post some code that shows how you use networking? You should be able to catch them around send/receive calls.


#3
if (c != null)
                {
                    try
                    {
                        HttpRequest httpReq = WebClient.GetFromWeb("http://" + Variables.ServiceSett.ServiceHostnameIP  + ":" + Variables.ServiceSett.PortNumber + "/ODCardReaderService" +  "/signonoff?ID=" + c.Card + "&IP=" + Variables.DeviceCurrentIP  + "&DevID=" + Variables.ServiceSett.DeviceName );
                        HttpRequest.ResponseHandler resp = new HttpRequest.ResponseHandler(Program_ResponseReceived);
                        httpReq.ResponseReceived += resp;
                    }
                    catch (Exception ex)
                    {
                        //Call ShowErrorWindow("SERVERERROR")
                        string s = "";
                        Debug.Print(ex.Message);
                    }
                    finally{ 
                        c = null;
                    }
                }
                else
                {
                    Thread.Sleep(50);
                }

This is the code where the exception is happening. The code works fine while service and cable is connected.


#4

@ omborddata - It looks like Gadgeteer might be doing things internally on a different thread that you cannot catch. You can always take a look at its source to dig deeper: http://gadgeteer.codeplex.com/


#5

@ andre.m
I was expecting this to happen, i want to catch the exception to display a error message and to reset to another screen on the device. As it is right now when this exception is thrown i stays on a screen and stays there untill either you reconnect the cable and start the service, but i want the catch the exception for cosmetic reasons.


#6

Yea checking for network access is easy i posted the wrong socketException. The part where i needed to catch that is when the service is down, i managed to fix it with a while loop, but it didnt handle fast requests. And begin to live its own life when you try to do a couple of card swipes in a row in fast succession.

But thanks for everything ill look in to it and post if i find anything.