Technically i should not even be attempting anything like this so soon with my knowledge level of .netmf
But reading gets boring after a while and i gotta try something to keep it exciting.
If anyone is bored enough to have a lookse at it and tell me if i am at least closing the connection properly. I know things could be made better, but i am more concerned about proper opening and closing connection.
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Net;
using System.Net;
using System.Collections;
using System.IO;
using System.Text;
using System.Net.Sockets;
using GHIElectronics.NETMF.FEZ;
namespace FEZ_Cobra_Console_Application2
{
public class Program
{
public static void Main()
{
HttpListenerContext context;
HttpListenerRequest request;
HttpListenerResponse response;
new Thread(LedBlinker).Start();
HttpListener myListener = new HttpListener("http", -1);
myListener.Start();
while (true)
{
try
{
Debug.Print("Waiting for Request");
context = myListener.GetContext();
request = context.Request;
response = context.Response;
// Just show some info so i know whats going on.
Debug.Print("Request From:" + request.UserHostAddress);
Debug.Print("Keep Alive:" + request.KeepAlive);
Debug.Print("URL Raw:" + request.RawUrl);
Debug.Print("Input Stream:" + request.InputStream);
Debug.Print("http Method:" + request.HttpMethod);
Debug.Print("Content Length:" + request.ContentLength64);
Debug.Print("Content Type:" + request.ContentType);
//Not sure what the heck this does yet, or if needed.
response.StatusCode = (int)HttpStatusCode.OK;
string myResponse;
myResponse = "<html xmlns='http://www.w3.org/1999/xhtml'>\r\n";
myResponse += "<head>\r\n";
myResponse += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\r\n";
myResponse += "<title>Untitled Document</title>\r\n";
myResponse += "\r\n";
myResponse += "</head>\r\n";
myResponse += "\r\n";
myResponse += "<body>\r\n";
myResponse += "<p> </p>\r\n";
myResponse += "<p> </p>\r\n";
myResponse += "<p> </p>\r\n";
myResponse += "<p align='center'><strong>Hello World</strong></p>\r\n";
myResponse += "<p align='center'> </p>\r\n";
myResponse += "</body>\r\n";
myResponse += "</html>\r\n";
// Yam the string into bytes
byte[] buffer = Encoding.UTF8.GetBytes(myResponse);
// Het the length
response.ContentLength64 = buffer.Length;
try
{
using (Stream output = response.OutputStream)
output.Write(buffer, 0, buffer.Length);
// I dont think this is necessary, because the "using" closes this?
response.Close();
}
catch (Exception)
{
Debug.Print("Crap!");
}
}
catch
{
Debug.Print("Double Crap!");
}
// Sleep for 50 milliseconds
Thread.Sleep(50);
}
}
//Just blink the onboard led so i know we are running
static void LedBlinker()
{
bool ledState = false;
Microsoft.SPOT.Hardware.OutputPort led = new Microsoft.SPOT.Hardware.OutputPort((Microsoft.SPOT.Hardware.Cpu.Pin)FEZ_Pin.Digital.LED, ledState);
while (true)
{
// Sleep for 500 milliseconds
Thread.Sleep(250);
// toggle LED state
ledState = !ledState;
led.Write(ledState);
}
}
}
}