Main Site Documentation

Send data from client to web server using ENC28 Ethernet and Fez Cerberues


#1

I am running a modified web server example. Beyond sending HTML to the client once the web server IP is hit from the browser, I want the web server to be able to send HTML based on data coming from the client. For example: if the client uses http://IPAddress/message1 then the web server will send Message 1 if the client uses http://IPAddress/message2 then the webserver will send message 2. Alternatively find another way to send data from the client (browser) to the web server. Any ideas?


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Net;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.Net;
using System.Net.Sockets;
using Microsoft.SPOT.Net.NetworkInformation;


namespace GadgeteerApp_webServerENC28
{
    public partial class Program
    {

        static NetworkInterface iface = NetworkInterface.GetAllNetworkInterfaces()[0];
        static Socket server;
        Thread t;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {

            Debug.Print("Program Started");
                     //Set iface to accept ip address automatically

            t = new Thread(listen); 
            t.Start();

         iface.EnableDhcp();
 
         //Wait for confirmation that ip address has been given
         while (iface.IPAddress == "0.0.0.0")
         {
            Debug.Print("Awaiting IP Address");
            Thread.Sleep(1000);
         }
 
         Debug.Print("IP Address Granted: " + iface.IPAddress);
        }
        void listen() {
            Debug.Print("Listen..");
            server = new Socket(AddressFamily.InterNetwork,
                              SocketType.Stream, ProtocolType.Tcp);
 
         //Initialize a local endpoint to listen on the default http port 80
         IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 80);
 
         server.Bind(localEndPoint);
        
         server.Listen(1);
 

         while (true)
         {
            // Wait for a client to connect.
            Socket clientSocket = server.Accept();
            Debug.Print("Connection: " + clientSocket.RemoteEndPoint.ToString());
            // Process the client request.
            ProcessClientRequest(clientSocket);
         }
      }


      static void ProcessClientRequest(Socket m_clientSocket)
      {
         const Int32 c_microsecondsPerSecond = 1000000;
         // 'using' ensures that the client's socket gets closed.
         using (m_clientSocket)
         {
            // Wait for the client request to start to arrive.
            Byte[] buffer = new Byte[1024];
            if (m_clientSocket.Poll(5 * c_microsecondsPerSecond,
            SelectMode.SelectRead))
            {
               // If 0 bytes in buffer, 
               // then the connection has been closed,
               // reset, or terminated.
               if (m_clientSocket.Available == 0)
                  return;
               // Read the first chunk of the request 
               // (we don't actually do anything with it).
               Int32 bytesRead = m_clientSocket.Receive(buffer,
               m_clientSocket.Available, SocketFlags.None);
            
               Debug.Print(bytesRead.ToString());
               // Return a static HTML document to the client.
               String s =
               "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<html><head><title>.NET Micro Framework Web Server</title></head>"
               + "<body><bold><a href=\"http://www.ghielectronics.com/\">Learn more about the .NET Micro Framework with FEZ by clicking here</a></bold></body></html>";
               byte[] buf = System.Text.Encoding.UTF8.GetBytes(s);
               int offset = 0;
               int ret = 0;
               int len = buf.Length;
               while (len > 0)
               {
                  ret = m_clientSocket.Send(buf, offset, len,
                                      SocketFlags.None);
                  len -= ret;
                  offset += ret;
               }
               m_clientSocket.Close();
            }
         }
      }

        }
    }


#2

I would recommend to read couple of tutorials on how HTTP protocol works. That would help you a lot.

Also this example would be a great help:
https://www.ghielectronics.com/community/codeshare/entry/186


#3

check out the httpserver in you samples folder installed by the NETMF 4.3 in MyDocuments folder :slight_smile:

Cheers,