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
-----

Posts I've Made

In Topic: Problem With Basic Project

25 August 2011 - 08:37 PM

I'm using a static IP as well. Only thing I am left thinking is that it has something to do with the fact that we have 3 netduino's on the same LAN?
All different statically assigned IPs, all with their correct MACs set.



Whatttt!! How about using 4.2RC1? I think there was a GC bug in 4.1 from what I've read.

In Topic: Problem With Basic Project

24 August 2011 - 09:07 PM

Running this exact code on the web server side, I get no more than 2 hours out, before becoming completely unresponsive.
I'm wandering if it's a RAM issue (I have ~30KB free at any time)


Hmm haven't checked mine in days until now and it still works :) I'm running 4.2RC1 all around. The only difference I can think of is I'm using a static ip, are you?

In Topic: Problem With Basic Project

19 August 2011 - 03:17 PM

You got it! And yeah it's been running for days :)

In Topic: Network Resetting Issue

18 August 2011 - 08:44 PM

Sure. It's basically:

main() { while(true) { Thread.sleep(5000); } }

I thought it was something to do with my code so simplified and simplified until I ended up with that. If you erase the app using mfdeploy so that there's no running program it still does the reset thing.

After reading this, http://forums.netdui...in-the-network/ it looks like I'm seeing the same issue - the link going up and down repeatedly.


I'm no expert, I'm just starting to learn too, but I think it's because you only have one thread and you are blocking it for 5 seconds at a time.

Try this: main() { while(true) { int i = 0; } }

In Topic: Problem With Basic Project

17 August 2011 - 08:47 PM

oofki, do you mind posting the threaded code that is now working?


You got it!

Program.cs:
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;
using System.IO.Ports;


namespace NetduinoPlusApplication1
{
    public class Program
    {
        public static OutputPort led;
        public static void Main()
        {

            led = new OutputPort(Pins.ONBOARD_LED, false);
             Listener webServer = new Listener(RequestReceived);
             webServer.StartListening();

        }
        private static void RequestReceived(Request request)
        {

            if (request.URL.IndexOf("START") > 0)
            {

                led.Write(true);
            }
            else if (request.URL.IndexOf("STOP") > 0)
            {
                led.Write(false);
            }      
  
            request.SendResponse("<html><body><p>Request from " + request.Client.ToString() + " received at " + DateTime.Now.ToString() + "</p><p>Method: " + request.Method + "<br />URL: " + request.URL +"</p></body></html>");
        }
    }
}

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

namespace NetduinoPlusApplication1
{
    public delegate void RequestReceivedDelegate(Request request);

    public class Listener : IDisposable
    {
        const int maxRequestSize = 1024;
        readonly int portNumber = 80;

        private Socket listeningSocket = null;
        private RequestReceivedDelegate requestReceived;

        public Listener(RequestReceivedDelegate RequestReceived)
            : this(RequestReceived, 80) { }

        public Listener(RequestReceivedDelegate RequestReceived, int PortNumber)
        {
            portNumber = PortNumber;
            requestReceived = RequestReceived;
            listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listeningSocket.Bind(new IPEndPoint(IPAddress.Any, portNumber));
            listeningSocket.Listen(10);

            new Thread(StartListening).Start();

        }

        ~Listener()
        {
            Dispose();
        }

        public void StartListening()
        {

            while (true)
            {
                using (Socket clientSocket = listeningSocket.Accept())
                {
                    IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
                    Debug.Print("Received request from " + clientIP.ToString());
                    var x = clientSocket.RemoteEndPoint;

                    int availableBytes = clientSocket.Available;
                    Debug.Print(DateTime.Now.ToString() + " " + availableBytes.ToString() + " request bytes available");

                    int bytesReceived = (availableBytes > maxRequestSize ? maxRequestSize : availableBytes);
                    if (bytesReceived > 0)
                    {
                        byte[] buffer = new byte[bytesReceived]; // Buffer probably should be larger than this.
                        int readByteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);

                        using (Request r = new Request(clientSocket, Encoding.UTF8.GetChars(buffer)))
                        {
                            Debug.Print(DateTime.Now.ToString() + " " + r.URL);
                            if (requestReceived != null) requestReceived(r);

                        }
                    }
                    
                }

                // I always like to have this in a continuous loop. Helps prevent lock-ups
                Thread.Sleep(10);
            }

        }


