Addressing Protocols

Addressing Protocols

Once you have created a Socket you have to address (or name) one to start communication over a protocol. Addressing protocols in .NET Sockets is done through the System.Net.Endpoint class. The Endpoint class is derived from a System.Net.SocketAddress class, which is similar to a Winsock sockaddr structure because all protocol address families can be addressed from its base class through inheritance. In .NET Frameworks version 1, there is currently only one available derived address Endpoint class that will support IPv4 addressing: System.Net.IPEndPoint. Because of this, in the chapter samples we primarily use IPv4; however, we also have derived a new Endpoint class to support IPv6, called IPv6EndPoint. In future versions of the .NET Application Frameworks there will eventually be more built-in support to address other protocols, such as IPv6. If you understand the Winsock sockaddr structure, you can technically address other protocols such as IPX by creating your own derived EndPoint class.

Because IPv4 support is built-in, we will describe IPEndPoint. There are two IPEndPoint constructor functions that are defined as

public IPEndPoint(    IPAddress address,    int port ); public IPEndPoint(    long address,    int port );

The first constructor accepts an IP address as a System.Net.IPAddress class that is used to manage an IPv4 address. The other constructor takes an IP address as a numeric value. Both functions accept a port as a numeric value. The System.Net.IPAddress class provides convenient methods (similar to Winsock 1) that are used to manage and manipulate an IPv4 address. The constructor of this class is defined as

public IPAddress(    long newAddress ); 

The constructor can take an IPv4 address as a numeric value, however, the real benefit of this class is the methods available to construct an IP address, as described in Table 13-3.

Table 13-3 Convenient IP Address Methods for IPv4 Address Manipulation

Method

Description

HostToNetworkOrder

Similar to Winsock htonl

IsLoopback

Determines if an IP address is a loopback address

NetworkToHostOrder

Similar to Winsock ntohl

Parse

Converts an IP address represented as a string to an IPAddress object instance

Equals

Allow comparing two IPAddresses

ToString

Converts an IP address to the string format x.x.x.x.

The following code fragment demonstrates how to construct an IPEndPoint using the IPAddress class.

System.Net.IPAddress MyAddress =      System.Net.IPAddress.Parse("136.149.3.29"); System.Net.IPEndPoint MyEndPoint = new      System.Net.IPEndPoint(MyAddress, 5150);

At this point, you can use the MyEndPoint in the Bind, Connect, SendTo, and ReceiveFrom methods of System.Net.Sockets.Socket class to address an IPv4 socket.



Network Programming for Microsoft Windows
Network Programming for Microsoft Windows (Microsoft Professional Series)
ISBN: 0735605602
EAN: 2147483647
Year: 2001
Pages: 172
Authors: Anthony Jones

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net