Setting up your hardware

Configuring the NetworkingExample Project

Code Examples

The following are some code examples that demonstrate some of the core capabilities...

A Simple Web Server listening on port 80 and responding to all requests with a Hello World page.

        public static void Main()
        {
            // 5C-86-4A-00-00-DD   This is a test MAC address courtesy of Secret Labs
            // Note: This MAC address should be Unique on your network to avoid collisions
            Networking.Adapter.Start(new byte[] { 0x5c,0x86,0x4a,0x00,0x00,0xdd });
            Networking.Adapter.OnHttpReceivedPacketEvent += 
                    new Adapter.HttpPacketReceivedEventHandler(OnHttpReceivedEvent);
            Networking.Adapter.ListenToPort(80);  // Listen on Port 80 for web servers

            // Loop to keep program alive
            while (true) Thread.Sleep(100);
        }

        static void OnHttpReceivedEvent(HttpRequest request)
        {
            var webPage = 
                 Encoding.UTF8.GetBytes("<html><body><h1>Hello World</h1></body></html>");
            var s = new System.IO.MemoryStream(webPage);  
            request.SendResponse(new HttpResponse(s));
        }
    


Local Name Resolution that is Windows, MacOS and iOS compatible is as easy as just setting the Name property. Everything else is multicast magic!

     Networking.Adapter.Name = "netduino";


Static IP address is not the default, dynamic IP (or DHCP) is the default, so to assign your IP address, Dns Server and gateway statically, just set it before you start the Networking.Adapter.

Networking.Adapter.IPAddress = new byte[] { 0xc0, 0xa8, 0x01, 0x5f };  //192.168.1.95
Networking.Adapter.DnsServer = new byte[] { 0xc0, 0xa8, 0x01, 0xfe };  // 192.168.1.254
Networking.Adapter.Gateway = new byte[] { 0xc0, 0xa8, 0x01, 0xfe };  // 192.168.1.254
Networking.Adapter.DhcpDisabled = true;

// 5C-86-4A-00-00-DD   This is a test MAC address courtesy of Secret Labs
// Note: This MAC address should be Unique on your network to avoid collisions
Networking.Adapter.Start(new byte[] { 0x5c,0x86,0x4a,0x00,0x00,0xdd });
Networking.Adapter.OnHttpReceivedPacketEvent += 
           new Adapter.HttpPacketReceivedEventHandler(OnHttpReceivedEvent);
Networking.Adapter.ListenToPort(80);  // Listen on Port 80 for web servers


Domain Name Lookup or a DNS Lookup allows you to query a domain name, like bing.com, and get the cooresponding IP address. This is basically an Synchronous call. You also have the option to run the Asynchronous version of the method and listen for the OnDNSLookupEvent. And, since it has a built-in timeout, it will never hang your program!

     // ... Start Networking as usual (not shown)

     var addressBytes = Networking.DNS.Lookup("odata.netflix.com");
     Debug.WriteLine("DNS Lookup: odata.netflix.com -> " + addressBytes.ToAddress());


UDP Example
        public static void Main()
        {
            // http://forums.netduino.com/index.php?/topic/322-experimental-drivers-for-wiznet-based-ethernet-shields/page__view__findpost__p__3170
            // 5C-86-4A-00-00-DD   This is a test MAC address from Secret Labs
            // Note: This MAC address should be Unique, but it should work fine on a local network (as long as there is only one instance running with this MAC)
            Networking.Adapter.Start(new byte[] { 0x5c, 0x86, 0x4a, 0x00, 0x00, 0xdd }, "mip", InterfaceProfile.NetduinoGo_Socket3_ENC28);

            // Listen for UDP messages sent to activated ports
            Networking.Adapter.OnUdpReceivedPacketEvent += new Adapter.UdpPacketReceivedEventHandler(Adapter_OnUdpReceivedPacketEvent);
            // Activate the NTP (date/time) port 123
            Networking.Adapter.ListenToPort(123); 

            // Create a NTP (date/time) Request Message
            var msg = new byte[48];
            msg[0] = 0x1b;

            // Let's get the UTC time from a time zerver using a UDP Message
            UDP.SendUDPMessage(msg, new byte[4] { 0x40, 0x5a, 0xb6, 0x37 }, 123, 123);  // 64.90.182.55 the address of a NIST time server

            // Loop to keep program alive
            while (true) Thread.Sleep(100);
        }

        static void Adapter_OnUdpReceivedPacketEvent(Packet packet)
        {
            if (packet.Socket.RemotePort == 123)
            {
                var transitTime = Utility.ExtractRangeFromArray(packet.Content, 40, 8);
                Debug.WriteLine("Current UTC Date/Time is " + transitTime.ToDateTime());
            }
        }


HTTP GET Request is super easy with a simple synchronous call! With integrated and transparent DNS lookup, all you have to do is put in the URL and call the send method! Includes timeout, of course.
     // ... Start Networking as usual (not shown)

     // This is a call to the OData REST web server hosted by netflix.    
     var r = new HttpRequest("http://odata.netflix.com/Catalog/Titles('BVIuO')/Synopsis/$value");
     r.Headers.Add("Accept", "*/*");  // Add custom properties to the Request Header
     var response = r.Send();

     if (response != null) Debug.Print("Response: " + response.Message);

Last edited Aug 27, 2012 at 4:35 AM by ValkyrieMT, version 22

Comments

vishaletm Feb 2, 2013 at 5:39 PM 
Please give the necessary modification to use ENC28J60 with STm32F4 discovery board.