I’m trying to implement a Web Server and have had mixed results. I have an Arduino Ethernet Shield mounted to my FEz Panda II. I have wired SPI1 to the ICSP connector and have watched the SPI data on a logic analyzer. A variation of the code below worked at one point and then I modified as it appears below. Now neither version of the code results in the expected browser response. I’m using IE 8 and getting the error that it “can’t display the web page.” The Debug output looks like this:
***************** Debug output below *********
GET / HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, /
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Host: 192.168.0.177
Connection: Keep-Alive
************** Response **************
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Counter=33
Which seems to show I’m getting the request and sending what I think is a proper reply. I can see that the response buffer is being sent to the W5100 on the logic analyzer.
Anyone see any problems with my code?
**************** Code **************************
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.Sockets;
using GHIElectronics.NETMF.Net.NetworkInformation;
using System.Text;
namespace FPII_Web_Srvr
{
public class Program
{
static OutputPort p1 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.An0, false);
static int ctr = 0;
static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
public static void Main()
{
bool ledState = false;
Thread ProcHttp = new Thread(ProcessRequest);
ProcHttp.Start();
while (true)
{
// Sleep for 500 milliseconds
Thread.Sleep(1000);
// toggle LED state
ledState = !ledState;
led.Write(ledState);
ctr++;
if (ctr>2000) ctr=0;
}
}
static void ProcessRequest()
{
while (true)
{
const Int32 c_port = 80;
byte[] ip = { 192, 168, 0, 177 };
byte[] subnet = { 255, 255, 255, 0 };
byte[] gateway = { 192, 168, 0, 1 };
byte[] mac = { 43, 185, 44, 2, 206, 127 };
const Int32 c_microsecondsPerSecond = 1000000;
p1.Write(true);
WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10,
(Cpu.Pin)FEZ_Pin.Digital.Di7, true);
p1.Write(false);
NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
NetworkInterface.EnableStaticDns(new byte[] { 192, 168, 0, 1 });
p1.Write(true);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
p1.Write(false);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, c_port);
server.Bind(localEndPoint);
server.Listen(1);
p1.Write(true);
while (true)
{
// Wait for a client to connect.
Socket clientSocket = server.Accept();
// 'using' ensures that the client's socket gets closed.
using (clientSocket)
{
// Wait for the client request to start to arrive.
Byte[] buffer = new Byte[1024];
if (clientSocket.Poll(5 * c_microsecondsPerSecond,
SelectMode.SelectRead))
{
// If 0 bytes in buffer, then the connection has been closed,
// reset, or terminated.
if (clientSocket.Available == 0)
return;
// Read the first chunk of the request (we don't actually do
// anything with it).
Int32 bytesRead = clientSocket.Receive(buffer,
clientSocket.Available, SocketFlags.None);
Debug.Print(new String(Encoding.UTF8.GetChars(buffer)));
// Return a static HTML document to the client.
String s =
"HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
// charset=utf-8\r\n" +
"Connection: close\r\n" +
// "Refresh: 5\r\n" +
"<!DOCTYPE HTML>\r\n" +
"<html>\r\n" +
"Scott's Server Test 2<br />\r\n" +
"Counter=" + ctr.ToString() + "<br />\r\n" +
"</html>\r\n";
Debug.Print("************** Response **************");
Debug.Print(s);
Debug.Print("**************************************");
byte[] buf = Encoding.UTF8.GetBytes(s);
int offset = 0;
int ret = 0;
int len = buf.Length;
p1.Write(false);
while (len > 0)
{
ret = clientSocket.Send(buf, offset, len, SocketFlags.None);
len -= ret;
offset += ret;
}
p1.Write(true);
clientSocket.Close();
}
}
}
}
}
}
}