Main Site Documentation

Socket exception, driving me insane


#1

Hey guys,

To explain briefly, I’m playing around with taking pictures and uploading them to a local web server. I’m storing pictures in an array and have a loop running on another thread to upload any new additions to the array. I seem to have run into a problem that I just can’t wrap my mind around and it’s driving me absolutely mad. Would be very grateful if anyone could offer some insight.

The following line is what’s throwing the exception:


HttpRequest request = HttpHelper.CreateHttpPostRequest("http://192.168.0.10/s/u.php", POSTContent.CreateBinaryBasedContent(picArray[upSuccess].PictureData), "image/jpeg");

Here’s the troublesome part…
When that line runs for the first time after compiling it works just fine. As soon as I take another picture and that line runs again, I get the following exception:

[quote] #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (12) ####
#### 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] ####
#### Gadgeteer.Networking.HttpRequest::HandleRequestSync [IP: 0143] ####
#### 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

An exception occured while connecting to the Internet. Please, make sure that a valid URL is used and a network connection is up.

Exception was thrown: System.Net.Sockets.SocketException
The thread ‘’ (0xc) has exited with code 0 (0x0).[/quote]


#2

@ johndgaf - The 10054 code usually means that the remote host terminated the connection. Is that server setup properly? Can you see anything in its logs as to why it would close the connection?


#3

Don’t see anything in the logs at all. What’s interesting is that if I send the exact same request several times, it will work and the exact same picture will be uploaded.

As long as picArray[upSuccess] is pointing to picArray[0], it runs flawlessly and just uploads that picture over and over. As soon as upSuccess = 1 and it now points to picArray[1] (which is not null, I checked), it throws that error and the new picture is not uploaded and the thread terminates completely.


#4

@ johndgaf - Can you create as minimal a program as possible that reproduces the issue? Does it fail no matter what picture is in picArray[1]? Which SDK version and board are you using?


#5

John,

Here you go boss. Camera takes a picture on button press, picture is added to the array. When number of uploads is less than total items in the array, it attempts to upload new items…same error, works the first time and breaks on the next.

I’m using a FEZ Spider, 4.2 SDK if I’m not mistaken.

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using System.Text;
using System.Security;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHI.Premium.Net;


namespace wifi
{
    public partial class Program
    {

        bool wifiConnected = false;
        int count = 0;
        int upSuccess = 0;
        public static GT.Picture[] picArray;

        void ProgramStarted()
        {
            Debug.Print("Program Started");
            display_TE35.SimpleGraphics.Clear();
            camera.PictureCaptured += new Camera.PictureCapturedEventHandler(camera_PictureCaptured);
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            wifi_RS21.Interface.Disconnect();
            picArray = new GT.Picture[100];
            new Thread(connectWifi).Start();
            new Thread(uploader).Start();
     
        }

        void connectWifi()
        {

            while (1 > 0)
            {
                try
                {
                    if (wifiConnected == false)
                    {
                        Debug.Print("Connecting to WiFi");

                        if (!wifi_RS21.Interface.IsOpen)
                            wifi_RS21.Interface.Open();

                        if (!wifi_RS21.Interface.NetworkInterface.IsDhcpEnabled)
                            wifi_RS21.Interface.NetworkInterface.EnableDhcp();

                        NetworkInterfaceExtension.AssignNetworkingStackTo(wifi_RS21.Interface);

                        wifi_RS21.Interface.WirelessConnectivityChanged += new WiFiRS9110.WirelessConnectivityChangedEventHandler(Interface_WirelessConnectivityChanged);

                        WiFiNetworkInfo[] ScanResp = wifi_RS21.Interface.Scan("ssid");
                        Debug.Print(ScanResp.Length.ToString() + " network(s) found");                      

                        if (ScanResp != null && ScanResp.Length > 0)
                        {
                            wifi_RS21.Interface.Join(ScanResp[0], "password");
                            Thread.Sleep(10000);
                        }
                        else
                        {
                            Debug.Print("Could not find network, retrying in 3 seconds");
                            Thread.Sleep(3000);
                        }
                    }
                    else
                    {
                        Thread.Sleep(8000);
                    }
                }
                catch (Exception e)
                {
                }

            }
        }
        

        void Interface_WirelessConnectivityChanged(object sender, WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            if (e.IsConnected == true)
            {
                wifiConnected = true;
                Debug.Print("Connected");
            }
            else
            {
                wifiConnected = false;
                Debug.Print("Connection failed");
            }
        }

   

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            camera.TakePicture();
        }

