Exemple #1
0
        public static IPHostEntry GetHostEntry(string hostNameOrAddress)
        {
            //Do we need to try to pase this as an Address????
            string canonicalName = "";

            byte[][] addresses;

            /* special case: if the caller requests the IPHostEntry for String.Empty, return our local IP address */
            if (hostNameOrAddress == string.Empty)
            {
                addresses = new byte[][] {
                    IPAddress.Parse(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress).GetAddressBytes()
                };
            }
            else
            {
                NativeSocket.getaddrinfo(hostNameOrAddress, out canonicalName, out addresses);
            }

            int cAddresses = addresses.Length;

            IPAddress[] ipAddresses = new IPAddress[cAddresses];
            IPHostEntry ipHostEntry = new IPHostEntry();

            for (int i = 0; i < cAddresses; i++)
            {
                byte[] address = addresses[i];

                SocketAddress sockAddress = new SocketAddress(address);

                AddressFamily family;

                if (SystemInfo.IsBigEndian)
                {
                    family = (AddressFamily)((address[0] << 8) | address[1]);
                }
                else
                {
                    family = (AddressFamily)((address[1] << 8) | address[0]);
                }
                //port address[2-3]

                if (family == AddressFamily.InterNetwork)
                {
                    //This only works with IPv4 addresses

                    uint ipAddr = (uint)((address[7] << 24) | (address[6] << 16) | (address[5] << 8) | (address[4]));

                    ipAddresses[i] = new IPAddress((long)ipAddr);
                }
            }

            ipHostEntry.hostName    = canonicalName;
            ipHostEntry.addressList = ipAddresses;

            return(ipHostEntry);
        }
        public static IPHostEntry GetHostEntry(string hostNameOrAddress)
        {
            //Do we need to try to pase this as an Address????
            string canonicalName;

            byte[][] addresses;

            NativeSocket.getaddrinfo(hostNameOrAddress, out canonicalName, out addresses);

            int cAddresses = addresses.Length;

            IPAddress[] ipAddresses = new IPAddress[cAddresses];
            IPHostEntry ipHostEntry = new IPHostEntry();

            for (int i = 0; i < cAddresses; i++)
            {
                byte[] address = addresses[i];

                SocketAddress sockAddress = new SocketAddress(address);

                AddressFamily family;

                if (SystemInfo.IsBigEndian)
                {
                    family = (AddressFamily)((address[0] << 8) | address[1]);
                }
                else
                {
                    family = (AddressFamily)((address[1] << 8) | address[0]);
                }
                //port address[2-3]

                if (family == AddressFamily.InterNetwork)
                {
                    //This only works with IPv4 addresses

                    uint ipAddr = (uint)((address[7] << 24) | (address[6] << 16) | (address[5] << 8) | (address[4]));

                    ipAddresses[i] = new IPAddress((long)ipAddr);
                }
            }

            ipHostEntry.hostName    = canonicalName;
            ipHostEntry.addressList = ipAddresses;

            return(ipHostEntry);
        }