Hello !
I am running into a strange behavior of the HttpListener class.
It’s maybe just that I am not properly undertanding the way it works.
It’s like to treat a single http request, the class would be using 2 sockets ?
For an example,
In the following code, I am reserving one socket for the W5100 class;
I am reserving 2 sockets for "other use"
There should be one left, enough to serve a single simple test page.
However, it is throwing a “No free sockets” exception when trying to load the page.
If I comment the s2 socket out, this works fine
I first thought that the browser was opening 2 tcp connections simultaneously (trying /favicon.ico or ???) but after checking with WireShark, or manualy opening a “telnet 192.168.0.10 80”, the result is the same : exception.
// Let's enable the W5100 network chip with static IPs and ReserveSocket=true
WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di9, true);
NetworkInterface.EnableStaticIP(new byte[] { 192, 168, 0, 10 },
new byte[] { 255, 255, 255, 0 },
new byte[] { 192, 168, 0, 1 },
new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA });
// Here we should have 3 sockets left
// Let's reserve 2 sockets for other uses
Socket s1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Socket s2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Here we should have one socket left
// instead of the following code, we can reserve another "s3" socket
// like s1 & s2 and it will not throw exceptions
HttpListener Listener = new HttpListener("http", 80);
Listener.Start();
HttpListenerContext ctx;
HttpListenerResponse Response;
string s= "<html><body>Hello, World !</body></html>";
byte[] b= Encoding.UTF8.GetBytes(s);
while (true) // Listen to http requests, and reply them with a simple test message
{
ctx = Listener.GetContext();
// this is throwing a "No free sockets" exception at 1st request ?!!
Debug.Print("Serving a page...");
Response = ctx.Response;
Response.ContentLength64 = b.Length;
Response.ContentType = "text/html";
Response.OutputStream.Write(b, 0, b.Length);
Response.Close();
ctx.Close();
Debug.Print(" Done with the page.");
}
What am I doing wrong or misanderstanding ?
Thank you in advance for any help !
Nicolas