        void camera_PictureCaptured(Camera sender, GT.Picture picture)
        {
            try
            {
                Debug.Print("Picture captured");
                picArray[count] = picture;
                count++;
            }
            catch (Exception err)
            {
                Debug.Print("Pic capture failed: " + err.Message);
            }

        }


        void uploader()
        {

            while (1 > 0)
            {
                if (upSuccess < count)
                {
                    try
                    {
                        if (wifi_RS21.IsNetworkConnected == true && picArray[upSuccess].PictureData.Length > 0)
                        {

                            display_TE35.SimpleGraphics.DisplayImage(picArray[upSuccess], 0, 0);

                            POSTContent picData = POSTContent.CreateBinaryBasedContent(picArray[upSuccess].PictureData);
                            HttpRequest request = HttpHelper.CreateHttpPostRequest("http://192.168.1.65/s/u.php", picData, "image/jpeg");
                            request.ResponseReceived += new HttpRequest.ResponseHandler(request_ResponseReceived);
                            request.SendRequest();
                       
                            while (!request.IsReceived)
                             {
                            Thread.Sleep(1000);
                             }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.Print("POST request failed: " + e.Message);
                    }

                }
                Thread.Sleep(2000);
            }
        }

        void request_ResponseReceived(HttpRequest sender, HttpResponse response)
        {
            try
            {

                if (response.Text.IndexOf("success") > 0)
                {
                    Debug.Print("Uploaded picture " + upSuccess.ToString());
                    upSuccess++;
                }
                else
                {
                    Debug.Print("Upload failed");
                }
            }
            catch (Exception e)
            {
                Debug.Print("Upload failed: " + e.Message);
            }
        }
       
    }
 }

#6

@ johndgaf - Can you upgrade your project to our just released 4.3 SDK and see if it fixes it?


#7

Project is upgraded to new 4.3 SDK, receiving a 10040 exception now when attempting to upload image via POST request…

[quote] #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (10) ####
#### 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] ####
#### Gadgeteer.Networking.HttpRequest::HandleRequestSync [IP: 0142] ####
#### SocketException ErrorCode = 10040
#### SocketException ErrorCode = 10040
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10040
#### SocketException ErrorCode = 10040

An exception occured while connecting to the Internet. Please, make sure that a valid URL is used and a network connection is up.

Exception was thrown: System.Net.Sockets.SocketException
The thread ‘’ (0xa) has exited with code 0 (0x0).[/quote]

10040: Message too long, same POST I had some success with earlier though

Getting this on every single request, whereas before at least some would go up with others getting the 10054 ???

Edit: On further attempts, GET requests are returning a NULL on the first attempt and a 10053 on any future attempts. So stumped right now :-[


#8

@ johndgaf - Are you able to use Wireshark to look at the HTTP traffic going back and forth?


#9

did you maybe start the uploading before the connektion is up??


#10

@ johndgaf -

If you create an equivalent program that runs on a desktop/laptop, does that behave better? does it react well under pressure? can you reproduce the error at all on a desktop/laptop?

This may help isolate the issue as being either in the NETMF area or the external host/environment/lan.


#11

Hi,
I just wanted to mention that some HTTP servers have limits on the post/get request body size.

So make sure you have set that in the binding settings in the web.config if its an asp.net site running a service that handles the images…
Look for this part:


The maxReceivedMessageSize is probably not in there :wink: and limits the size.

In the example I just made it very big.

Cheers Olaf


#12

Truly appreciate all the responses guys, still working on it.

VB-Daniel: Nope, connection is established before I start uploading.
Barraty: I’ve personally never come across this issue on a desktop/laptop.
Beastyboy: What’s interesting to me is that it was working, if only partially, before I upgraded to 4.3. The size exception first started with 4.3.

Attempting to do some research with Fiddler but I can’t seem to figure out how to set proxy for HttpRequest in 4.3 ???


#13

Thanks everyone for the help,

Have got a temporary fix at the moment, working toward something less shoddy. Curiously enough, when I run the requests through a transparent proxy like Fiddler or Charles they go through without throwing off those exceptions.

The one exception I’ve been unable to fix though is the 10040, message too long. Can’t understand why it’s happening, was working just fine with 4.2. I’m splitting my picture up into several byte arrays and sending them over multiple requests to avoid the exception, but it’s taking waaaaay too long to do it this way. Any ideas? :’(