Netduino Plus Web Server Hello World
- Let's start by creating a new project. Name it "WebserverHelloWorld"
- Right click on project name and choose to "Add" > "Add new item"
- Add new "Class" and name is "WebServer.cs"
- Here is a very simple version of a web server. It will listen for requests and respond with "Hello World". It will also blink the on-board LED when a web request comes into the Netduino plus.
public class WebServer : IDisposable { private Socket socket = null; //open connection to onbaord led so we can blink it with every request private OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); public WebServer() { //Initialize Socket class socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Request and bind to an IP from DHCP server socket.Bind(new IPEndPoint(IPAddress.Any, 80)); //Debug print our IP address Debug.Print(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress); //Start listen for web requests socket.Listen(10); ListenForRequest(); } public void ListenForRequest() { while (true) { using (Socket clientSocket = socket.Accept()) { //Get clients IP IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint; EndPoint clientEndPoint = clientSocket.RemoteEndPoint; //int byteCount = cSocket.Available; int bytesReceived = clientSocket.Available; if (bytesReceived > 0) { //Get request byte[] buffer = new byte[bytesReceived]; int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None); string request = new string(Encoding.UTF8.GetChars(buffer)); Debug.Print(request); //Compose a response string response = "Hello World"; string header = "HTTP/1.0 200 OK\r\nContent-Type: text; charset=utf-8\r\nContent-Length: " + response.Length.ToString() + "\r\nConnection: close\r\n\r\n"; clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None); clientSocket.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None); //Blink the onboard LED led.Write(true); Thread.Sleep(150); led.Write(false); } } } } #region IDisposable Members ~WebServer() { Dispose(); } public void Dispose() { if (socket != null) socket.Close(); } #endregion }
- Now add the following code to Program.cs.
public static void Main() { Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableDhcp(); WebServer webServer = new WebServer(); webServer.ListenForRequest(); }
- Make your program run on the Netduino and not the emulator by right clicking on your project > "Properties"
- Select the ".Net Micro Framework" tab and change the following settings.
- Transport: USB
- Device: NetduinoPlus_NetduinoPlus
- Now run the program by pressing F5 or "Debug" > "Start Debugging".
- In the "Output" the program will write the Netduino plus IP address. If you don't have the "Output" window open, select "View" > "Output" to open it.
- Now open a web browser and type the Netduino plus IP in the address bar.
- You should receive back "Hello World" from the Netduino plus.
http://www.youtube.com/watch?v=2E7ruAYdfQU
Complete source code attached.