Hi all,
I’m experimenting for the first time with the httplistener class. I’m making a little test application based on the http server example of which the full code is below. I’m running this on a Cerbuino NET.
Two questions currently:
[ul]When I navigate to the ip address in my browser there seem to be 2 requests, this is the output:
[/ul]
Context counter: 1
Request handler counter: 1
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
Context counter: 2
Request handler counter: 2
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
The thread '<No Name>' (0x6) has exited with code 0 (0x0).
The thread '<No Name>' (0x7) has exited with code 0 (0x0).
[ul]When I keep refreshing the page in the browser quickly the application crashes with this message: “An unhandled exception of type ‘System.NullReferenceException’ occurred in System.Http.dll” and it tries to break in System.Net._OutputNetworkStreamWrapper.cs. This is the output[/ul]
Found debugger!
Create TS.
Loading start at 8078af4, end 809ea80
Assembly: mscorlib (4.3.1.0) Assembly: Microsoft.SPOT.Native (4.3.1.0) Assembly: Microsoft.SPOT.Hardware (4.3.1.0)
Assembly: Microsoft.SPOT.Graphics (4.3.1.0) Assembly: Microsoft.SPOT.TinyCore (4.3.1.0)
Assembly: Microsoft.SPOT.IO (4.3.1.0) Assembly: System.IO (4.3.1.0) Assembly: Microsoft.SPOT.Hardware.Usb (4.3.1.0)
Assembly: Microsoft.SPOT.Hardware.SerialPort (4.3.1.0) Assembly: Microsoft.SPOT.Hardware.PWM (4.3.1.0)
Loading Deployment Assemblies.
Attaching deployed file.
Assembly: GHI.Hardware (4.3.6.0) Attaching deployed file.
Assembly: GatewayServer (1.0.0.0) Attaching deployed file.
Assembly: System.Http (4.3.1.0) Attaching deployed file.
Assembly: System (4.3.1.0) Attaching deployed file.
Assembly: System.Net.Security (4.3.1.0) Attaching deployed file.
Assembly: GHI.Pins (4.3.6.0) Attaching deployed file.
Assembly: Microsoft.SPOT.Net (4.3.1.0) Attaching deployed file.
Assembly: Microsoft.SPOT.Net.Security (4.3.1.0) Attaching deployed file.
Assembly: GHI.Networking (4.3.6.0) Resolving.
The debugging target runtime is loading the application assemblies and starting execution.
Ready.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\mscorlib.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.Native.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.Hardware.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.Graphics.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.TinyCore.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\System.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.Hardware.Usb.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.Hardware.SerialPort.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.Hardware.PWM.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\GHI Electronics\GHI NETMF v4.3 SDK\Libraries\le\GHI.Hardware.dll'
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.Net.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\System.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\Microsoft.SPOT.Net.Security.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\System.Net.Security.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\System.Http.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\GHI Electronics\GHI NETMF v4.3 SDK\Libraries\le\GHI.Pins.dll'
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Program Files (x86)\GHI Electronics\GHI NETMF v4.3 SDK\Libraries\le\GHI.Networking.dll'
'Microsoft.SPOT.Debugger.CorDebug.12.dll' (Managed): Loaded 'C:\Data\Cerbuino\GatewayServer\GatewayServer\bin\Debug\le\GatewayServer.exe', Symbols loaded.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Hello World!
Context counter: 1
Request handler counter: 1
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
Context counter: 2
Request handler counter: 2
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
The thread '<No Name>' (0x6) has exited with code 0 (0x0).
The thread '<No Name>' (0x7) has exited with code 0 (0x0).
Context counter: 3
Request handler counter: 3
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
Context counter: 4
Request handler counter: 4
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
Context counter: 5
Request handler counter: 5
The thread '<No Name>' (0x8) has exited with code 0 (0x0).
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
The thread '<No Name>' (0x9) has exited with code 0 (0x0).
Context counter: 6
Request handler counter: 6
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
Context counter: 7
Request handler counter: 7
The thread '<No Name>' (0xb) has exited with code 0 (0x0).
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
Context counter: 8
Request handler counter: 8
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
The thread '<No Name>' (0xc) has exited with code 0 (0x0).
Context counter: 9
Request handler counter: 9
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
The thread '<No Name>' (0xd) has exited with code 0 (0x0).
The thread '<No Name>' (0xe) has exited with code 0 (0x0).
Context counter: 10
Request handler counter: 10
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
Context counter: 11
Request handler counter: 11
The thread '<No Name>' (0xf) has exited with code 0 (0x0).
A first chance exception of type 'System.NullReferenceException' occurred in System.Http.dll
The thread '<No Name>' (0x10) has exited with code 0 (0x0).
An unhandled exception of type 'System.NullReferenceException' occurred in System.Http.dll
This is my code:
using System;
using Microsoft.SPOT;
using System.Threading;
using System.Net;
using GHI.Networking;
using GHI.Utilities;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using System.Collections;
using System.Net.Sockets;
namespace GatewayServer
{
public class Program
{
static Queue m_responseQueue = new Queue();
public static void Main()
{
Debug.Print(Resources.GetString(Resources.StringResources.String1));
// make sure the date is set on the device
if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU != 3)
{
Microsoft.SPOT.Hardware.Utility.SetLocalTime(new DateTime(2012, 8, 24));
}
EthernetENC28J60 netif = new EthernetENC28J60(SPI.SPI_module.SPI1, GHI.Pins.Generic.GetPin('A', 13), GHI.Pins.Generic.GetPin('A', 14), GHI.Pins.Generic.GetPin('B', 10));
netif.Open();
netif.EnableDhcp();
netif.EnableDynamicDns();
while (netif.IPAddress == "0.0.0.0")
{
Debug.Print("Waiting for DHCP");
Thread.Sleep(250);
}
Thread serverThread = new Thread(StartServer);
serverThread.Start();
Thread.Sleep(Timeout.Infinite);
}
static void StartServer()
{
HttpListener listener = new HttpListener("http", -1);
int contextCounter = 0;
while (true)
{
try
{
if (!listener.IsListening)
{
listener.Start();
}
HttpListenerContext context = listener.GetContext();
lock (m_responseQueue)
{
m_responseQueue.Enqueue(context);
}
contextCounter++;
Debug.Print("Context counter: " + contextCounter.ToString());
Thread th = new Thread(new ThreadStart(HandleRequestThread));
th.Start();
}
catch (InvalidOperationException)
{
listener.Stop();
Thread.Sleep(200);
}
catch (ObjectDisposedException)
{
listener.Start();
}
catch
{
Thread.Sleep(200);
}
}
}
private static int requestHandlerCounter = 0;
private static void HandleRequestThread()
{
HttpListenerContext context = null;
requestHandlerCounter++;
Debug.Print("Request handler counter: " + requestHandlerCounter.ToString());
try
{
lock (m_responseQueue)
{
context = (HttpListenerContext)m_responseQueue.Dequeue();
}
if (context != null)
{
HttpListenerRequest request = context.Request;
switch (request.HttpMethod.ToUpper())
{
case "GET": ProcessClientGetRequest(context); break;
//case "POST": ProcessClientPostRequest(context); break;
}
}
}
catch (SocketException)
{
}
finally
{
if (context != null)
{
context.Close();
}
}
}
private static void ProcessClientGetRequest(HttpListenerContext context)
{
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
response.StatusCode = (int)HttpStatusCode.OK;
// Find if file is present. If file exists - sends its context
// Start HTML document
string strResp = "<HTML><BODY>.Net Micro Framework Example HTTP Server<p>";
// Print requested verb, URL and version.. Adds information from the request.
strResp += "HTTP Method: " + request.HttpMethod + "<br> Requested URL: \"" + request.RawUrl +
"<br> HTTP Version: " + request.ProtocolVersion + "\"<p>";
// Information about the path that we access.
strResp += "Raw URL: " + request.RawUrl + "<p>";
// Closes HTML
strResp += "</BODY></HTML>";
// Sends it.
byte[] messageBody = System.Text.Encoding.UTF8.GetBytes(strResp);
response.ContentType = "text/html";
response.OutputStream.Write(messageBody, 0, messageBody.Length);
response.Close();
context.Close();
}
}
}