Issue: upload only half image on a webser

Hi everybody, I have a FEZ Spider II and I’m starting using visual studio with C#.
I’m using this code, it starts a webserver at 192.168.100.2 (given though fez config), it takes a picture, but then when I want to see the pic, the uploading lasts too much and ends with half image uploaded and a debug error. Can you help me? Thanks.

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 Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;


namespace Ethernet2
{

    public partial class Program
    {

        Gadgeteer.Networking.WebEvent hello;
        Gadgeteer.Networking.WebEvent takePicture;
        Gadgeteer.Networking.WebEvent seePicture;

        GT.Picture pic = null;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {

      
            ethernetJ11D.UseThisNetworkInterface();
     

            ethernetJ11D.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkUp);
            ethernetJ11D.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkDown);

            camera.PictureCaptured += new Camera.PictureCapturedEventHandler(camera_PictureCaptured);

            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }

        void camera_PictureCaptured(Camera sender, GT.Picture picture)
        {
            pic = picture;
        }

        private void ethernet_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network is down!");
        }

        void ethernet_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network is up!");
            Debug.Print("IP address: " + ethernetJ11D.NetworkSettings.IPAddress);

            Gadgeteer.Networking.WebServer.StartLocalServer(ethernetJ11D.NetworkSettings.IPAddress, 80);

            hello = Gadgeteer.Networking.WebServer.SetupWebEvent("hello");
            hello.WebEventReceived += new WebEvent.ReceivedWebEventHandler(hello_WebEventReceived);

            takePicture = Gadgeteer.Networking.WebServer.SetupWebEvent("takepicture");
            takePicture.WebEventReceived += new WebEvent.ReceivedWebEventHandler(takePicture_WebEventReceived);

            seePicture = Gadgeteer.Networking.WebServer.SetupWebEvent("seepicture");
            seePicture.WebEventReceived += new WebEvent.ReceivedWebEventHandler(seePicture_WebEventReceived);
        }

        void seePicture_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            if (pic != null)
            {
                responder.Respond(pic);

            }
            else
                responder.Respond("Take picture first");
        }

        void takePicture_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            responder.Respond("Taking picture");
            camera.TakePicture();
        }

        void hello_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            Debug.Print("Hello world");
            responder.Respond("Hello world");
        }
    }
}

ERROR:

Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1)

#### Message: 
#### Microsoft.SPOT.Net.SocketNative::send [IP: 0000] ####
#### System.Net.Sockets.Socket::Send [IP: 0018] ####
#### Gadgeteer.Networking.Responder::SendResponse [IP: 0049] ####
#### Gadgeteer.Networking.Responder::Respond [IP: 004a] ####
#### Gadgeteer.Networking.WebEvent::OnWebEventReceived [IP: 004e] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 001d] ####
#### SocketException ErrorCode = 10060

A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
Exception sending web event response (connection was probably terminated)
The thread ‘’ (0xc) has exited with code 0 (0x0).

@ Arcixx - welcome to the community.

I would not recommend using the gadgeteer core, especially with networking. Use the drivers to initialize networking maybe but from there use pure netmf code.

Thank you for your reply.
I’m a beginner so I red this file https://www.ghielectronics.com/downloads/Gadgeteer/NET_Gadgeteer_for_beginners.pdf
At page 38 it initializes the ethernet module (although is different from mine). Have you got any example or code to show me to initialize ethernet through driver/netmf?

You are using the but in Ethernet, which is easy to initialize since it doesn’t require any parameters.

Take a look at this please https://www.ghielectronics.com/docs/30/networking

Just change the netif initialize to use the built in Ethernet instead of enc28

Ok I used the code in the example, but using the built in ethernet how can I use the J11D module? To make work the code I had to disconnect the module in the schematic, and so the functions to take and see the picture don’t work anymore because based on the module.
Moreover with the code I have a debug output:
Network address changed
Network availability: TRUE

