IP by XML

That is great!

I must admit the IndexOutOfRangeException seems a little strange, that threw me off completely when I first read your question. I think you should have got a FormatException or something like that, but I am quite new to the NETMF my self so I am not 100% familiar with the exact semantics of all the methods yet.

but now i have the same problem with the "check_***"
there is nothing inside and xmlr.ReadElementInt() dosen’t exist ^^

do you have an idea how i can do ?

thank a lot again !

@ Dylan, there is no ReadElementInt, you need to call ReadElementString and then convert the string to an integer. You can first check that the string is not empty and then decide what logic best suites your application if it is empty.

@ taylorza - yes it was simple … sorry i’m a beginner :wink:

thank you !

It is a pleasure, no need to apologize, at some point we are all beginners!

1 Like

i have again i little problem …

I try to ue NTPtime but it dosen’t work.

public static bool NTPTime(string TimeServer, int GmtOffset)
        {
            Socket s = null;
            try
            {
                EndPoint rep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123);  //     this line dosen't work and it goes to the catch ...
                s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                byte[] ntpData = new byte[48];
                Array.Clear(ntpData, 0, 48);
                ntpData[0] = 0x1B; // Set protocol version
                s.SendTo(ntpData, rep); // Send Request   
                if (s.Poll(30 * 1000 * 1000, SelectMode.SelectRead)) // Waiting an answer for 30s, if nothing: timeout
                {
                    s.ReceiveFrom(ntpData, ref rep); // Receive Time
                    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();
                    DateTime dateTime = new DateTime(1900, 1, 1) + TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
                    Utility.SetLocalTime(dateTime.AddMinutes(GmtOffset));
                    RealTimeClock.SetTime(DateTime.Now);
                    return true;
                }
                s.Close();
            }
            catch
            {
                try { s.Close(); }
                catch { }
            }
            return false;
        }

I don’t know why … i call this function with an internet adress “delphi.phys.univ-tours.fr
thank for looking :slight_smile:

NEW TOPIC HERE =>>>> http://www.tinyclr.com/forum/topic?id=7622

@ Dylan - You should start a new thread for new questions

@ Dylan

I know your questions was already answered, but I thought I would offer an alternative solution.
The two methods defined below would be defined inside a static class such as "NetUtils"
You would use the IPAddressToBytes static method to convert the xml address string to a byte array.

The code below would allow you to format your XML as follows:

192.168.1.1


/// <summary>
/// Converts IP address string to byte array
/// </summary>
/// <param name="ipAddress">IP address in ###.###.###.### format.</param>
/// <returns></returns>
public static byte[] IPAddressToBytes(string ipAddress)
{
	string[] ipValues = ipAddress.Split('.');

	if (ipAddress.Length > 17 || ipAddress.Length < 7 || ipValues.Length != 4)
		return null;

	byte[] ipBytes = new byte[4];
	for (int i = 0; i < 4; i++)
	{
		ipBytes[i] = IntToByte(ipValues[i]);
	}
	return ipBytes;
}

/// <summary>
/// Converts a multi-digit decimal string to a byte value.
/// </summary>
/// <param name="intString"></param>
/// <returns></returns>
public static byte IntToByte(string intString)
{
	int intValue = 0;
	try
	{
		intValue = int.Parse(intString);
	}
	catch
	{
		throw new ArgumentException("Invalid integer number.");
	}
	if (intValue > 255)
	{
		throw new ArgumentException("Invalid integer number.");
	}
	return (byte)intValue;
}