One of the POCs was to test the web-server capability and limitations. I scanned the web and came into several examples of implementing a simple HTTP web server. It seems that the basic implementation is very common:
Socket Listening method which runs on a different thread:
private void RunServer() { listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint BindingAddress = new IPEndPoint(IPAddress.Any, LISTEN_PORT); listenSocket.Bind(BindingAddress); listenSocket.Listen(1); IsServerRunning = true; while (true) { acceptedSocket = listenSocket.Accept(); ProcessRequest(); acceptedSocket.Close(); } }
The processing Method:
private void ProcessRequest() { acceptedSocket.Receive(RECEIVE_BUFFER); var request = new string(UTF8Encoding.UTF8.GetChars(RECEIVE_BUFFER)); var HEADER = UTF8Encoding.UTF8.GetBytes(HtmlPageHeader + "text/html" + "; charset=utf-8\r\nContent-Length: " + fileLength.ToString() + "\r\n\r\n"); acceptedSocket.Send(HEADER, 0, HEADER.Length, SocketFlags.None); while (FILE_LENGTH > sendBuffer.Length) { fileStream.Read(sendBuffer, 0, sendBuffer.Length); acceptedSocket.Send(sendBuffer, 0, sendBuffer.Length, SocketFlags.None); FILE_LENGTH -= sendBuffer.Length; } fileStream.Read(sendBuffer, 0, (int)FILE_LENGTH); acceptedSocket.Send(sendBuffer, 0, (int)FILE_LENGTH, SocketFlags.None); fileStream.Close(); }
let's assume that the ProcessRequest method, always returns the same simple html file (Text and Styles only, no images etc...)
This works okay when a Chrome browser running on a Windows 7 machine is trying to access the pages, click several times (slowly) on the refresh button works as well but:
1. Trying to access the Netduino web server from a chrome running on Android 4.0.4 result many times with Timeout
2. Constantly clicking the Refresh button (F5) of the Chrome@win7 causes an exception on the Netduino, this usually happens after 10-20 requests one after the other.
The exception is:
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
On the client side I'm getting:
Chromium's connection attempt to 10.XXX.YYY.55 was rejected.
Any ideas?
Tal