Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

oofki

Member Since 12 Aug 2011
Offline Last Active Aug 25 2011 08:34 PM
-----

Topics I've Started

Problem With Basic Project

14 August 2011 - 01:16 AM

I used code that was in an example and modified it some to do some tests. It's just a basic web server. Well id say 2/3 requests I sent get back a 503 error. I've attached the code, it doesn't make sense to me. I tried changing to 4.2 as well and that did not fix it:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Text;


namespace NetduinoPlusApplication1
{
    public class Program
    {
        public static OutputPort led;
        public static WebServer webServer;
        public static void Main()
        {
             led = new OutputPort(Pins.ONBOARD_LED, false);
             webServer = new WebServer();
             webServer.ListenForRequest();
        }
        public class WebServer : IDisposable
        {
            private Socket socket = null;
            public WebServer()
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(new IPEndPoint(IPAddress.Any, 80));
                socket.Listen(10);
                ListenForRequest();
            }
            public void ListenForRequest()
            {
                while (true)
                {
                    using (Socket clientSocket = socket.Accept())
                    {
                        int bytesReceived = clientSocket.Available;
                        if (bytesReceived > 0)
                        {
                            byte[] buffer = new byte[bytesReceived];
                            clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);
                            string request = new string(Encoding.UTF8.GetChars(buffer));
                            int start = request.IndexOf("cmd=");
                            string OMG = "";
                            if (start > 0)
                            {
                                    start += 4;
                                    int stop = request.IndexOf(" ", start);
                                    OMG = request.Substring(start, stop - start);
                            }     
                            switch(OMG)
                            {
                                case"START":
                                    led.Write(true);
                                    break;
                                case "STOP":
                                    led.Write(false);
                                    break;
                                default:
                                    break;
                            }
                            string header = "HTTP/1.0 200 OK\r\nContent-Type: text; charset=utf-8\r\nContent-Length: " + OMG.Length.ToString() + "\r\nConnection: close\r\n\r\n";
                            clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
                            clientSocket.Send(Encoding.UTF8.GetBytes(OMG), OMG.Length, SocketFlags.None);
                        }
                    }
                }
            }
            #region IDisposable Members
            ~WebServer()
            {
                Dispose();
            }
            public void Dispose()
            {
                if (socket != null)
                    socket.Close();
            }
            #endregion
        }
    }
}

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.