TCP Sockets work ONLY if cerberus is in VS debug mode

Hello,

I have been having a problem using my Cerbuino Bee with an ENC28. I can serve up webpages fine in debug mode but cannot when I am running the Cerbuino on its own. I found this discussion:
https://www.ghielectronics.com/community/forum/topic?id=16536
which seemed to start discussing my situation but there did not seem to be a solution. Has anyone else encountered this problem?

@ Bill_Wyo -

What is firmware are you using on the board?

@ Dat - Cerbuino Bee rev 1.2 with ENC28. Firmware is 4.3.6

@ Bill_Wyo - Can you post a minimal but complete code sample that reliably reproduces the issue?

@ John - I tested this and it duplicates the problem. I have tried adding delays in different places. I can ping the device.


using System;
using System.Net;
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 EnvironmentalMonitorSmallSample
{
    public partial class Program
    {
        static HttpListener listener;
        static string ip;
        static string gateway;
        static string subnet;
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            ip = "192.168.1.200";
            gateway = "192.168.1.1";
            subnet = "255.255.255.0";

            ethernetENC28.NetworkUp += ethernetENC28_NetworkUp;
            ethernetENC28.NetworkInterface.Open();
            GT.Timer timer = new GT.Timer(60000);
            timer.Tick += timer_Tick;
            timer.Start();
            Debug.Print("Program Started");
        }

        void timer_Tick(GT.Timer timer)
        {
            //Do something
        }

        void ethernetENC28_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            CreateWebThread();
        }
        public static void CreateWebThread()
        {
            Thread httpThread = new Thread((new PrefixKeeper("http")).RunServerDelegate);
            httpThread.Start();
        }
        class PrefixKeeper
        {
            private string m_Prefix;

            internal PrefixKeeper(string prefix)
            {
                m_Prefix = prefix;
            }
            internal void RunServerDelegate()
            {
                Program.RunServer(m_Prefix);
            }
        }
        internal static void RunServer(string prefix)
        {
            ethernetENC28.NetworkSettings.EnableStaticIP(ip, subnet, gateway);
            while (true)
            {
                try
                {
                    HttpListener listener = new HttpListener("http", 80);
                    listener.Start();
                    while (listener.IsListening)
                    {
                      try
                       {
                            HttpListenerContext context = null;
                            Thread.Sleep(500);
                            context = listener.GetContext();
                            if (context != null)
                            {
                                HandleRequest(context);
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.Print(ex.ToString());
                        }
                    }
                listener.Stop();
                listener.Close();
                }
               catch (Exception e)
                {

                }
            }
        }
        private static void HandleRequest(HttpListenerContext context)
        {
            HttpListenerResponse response = null;
            {
                try
                {
                    response = context.Response;
                    response.StatusCode = (int)HttpStatusCode.OK;
                    byte[] HTML;
                    HTML = System.Text.Encoding.UTF8.GetBytes(
                            "<html><body><p>Name: NetInfo<br />" +
                            "IP Address: " + ip + "<br />" +
                            "Gateway: " + gateway + "<br /></p>" +
                            "</body></html>");
                    response.ContentType = "text/html";
                    response.OutputStream.Write(HTML, 0, HTML.Length);
                    response.Close();
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    context.Response.Close();
                }
            }
        }
    }
} 

you are experiencing a timing situation. when not connected to debugger, your program starts faster.

you need to check for the IP address changing from 0.0.0.0 to the actual IP address. it takes a bit of time.

https://www.ghielectronics.com/docs/30/networking

@ Mike - should I check for the ip change in the networkup event?

@ John - Mike thinks I have a timing issue. I am pretty sure he is right as that is the only reason I can think of that would create the problem I am seeing. I have put delays in several places and can ping the device. I connected a characterdisplay module so I could see where the code seemed to stop so I know it gets to - context = listener.GetContext();

I usually start a thread in the ProgramStarted method, and when I see the address change, I start the service requiring network support.

@ Mike - Hey, Thank you for the help. I will continue looking at the state of the connection to see if I can slow my code down at the right point. In the meantime I have several of these systems with code working using 4.2, I will test one of these at my home to see if it works. I may take a system in to work and see if the 4.3 code works there.

@ Bill_Wyo -

Fixed in next release.

@ Dat - Thank you. I will stick with my 4.2 code in the meantime.