        #region IDisposable Members

        public void Dispose()
        {
            if (listeningSocket != null) listeningSocket.Close();

        }

        #endregion
    }
}



Request.cs:
using System;
using Microsoft.SPOT;
using System.Net.Sockets;
using System.Text;
using System.Net;
using System.IO;

namespace NetduinoPlusApplication1

{
    /// <summary>
    /// Holds information about a web request
    /// </summary>
    /// <remarks>
    /// Will expand as required, but stay simple until needed.
    /// </remarks>
    public class Request : IDisposable
    {
        private string method;
        private string url;
        private Socket client;

        const int fileBufferSize = 256;

        internal Request(Socket Client, char[] Data)
        {
            client = Client;
            ProcessRequest(Data);
        }

        /// <summary>
        /// Request method
        /// </summary>
        public string Method
        {
            get { return method; }
        }

        /// <summary>
        /// URL of request
        /// </summary>
        public string URL
        {
            get { return url; }
        }

        /// <summary>
        /// Client IP address
        /// </summary>
        public IPAddress Client
        {
            get
            {
                IPEndPoint ip = client.RemoteEndPoint as IPEndPoint;
                if (ip != null) return ip.Address;
                return null;
            }
        }

        /// <summary>
        /// Send a response back to the client
        /// </summary>
        /// <param name="response"></param>
        public void SendResponse(string response, string type = "text/html")
        {
            if (client != null)
            {
                string header = "HTTP/1.0 200 OK\r\nContent-Type: " + type + "; charset=utf-8\r\nContent-Length: " + response.Length.ToString() + "\r\nConnection: close\r\n\r\n";

                client.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
                client.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None);

                Debug.Print("Response of " + response.Length.ToString() + " sent.");
            }

        }

        /// <summary>
        /// Sends a file back to the client
        /// </summary>
        /// <remarks>
        /// Assumes the application using this has checked whether it exists
        /// </remarks>
        /// <param name="filePath"></param>
        public void SendFile(string filePath)
        {
            // Map the file extension to a mime type
            string type = "";
            int dot = filePath.LastIndexOf('.');
            if (dot != 0)
                switch (filePath.Substring(dot + 1))
                {
                    case "css":
                        type = "text/css";
                        break;
                    case "xml":
                    case "xsl":
                        type = "text/xml";
                        break;
                    case "jpg":
                    case "jpeg":
                        type = "image/jpeg";
                        break;
                    case "gif":
                        type = "image/gif";
                        break;
                    // Not exhaustive. Extend this list as required.
                }


            using (FileStream inputStream = new FileStream(filePath, FileMode.Open))
            {
                // Send the header
                string header = "HTTP/1.0 200 OK\r\nContent-Type: " + type + "; charset=utf-8\r\nContent-Length: " + inputStream.Length.ToString() + "\r\nConnection: close\r\n\r\n";
                client.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);

                byte[] readBuffer = new byte[fileBufferSize];
                while (true)
                {
                    // Send the file a few bytes at a time
                    int bytesRead = inputStream.Read(readBuffer, 0, readBuffer.Length);
                    if (bytesRead == 0)
                        break;

                    client.Send(readBuffer, bytesRead, SocketFlags.None);
                    Debug.Print("Sending " + readBuffer.Length.ToString() + "bytes...");
                }
            }

            Debug.Print("Sent file " + filePath);
        }

        /// <summary>
        /// Send a Not Found response
        /// </summary>
        public void Send404()
        {
            string header = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n";
            if (client != null)
                client.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
            Debug.Print("Sent 404 Not Found");
        }
        /// <summary>
        /// Process the request header
        /// </summary>
        /// <param name="data"></param>
        private void ProcessRequest(char[] data)
        {
            string content = new string(data);
            string firstLine = content.Substring(0, content.IndexOf('\n'));

            // Parse the first line of the request: "GET /path/ HTTP/1.1"
            string[] words = firstLine.Split(' ');
            method = words[0];
            url = words[1];

            // Could look for any further headers in other lines of the request if required (e.g. User-Agent, Cookie)
        }


        #region IDisposable Members

        public void Dispose()
        {
            if (client != null)
            {
                client.Close();
                client = null;
            }
        }

        #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.