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

Problem with simple Webserver

HTTP Webserver Server

Best Answer VanKurt, 09 September 2013 - 05:02 AM

Hi Anthony,

actually I solved the problem yesterday by using the following code. Now everything works 100% reliably. Yay! :D

mServerSocket.ReceiveTimeout = 500;            while (true)            {                try                {                    using (Socket clientSocket = mServerSocket.Accept())                    {                        // Try to read a request                        var buffer = new byte[1024];                        if(clientSocket.Receive(buffer, 1024, SocketFlags.None) > 0)                        {

But I think your solution might work just as well, too. The trick is to wait for the bytes to come in and not to abort prematurely. So screw those online-tutorials; they are all wrong ;)

Thank you!

Go to the full post


  • Please log in to reply
2 replies to this topic

#1 VanKurt

VanKurt

    Member

  • Members
  • PipPip
  • 19 posts

Posted 06 September 2013 - 03:13 PM

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:

 

Posted Image

 

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?



#2 Anthony Glenwright

Anthony Glenwright

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts

Posted 09 September 2013 - 04:47 AM

Try adding a while (clientSocket.Poll(500, SelectMode.SelectRead)) before your bytesReceived = clientSocket.Available?



#3 VanKurt

VanKurt

    Member

  • Members
  • PipPip
  • 19 posts

Posted 09 September 2013 - 05:02 AM   Best Answer

Hi Anthony,

actually I solved the problem yesterday by using the following code. Now everything works 100% reliably. Yay! :D

mServerSocket.ReceiveTimeout = 500;            while (true)            {                try                {                    using (Socket clientSocket = mServerSocket.Accept())                    {                        // Try to read a request                        var buffer = new byte[1024];                        if(clientSocket.Receive(buffer, 1024, SocketFlags.None) > 0)                        {

But I think your solution might work just as well, too. The trick is to wait for the bytes to come in and not to abort prematurely. So screw those online-tutorials; they are all wrong ;)

Thank you!







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.