Main Site Documentation

Ethernet shield + fez domino + http client problem


#1

hi all,
i have fe domino, mp3 shield + ethernet shield connected together.
i can play mp3 files without any problem, today i started to use ethernet shield, and found the code in code library… while using the code:


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft Corporation.  All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Modified to work with GHI's Wiznet W5100 Ethernet (TCP/IP) libraries.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.NetworkInformation;

/// This program demonstrates how to use the .NET Micro Framework HTTP classes 
/// to create a simple HTTP client that retrieves pages from several different 
/// websites, including secure sites.
namespace HttpClientSample
{
    public static class MyHttpClient
    {
        /// <summary>
        /// Retrieves a page from a Web server, using a simple GET request.
        /// </summary>
        public static void ethernetMain()
        {
            
            byte[] mac = { 0x00, 0x26, 0x1C, 0x7B, 0x29,0xE8 };
            WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true); // WIZnet interface on FEZ Connect
            //Dhcp.EnableDhcp(mac);
            Dhcp.EnableDhcp(mac,"smgethernet");
            Debug.Print("Network settings:");
            Debug.Print("IP Address: " + new IPAddress(NetworkInterface.IPAddress).ToString());
            Debug.Print("Subnet Mask: " + new IPAddress(NetworkInterface.SubnetMask).ToString());
            Debug.Print("Default Getway: " + new IPAddress(NetworkInterface.GatewayAddress).ToString());
            Debug.Print("DNS Server: " + new IPAddress(NetworkInterface.DnsServer).ToString());
            // Print the HTTP data from each of the following pages.
            PrintHttpData("http://www.google.com");
            PrintHttpData("http://www.tinyclr.com/compare/");
        }

        /// <summary>
        /// Prints the HTTP Web page from the given URL and status data while 
        /// receiving the page.
        /// </summary>
        /// <param name="url">The URL of the page to print.</param>
        public static void PrintHttpData(string url)
        {
            // Create an HTTP Web request.
            HttpWebRequest request = 
                HttpWebRequest.Create(url) as HttpWebRequest;

             // Set request.KeepAlive to use a persistent connection. 
            request.KeepAlive = true;

            // Get a response from the server.
            WebResponse resp = null;

            try
            {
                resp = request.GetResponse();
            }
            catch (Exception e)
            {
                Debug.Print("Exception in HttpWebRequest.GetResponse(): " + 
                    e.ToString());
            }

            // Get the network response stream to read the page data.
            if (resp != null)
            {
                Stream respStream = resp.GetResponseStream();
                string page = null;
                byte[] byteData = new byte[2048];
                char[] charData = new char[2048];
                int bytesRead = 0;
                Decoder UTF8decoder = System.Text.Encoding.UTF8.GetDecoder();
                int totalBytes = 0;

                // allow 5 seconds for reading the stream
                respStream.ReadTimeout = 5000; 

                // If we know the content length, read exactly that amount of 
                // data; otherwise, read until there is nothing left to read.
                if (resp.ContentLength != -1)
                {
                    for (int dataRem = (int)resp.ContentLength; dataRem > 0; )
                    {
                        Thread.Sleep(500);
                        bytesRead = 
                            respStream.Read(byteData, 0, byteData.Length);
                        if (bytesRead == 0)
                        {
                            Debug.Print("Error: Received " +
                                (resp.ContentLength - dataRem) + " Out of " +
                                resp.ContentLength);
                            break;
                        }
                        dataRem -= bytesRead;

                        // Convert from bytes to chars, and add to the page 
                        // string.
                        int byteUsed, charUsed;
                        bool completed = false;
                        totalBytes += bytesRead;
                        UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0, 
                            bytesRead, true, out byteUsed, out charUsed, 
                            out completed);
                        page = new String(charData, 0, charUsed);
                        // Display the page results.
                        Debug.Print(page);

                    }

                    page = new String(
                        System.Text.Encoding.UTF8.GetChars(byteData));
                }
                else
                {
                    // Read until the end of the data is reached.
                    while (true)
                    {
                        // If the Read method times out, it throws an exception, 
                        // which is expected for Keep-Alive streams because the 
                        // connection isn't terminated.
                        try
                        {
                            Thread.Sleep(500);
                            bytesRead = 
                                respStream.Read(byteData, 0, byteData.Length);
                        }
                        catch (Exception)
                        {
                            bytesRead = 0;
                        }

                        // Zero bytes indicates the connection has been closed 
                        // by the server.
                        if (bytesRead == 0)
                        {
                            break;
                        }

                        int byteUsed, charUsed;
                        bool completed = false;
                        totalBytes += bytesRead;
                        UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0, 
                            bytesRead, true, out byteUsed, out charUsed, 
                            out completed);
                        page =  new String(charData, 0, charUsed);
                        // Display the page results.
                        Debug.Print(page);
                        
                    }
                }


                // Close the response stream.  For Keep-Alive streams, the 
                // stream will remain open and will be pushed into the unused 
                // stream list.
                resp.Close();
            }
        }
    }
}


i get the
Network settings:
IP Address: 192.168.1.36
Subnet Mask: 255.255.255.0
Default Getway: 192.168.1.1
DNS Server: 192.168.1.1

i can ping this ip, i get response…

while getting ping responses, i can see the leds blinking on the ethernet shield…

but when i want to get any url result, i get error…

Exception in HttpWebRequest.GetResponse()

where/what is the problem? :slight_smile:


#2

You need to use a valid DNS server, like 8.8.8.8


#3

untested warning: this is general network device theory not tested on a fez.

Usually your home router is your gateway. In the case below, 192.168.1.1 is showing as the default gateway. In most home networking scenarios, your home router will also work as a DNS server, by reflecting the DNS query to an external DNS server you have either configured in the router setup, or that has been applied to the router through it’s DHCP address lease from your ISP.

So usually, a configuration like you have works fine for a device on a home network, assuming your router IS actually 192.168.1.1. Can you confirm what the default gateway on your PC is, or what your actual default gateway that gets to the internet is?

Here’s where I would expect Fez also to work in that scenario, I can’t think of any reason it would not. The DNS client (Fez) makes the request to the DNS server (your default gateway), that proxies this to the upstream DNS, that then responds with a correct IP address, which then gets delivered to Fez.

Time for breakpoints and debbugging before the getresponse, see what information is visible in your HTTP web request.


#4

Try simpler code. maybe

Dns.GetHostEntry("www.ghielectonics.com")

and try a simple http cilent that uses the sockets directly?