but if I connect to 192.168.100.2 is not reachable.

 public partial class Program
    {
        Gadgeteer.Networking.WebEvent hello;
        Gadgeteer.Networking.WebEvent takePicture;
        Gadgeteer.Networking.WebEvent seePicture;

        GT.Picture pic = null;
     static EthernetBuiltIn netif = new EthernetBuiltIn();
     void ProgramStarted()
        {
        
            NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
            NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
          //  ethernetJ11D.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkUp);
          //  ethernetJ11D.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkDown);
            
            camera.PictureCaptured += new Camera.PictureCapturedEventHandler(camera_PictureCaptured);
            
          
            
                        //  netif = new EthernetBuiltIn();
                        netif.Open();
                     //   netif.EnableDhcp();
                     //   netif.EnableDynamicDns();
                        netif.EnableStaticIP("192.168.100.2", "255.255.255.0", "192.168.100.1");
            

                        while (netif.IPAddress == "0.0.0.0")
                        {
                            Debug.Print("Waiting for DHCP");
                            Thread.Sleep(250);
                        }
                        
            Debug.Print("Program Started");

        }

        private static void NetworkChange_NetworkAddressChanged(object sender, Microsoft.SPOT.EventArgs e)
        {
            Debug.Print("Network address changed");   
        }

        private static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            Debug.Print("Network availability: " + e.IsAvailable.ToString());
        }
        void camera_PictureCaptured(Camera sender, GT.Picture picture)
        {
            pic = picture;
        }
