Addressing NET Micro Framework

An IP version 4 address is coded as 4-byte integer value but usually indicated in the more legible dotted decimal notation, for example, 192.168.101.100. When creating an instance of the IPEndPoint and IPAddress classes, you need to specify the address coded as a long value.
You cannot directly parse an IP address from a string with the Parse method of IPAddress as you can with the full .NET Framework; the .NET Micro Framework does not provide a Parse method for IPAddress. You can indirectly parse using the System.Net.Dns class, but first, take a look at the following code snippet, which demonstrates how you can convert a dotted decimal IP address to an IP address of the long value type.

public static long DottedDecimalToIp(byte a1, byte a2, byte a3, byte a4)
{
return (long)((ulong)a4 << 24 | (ulong)a3 << 16 | (ulong)a2 << 8 | (ulong)a1);
}

Using the preceding snippet, you can easily create an endpoint with legible decimal notation:
IPEndPoint ep = new IPEndPoint(DottedDecimalToIp(192, 168, 101, 100), 80);

As I said, there is a way to convert the IP address using the System.Net.Dns class, which allows you to use the Internet Domain Name System (DNS). The Dns class is responsible for resolving server names. Server addresses can be specified either in decimal notation or as a server name like www.microsoft.com. The GetHostEntry method returns a collection of the entries found in the DNS database. If you pass a particular address in decimal notation, you will always get one entry in the form of an IPAddress object:
IPHostEntry entry = Dns.GetHostEntry("192.168.101.100");
IPAddress ipAddr = entry.AddressList[0];
IPEndPoint ep = new IPEndPoint(ipAddr, 80);

Passing an empty string ("") to the GetHostEntry method will deliver the local IP address of the device.
The IPAddress class possesses two predefined static addresses properties: IPAddress.Loopback and IPAddress.Any IPAddress.Loopback describes the address 127.0.0.1, which is used if the client and server are on the same PC or device. IPAddress.Any is 0.0.0.0 and does not have a meaning for client applications; we will discuss that parameter in detail later, with server
applications.
For example, to connect with a server at the address 192.168.101.100 on the port 80, you need the following code:
Socket clientSocket = new Socket(Addressfamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint serverEndPoint = new IPEndPoint(DottedDecimalToIp(192, 168, 101, 100),
80); // port no
clientSocket.Connect(serverEndPoint);
You can obtain the two endpoints of the socket when a connection is available with the RemoteEndPoint and LocalEndPoint properties. Casting these into an IPEndPoint object will make the IP address available over the Address property.

You can determine the individual bytes of an IP address with the GetAddressBytes method, which supplies a byte array. With IP version 4, GetAddressBytes returns a byte array of 4 bytes.

In addition, the ToString method provides a string displaying the IP address in dotted decimal notation.
IPEndPoint remoteIPEndPoint = (IPEndPoint)communicationSocket.RemoteEndPoint;
byte[] addressBytes = remoteIPEndPoint.Address.GetAddressBytes();

No comments:

Post a Comment