Problem making webserver with System.Http

Im attempting to develop a small web server project, and am having problems getting the program to work. Im using the System.Http objects to work around some file handling issues with the Gadgeteer.Webserver objects that were related to serving large files with limited memory space.

Im basing my code on a Web Server sample program supplied with the 4.2 version of Net Micro Framework. I am using the Cerberus, and have upgraded my board to the July 23 beta release.

The problem that an exception occurs when

 listener.Start()

is called, but other than a debugger message saying

 'System.Net.Sockets.SocketException' 

, I have no idea what the issue could be. The networking version of the .dll is installed and I seem to have an IP address. Perhaps the System.Http stuff does not want to work with the GHI dispatcher?

Heres a code snippet that shows what Im trying to do:



    public partial class Program
    {
        
                 
        // This method is run when the mainboard is powered up or reset.
        void ProgramStarted()
        {
            Debug.Print("Program started...");

            // Get SD card mounted...
            sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
            sdCard.SDCardUnmounted += new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);

            // Make sure network is ready to go...
            Debug.Print("Starting network...");

            Microsoft.SPOT.Net.NetworkInformation.NetworkInterface[] ethintf = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            foreach (Microsoft.SPOT.Net.NetworkInformation.NetworkInterface nif in ethintf)
            {
                Debug.Print("Intf: " + nif.IPAddress);
            }

            // Start http server in another thread.
            Thread httpThread = new Thread((new PrefixKeeper("http")).RunServerDelegate);
            httpThread.Start();

        Debug.Print("Program Running...");
        }


        /// All this class does is keeps the prefix and provides 
        /// RunServerDelegate to run in separate thread.
        /// RunServerDelegate calls HttpServerApp.RunServer with saved prefix.

        class PrefixKeeper
        {
            // Keeps the prefix to start server.
            private string m_prefix;

            // Saves the prefix
            // <param name="prefix">Prefix</param>
            internal PrefixKeeper(string prefix)
            {
                m_prefix = prefix;
            }


            // Delegate to run server in separate thread.

            internal void RunServerDelegate()
            {
                Program.RunServer("http");
            }
        }


        internal static void RunServer(string prefix)
        {
            HttpListener listener = new HttpListener(prefix, 80);

            try
            {
                listener.Start();
                Debug.Print("Server started...");
            }
            catch
            {
                Debug.Print("Could not start server!!!");
            }


When I start the program, heres what I get from the debugger:


Program started...
Starting network...
Intf: 192.168.1.101
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
Could not start server!!!

Any thoughts on what the issue is that leads to the SocketException?

What do you mean by “seem to have an IP address”? Can you ping the device from the outside?

How do you set up the gateway, subnet, MAC address, and DHCP/static IP address?

1 Like

Yes, I have been able to ping the board. The gateway, subnet, MAC address, and DHCP/static IP address are set using MF Deploy.

Yes, OK the only comforting thing is that I think we’ve all been there, those times when you’re sure your code just has to work but doesn’t. You post a new topic detailing what you think is one of the greatest puzzlers you have seen in a while, written in desperation and secretly wondering if you could have missed something obvious but neverthess certain that you have considered the issue from every angle because your late-night brain said it was as sharp as ever but you now suspect that maybe it was a little off. A reply comes in from some alert soul asking what you think is an inane question: “Can you ping your device?” Well, of course you can! It worked just the other day and so you fire back a reply confidently stating that surely this still works, but then you think about this just a little and resolve to check this at the next opportunity. Sitting with laptop in lap later that day you fire up a command prompt and click off the ping, only to see no response. Then you remember about how the cleaning lady was there just the other day and you had to pick up your socks, take empty coffee mug to the kitchen, sort through the back issues of Elektor that you’re done with, clean off the desk, call your sister and explain why you forgot her birthday again this year, and oh yeah unplug the ethernet cable so the cleaning lady would not run the vacuum cleaner over it again. You plug the cable in and holy flying mackeral it works, you’re not a complete idiot in the way you thought you were but just in another, more humanly forgetful way. A soothing relief flows from the top of your head to you toes, accompanied by a mild irritation at yourself.

Thanks and hats off to you Peter Mortensen, whoever and wherever you are!

2 Likes