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

HttpListener does not respond


  • Please log in to reply
3 replies to this topic

#1 R.Ganesh

R.Ganesh

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationSwitzerland

Posted 01 March 2012 - 06:20 PM

Hi all,

I have a web server running on an Netduino plus using the HttpListener listening for client requests and responding with a simple html. I am investigating a problem at a project installation, where the web server stops responding after few days of working (client requests are not so often, once in a day or even longer). I tried to reproduce the problem but did not succeed. However, during testing I noticed that one way to make the web server non-responsive is to send a burst of requests - i did this by keeping F5 key pressed in the internet explorer (continously refereshes). After this, the web server stops responding. First, the code (simplified):
internal static void RunServer()
        {
            HttpListener listener = new HttpListener("http", -1);
            listener.Start();

            while (true)
            {
                try
                {
                    Debug.Print("Listening..");
                    HttpListenerContext context = null;
                    context = listener.GetContext();
                    Debug.Print("Received request from " +  context.Request.UserHostAddress.ToString());
                    context.Response.ContentType = "text/html";
                    string html = "<html><p>Hello World " + DateTime.Now.ToString() + "</p></html>";
                    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                    byte[] messageBody = encoding.GetBytes(html);
                    context.Response.OutputStream.Write(messageBody, 0, messageBody.Length);

                    if (context.Response != null)
                        context.Response.Close();
                }
                catch (Exception e)
                {
                    Debug.Print("Exception occured! " + e.Message);

                    PowerState.RebootDevice(true, 1000);
                }
            }
        }
Next, the version information:

.NetMF v4.1.2821.0
NetduinoPlus, Build Date:Mar 26 2011 02:15:31
ARM Compiler version 400902
TinyCLR (Build 4.1.2821.0)

Finally, the exception (as seen on MF Deploy console)

Listening..
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (5) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::accept [IP: 0000] ####
#### System.Net.Sockets.Socket::Accept [IP: 001c] ####
#### System.Net.HttpListener::AcceptThreadFunc [IP: 0020] ####
#### SocketException ErrorCode = 10056
Received request from 4.0.0.0
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (4) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::send [IP: 0000] ####
#### System.Net.Sockets.Socket::Send [IP: 0018] ####
#### System.Net.Sockets.NetworkStream::Write [IP: 0051] ####
#### System.Net.OutputNetworkStreamWrapper::Write [IP: 0017] ####
#### System.Net.HttpListenerResponse::SendHeaders [IP: 0021] ####
#### System.Net.OutputNetworkStreamWrapper::Write [IP: 000d] ####
#### NetduinoTest.WebServer::RunServer [IP: 0074] ####
#### SocketException ErrorCode = 10056
Exception occured! Exception was thrown: System.Net.Sockets.SocketException

Any pointers on this would be really helpful to me! Thank you!

Ganesh

#2 GlenS

GlenS

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationTamworth, UK

Posted 01 March 2012 - 11:30 PM

Ganesh, I haven't had the time to do any in-depth stdy, but what I think you may be seeing is the result of HTTP requests overlapping. Some quick testing I did was everythign seemed to work if I was slow in hitting the F5 refresh in Firefox, but if I hit it very quickly I hit the error you describe very quickly. Take a look at the code from Jasper Schuurmans at : http://www.schuurman...lus#comment-256 He has an excelent multi-threaded web server that you could adapt to your needs. I have used it in one of my demo projects without any issues once I had fixed a small issue he had in the code that did not return a complete HTTP header in the response. This was causing the response to fail on some mobile devices. Hope this helps Glen

#3 R.Ganesh

R.Ganesh

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationSwitzerland

Posted 13 April 2012 - 03:43 PM

Hi Glen, I tried using the multi-threaded variant, but no luck.. seems like a low level socket exception brings down the connectivity and is not recovered until a reset is done.. Cheers! Ganesh

#4 OZ8ET

OZ8ET

    Advanced Member

  • Members
  • PipPipPip
  • 72 posts
  • LocationHundested, Denmark

Posted 21 August 2012 - 05:08 AM

Hi
I use a socket-based server and get the same problem. It is very easy to reproduce. Just hit the F5 on the browser a couple of times and you get the Socket Exception.

With the socket-based server, it looks like it recovers after the exception - I have not tried it for a long perode of time yet.

This is what it looks like:
		public HttpSession(int Port = 80, WebRequest userRequester = null)
		{
			_userRequester = userRequester;
			_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			_socket.Bind(new IPEndPoint(IPAddress.Any, Port));
			new Thread(_Listen).Start();
		}

		private void _Listen()
		{
			_socket.Listen(10);
			while (true)
			{
				try
				{
					using (Socket client = _socket.Accept())
					{
						int _requestSize;
						byte[] _buffer;
						int _amountRead;
						Request _request;

						_requestSize = client.Available;
						_buffer = new byte[Settings.bufLen];
						_amountRead = client.Receive(_buffer, Settings.bufLen, SocketFlags.None);
						_request = new Request(client, _buffer);
						_buffer = null;
						if (_userRequester != null) _userRequester(_request); else _defaultRequester(_request);
						_request = null;
						Debug.GC(true);
					}
				}
				catch (Exception ex) { 
					Debug.Print(ex.Message);
					//PowerState.RebootDevice(false, 1000); 
				}
				Thread.Sleep(10);
			}
		}

The latest change I made was to move the statement "_socket.Listen(10);" from the sub HttpSession() to the sub _Listen().
For some unexplainable (for me!) reasoen, it made the thing recover after the exception rather than stopping the server.

This is with 4.1 - when (if!) 4.2 is relesed and when (if!) OneWire becomes available for 4.2, I look forward to see if this has been solved in 4.2.

Regards
Erik




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.