/*
        private void ethernet_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network is down!");
        }
...

@ Arcixx - Hi,
the first thing to know is that there is a bug in Gadgeteer Webserver. You can see here, how ist can be solved:
https://www.ghielectronics.com/downloads/Gadgeteer/NET_Gadgeteer_for_beginners.pdf

Here is a working code. Don’t forget to write “seePicture” and “takePicture” everywhere the same way (not e.g. “seepicture”)


 #define ethernetJ11D

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 Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.Net;   // Reference System must be included


namespace Spider_II_Camera_Webserver
{
    public partial class Program
    {
        private static IPAddress localIpAddress = null;
        private Microsoft.SPOT.Net.NetworkInformation.NetworkInterface settings;

        Gadgeteer.Networking.WebEvent hello;
        Gadgeteer.Networking.WebEvent takePicture;
        Gadgeteer.Networking.WebEvent seePicture;

        GT.Picture pic = null;

        void ProgramStarted()
        {

 #if ethernetENC28
            // Needed if an Ethernet ENC28 Module is used
            ethernetENC28.UseThisNetworkInterface();
            ethernetENC28.NetworkSettings.EnableDhcp();
            ethernetENC28.NetworkSettings.EnableDynamicDns();
            ethernetENC28.NetworkUp += ethernetENC28_NetworkUp;
            ethernetENC28.NetworkDown += ethernetENC28_NetworkDown;
 #endif

 #if ethernetJ11D
            // Needed if an Ethernet J11D Module is used
            ethernetJ11D.UseThisNetworkInterface();
            ethernetJ11D.NetworkSettings.EnableStaticIP("192.168.1.66", "255.255.255.0", "192.168.1.1");
            //ethernetJ11D.NetworkSettings.EnableDhcp();
            //ethernetJ11D.NetworkSettings.EnableDynamicDns();
            ethernetJ11D.NetworkUp += ethernetJ11D_NetworkUp;
            ethernetJ11D.NetworkDown += ethernetJ11D_NetworkDown;

            camera.PictureCaptured += new Camera.PictureCapturedEventHandler(camera_PictureCaptured);

 #endif         
        }
        void camera_PictureCaptured(Camera sender, GT.Picture picture)
        {
            pic = picture;
        }

        void ethernetJ11D_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network down");
        }

        void ethernetJ11D_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
              Debug.Print("Network Up");
            while (ethernetJ11D.NetworkSettings.IPAddress == "0.0.0.0")
            {
                Thread.Sleep(300);
            }
            
            localIpAddress = IPAddress.Parse(ethernetJ11D.NetworkSettings.IPAddress);
                       
            settings = ethernetJ11D.NetworkSettings;

            Debug.Print("------------------------------------------------");
            //Debug.Print("MAC: " + ByteExtensions.ToHexString(settings.PhysicalAddress));
            Debug.Print("IP Address:   " + settings.IPAddress);
            Debug.Print("DHCP Enabled: " + settings.IsDhcpEnabled);
            Debug.Print("Subnet Mask:  " + settings.SubnetMask);
            Debug.Print("Gateway:      " + settings.GatewayAddress);
            Debug.Print("------------------------------------------------");

            Gadgeteer.Networking.WebServer.StartLocalServer(ethernetJ11D.NetworkSettings.IPAddress, 80);

            hello = Gadgeteer.Networking.WebServer.SetupWebEvent("hello");
            hello.WebEventReceived += new WebEvent.ReceivedWebEventHandler(hello_WebEventReceived);

            takePicture = Gadgeteer.Networking.WebServer.SetupWebEvent("takePicture");
            takePicture.WebEventReceived += new WebEvent.ReceivedWebEventHandler(takePicture_WebEventReceived);

            seePicture = Gadgeteer.Networking.WebServer.SetupWebEvent("seePicture");
            seePicture.WebEventReceived += new WebEvent.ReceivedWebEventHandler(seePicture_WebEventReceived);

        }

        void seePicture_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            if (pic != null)
            {
                responder.Respond(pic);

            }
            else
                responder.Respond("Take picture first");
        }

        void takePicture_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            responder.Respond("Taking picture");
            camera.TakePicture();
        }

        void hello_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            Debug.Print("Hello world");
            responder.Respond("Hello world");
        }
    }
}


1 Like

Thank you @ RoSchmi
I used your code but still got the same error, and the image is not loaded.
I added all the file of webserver43, but the file with the bug was already correct (indeed I have problem not creating a webserver (that works) but when I want to see the picture taken).
I also added the whole project webserver43 but if I build it, it gives me error:
Error 1 Metadata file ‘C:\Users\Carl\Downloads\gadgeteer-43877\Main\GadgeteerCore\Gadgeteer43\bin\Debug\Gadgeteer.dll’ could not be found C:\Users\Carl\Downloads\gadgeteer-43877\Main\GadgeteerCore\Libraries\Core\WebServer43\CSC

Did you try to set breakpoints to see how far you get without exceptions?

Yes it gets error after executing line 104 of your code, then ends the function and a tab opens saying dispatcher.cs was not found (?)

As I understand your answer, that if you enter http://192.168.xxx.xxx/hello in the browser you get the answer hello world.
If this works, continue to set breakpoints in the responder Class.

@ RoSchmi - I put breakpoint in the responder class (responder.cs file) and it gives me no error.
I write hello->no problem
I write takePicture->no problem
I write seePicture->no problem, then the debug error appears
I rewrite hello->it works

@ Arcixx - I would now set breakpoints in every line of code in the responder class method SendResponse() and step through with F5 to see the command which really causes the exception.

What do you mean with : “I write seePicture->no Problem” ? Does the picture appear in your browser/http Client. Which browser are you using? Did you try to inactivate the firewall?

@ RoSchmi - so doing the line by line debug it still asks me a dispatcher.cs file, I press step out and it does another cycle, then the takepicture works (same for the seepicture).
When I write seepicture it executes untill line 13, it starts to load the image, then back to visual studio with the error and we are at line 17. I use mozilla and I have disabled firewall but still nothing.

  internal void SendResponse()
        {
            try
            {
                BinaryResponseTemplate template = this.webEvent.ComputeResponse();

                if (template.Header != null && template.Header.Length > 0)
                {
                    this.ClientSocket.Send(template.Header, 0, template.Header.Length, SocketFlags.None);

                    if (template.Content != null && template.Content.Length > 0)
                    {
                        this.ClientSocket.Send(template.Content, 0, template.Content.Length, SocketFlags.None);
                    }
                }
            }
            catch
            {
                Debug.Print("Exception sending web event response (connection was probably terminated) ");
            }
            finally
            {
                this.ClientSocket.Close();
            }
        } 

:think: that’s strange. On my side the code works and the complete picture is loaded by the browser (internet explorer).
Did you verify with FEZConfig that you have the latest firmware and the matching GHI SDK on your PC.
Perhaps now it’s time to use wireshark to watch the net traffic between Spider II and PC.

1 Like

@ RoSchmi - Yes everything updated and using netmf4.3
Maybe tomorrow or the day after I can try on a different pc.
I’m sorry but I don’t have any knowledge on wireshack, I have installed it but I don’t know what to watch.
If you can lead me we can try, by the way thank you for your patience.

@ Arcixx - I sent you a direct message, if you want you can send me a copy of your project to try it on my PC.

1 Like

@ Arcixx - your project works perfectly on my PC

1 Like

@ RoSchmi - Thank you, so also the references I put etc. are correct. I will try on a different pc. Strange thing though.

1 Like

@ Arcixx - I’ve had a couple of issues like this, spent two days troubleshooting a problem to have it disappear after a reboot. I’ve gotten to the point, I make sure I do a reboot prior to deploying in field code. Just curious if you tried that yet.

Yes, very strange.
I’m no expert on wireshark as well, but if you start it, start capture, start the Ethernet interface and write ip.addr == 192.168.100.2 in the filter Combo field and click Apply you should see the traffic of your PC and Spider II