Main Site Documentation

FEZ Domino and W5100


#1

Hi guys,

Maybe a stupid question, but I want to get the time from an ntp server on the internet. I have search this forum and dound that UDP is not supported, is that correct? at least that is what the Exception says when I try connecting through UDP. Then I thought ok, well I make a REST Wcf service that gives back the time in the form of a string. So all I have to do now is connect to this URL: http://localhost:10847/WcfNtpRestService/ and it will give me the current time. But then the next problem, every sample I tried to connect through sockets to that URL fails and I don’t know what I’m doing wrong. This is a peice of code that gives me errors:

// Get server's IP address.
            IPHostEntry hostEntry = Dns.GetHostEntry(server);

            // Create socket and connect to the server's IP address and port
            Socket socket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(new IPEndPoint(new IPAddress(new byte[]{127, 0, 0, 1}), port));
            return socket;

Can anyone tell me what I’m doing wrong?

Thanks,

Ramon


#2

You can use HttpWebRequest and enable HTTP GET method in your service:

Cheers,
Valentin


#3

Btw this thread shows how to work with UDP:

http://www.tinyclr.com/forum/12/1064/


#4

UDP is supported.

Take alook at Ethernet Shield brochure. It has some simple example on how to use TCP or USP sockets.


#5

Looks like you are trying to connect to yourself (127.0.0.1), not the remote server.


#6

@ All: Thanks very much for your replies! The thing is, most of the suggestions you gave me I tried already, but with no success. But I will try them again to see what happens.

@ Architect: Your first suggestion was the HttWebResponse. To get HttpReponse to work I have to add a reference to the “System.Http” dll. As soon as I add this dll the code compiles succesfully, but as soon as I hit run visual studio pops up this message:

Error	1	An error has occurred.  Please check your hardware		

Whatever I try, the error won’t go away, I restarted my PC, I flashed the firmware again. But as soon as I remove the reference to “System.Http” dll everything works great again! Well except ofcourse the code that uses HttWebResponse.

@ Gus, sorry I did see that UDP was supported, but VS kept telling me trough exceptions it was not, now I see I had to use "SendTo"instead of “Send” and “ReceiveFrom”


#7

I tried to get the following code to work:

public static DateTime NTPTime(String TimeServer)
        {
            // Find endpoint for timeserver
            EndPoint ep = new IPEndPoint(new IPAddress(new byte[]{207,46,232,182}), 123);

            // Connect to timeserver
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            //s.Connect(ep);
            //s.Bind(ep);

            // Make send/receive buffer
            byte[] ntpData = new byte[48];
            Array.Clear(ntpData, 0, 48);

            // Set protocol version
            ntpData[0] = 0x1B;

            // Send Request
            s.SendTo(ntpData, ep);

            // Receive Time
            s.ReceiveFrom(ntpData, ref ep);

            byte offsetTransmitTime = 40;

            ulong intpart = 0;
            ulong fractpart = 0;

            for (int i = 0; i <= 3; i++)
                intpart = (intpart << 8) | ntpData[offsetTransmitTime + i];

            for (int i = 4; i <= 7; i++)
                fractpart = (fractpart << 8) | ntpData[offsetTransmitTime + i];

            ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);

            s.Close();

            TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
            DateTime dateTime = new DateTime(1900, 1, 1);
            dateTime += timeSpan;

            TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
            DateTime networkDateTime = (dateTime + offsetAmount);

            return networkDateTime;
        }

I changed the

Dns.GetEntry()

to

new IPAddress(new byte[]{207,46,232,182}

Changed socket.Send() to socket.SendTo() etc.

But it gets stuck at:

// Receive Time
            s.ReceiveFrom(ntpData, ref ep);

Anybody any idea whats going on?

Thank you gu(y)s!


#8

Firewall blocking data?


#9

I have tried a wcf service that is running on my machine, unfortunately no success there either. To be sure I have disabled my firewall, but no go!


#10

I recommend that you simulate the NPT server on your PC. to test the communication with your FEZ Domino.
I mean make a little application on your PC that responds to the packets you are trying to send to the NTP server.

If your PC application receives the packet from FEZ Domino and FEZ Domino receives back the response. then this is a good start to change the IP to be the real NTP server.

Try this technique


socket.SendTo(bytesToSend, len, SocketFlags.None, ntpEndPoint);
                while (socket.Poll(2000000, SelectMode.SelectRead))
                {
                    if (socket.Available > 0)
                    {
                        byte[] inBuf = new byte[DnsSocket.Available];

                        EndPoint recEndPoint = null;
                        socket.ReceiveFrom(inBuf, ref recEndPoint);
                        if (!recEndPoint.Equals(ntpEndPoint))
                            continue;
}
}
}

#11

I recommend a good network hub (not switch) and network monitor on your PC and sniff the traffic :slight_smile:

And the UDP thing has been on a (no-forward-progress) to-do list for a while - the confirmation from your code of the send/recieve two step process was in line with what I thought too.


#12

Do Hubs still exist?


#13

Hi Brett, thanks for your reply, but I’m pretty sure my network isn’t the problem, I have a $300 managed 1Gbit switch and I have the test service on my PC, so it is not gonna get any further the from the Domino to the switch and than to the wcf REST service on my PC. I’m really puzzled here on why I can’t get any response from that webservice. Does anyone have a complete working sample that I can try to connect to a URL so not persay through UDP, but lets say I want to connect to a local URL and get a response from it. http://localhost:10847/WcfNtpRestService/


#14

Did you take a look at the code and recommendation I posted? I think It might help you.


#15

Hi Joe, Thanks for the code, I changed it like this:

NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
            NetworkInterface.EnableStaticDns(new byte[] { 192, 168, 1, 1 });
            Socket server = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, c_port);
            server.Bind(localEndPoint);

            string url = "http://localhost:10847/WcfNtpRestService";
            string host = "locahost";

            // Send request to the server.
            String request = "GET " + url + " HTTP/1.1\r\nHost: " + host +
                "\r\nConnection: Close\r\n\r\n";
            Byte[] bytesToSend = Encoding.UTF8.GetBytes(request);

            int len = bytesToSend.Length;
            

            IPEndPoint wcfRestNtpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10847);
            socket.Bind(wcfRestNtpEndPoint);

            socket.SendTo(bytesToSend, len, SocketFlags.None, wcfRestNtpEndPoint);
                while (socket.Poll(2000000, SelectMode.SelectRead))
                {
                    if (socket.Available > 0)
                    {
                        byte[] inBuf = new byte[socket.Available];
 
                        EndPoint recEndPoint = null;
                        socket.ReceiveFrom(inBuf, ref recEndPoint);
                        if (!recEndPoint.Equals(wcfRestNtpEndPoint))
                            continue;
                    }
                }

But when it gets to this line:

socket.SendTo(bytesToSend, len, SocketFlags.None, wcfRestNtpEndPoint);

It just stays there forever, nothing happens.


#16

            IPEndPoint wcfRestNtpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10847);
            socket.Bind(wcfRestNtpEndPoint);

First, Why are you sending to the loop back IP address?
Second, Loopback interface is not supported with W5100.

No 127.0.0.2 no localhost i accepted with our support for W5100

I remember we started with you request to use UDP, why are you using TCP?

Socket server = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);