Hello everyone,
I'm currently implementing a simple Webserver with my Netduino Plus 2. The code I use is pretty straight forward and similar to what you find in all those tutorials out there:
public void ListenerThread(){ mServerSocket.Listen(10); while (true) { using (Socket clientSocket = mServerSocket.Accept()) { // Try to read a request int bytesReceived = clientSocket.Available; if (bytesReceived > 0) { //Get request var buffer = new byte[bytesReceived]; clientSocket.Receive(buffer, bytesReceived, SocketFlags.None); string request = new string(Encoding.UTF8.GetChars(buffer)); // Compose a response ProcessRequest(clientSocket, request); clientSocket.Close(); }//end if }//end using }//end while}
Now to my problem:
When I connect to this Webserver from my PC that works fine in about 30% of the cases. In all other 70% percent my browser immediately (not waiting or anything!) shows an error message (Chrome):
...No data was received...
Or on Firefox:
....Zero sized reply...
When I hit the refresh button like mad I will finally get the website my Netduino should reply with.
So I started debugging the method shown above. In the debugger the problem becomes pretty obvious:
Calling "Available" on the TCP socket returns my 0 bytes. Thus the Netduino does not react (and does not respond with the HTML page). In the cases when it works the number of bytes is around 450, and the following Read-call returns a nice HTTP-Request sent by the browser.
So my question is: WTF are the browsers doing here? Why are they opening a TCP connection and not sending anything? Or am I handling the incomming connection the wrong way?
BTW: I'm using the 4.3 Beta Firmware from this Forum along with Visual Studio 2012.
I hope this problem isn't caused by the lwIP stack in the beta?