Simplified Socket Classes


The .NET Framework offers several simplified socket-oriented classes for the TCP and UDP protocols: TcpClient , TcpListener , and UdpClient . Typically, these classes offer multiple constructors that take most of the common parameters used to set up the socket and thereby remove a few steps from the creation process when compared to using the Socket class. Each class exposes the underlying Socket object as a protected property that can be used to perform the more powerful actions that the Socket class offers, which is only useful for derived classes. Note that each of these simplified classes offers constructors that have become obsolete because they were designed with IPv4 in mind. However, they have been extended and brought up-to-date in version 1.1 of the .NET Framework to support IPv6 as well. The next sections will look at each of these three classes in more detail.

See the SimplifiedSocketClasses sample located in Chap08\SimplifiedSocketClasses for examples on how to use these three classes.

TcpClient

The TcpClient class is a simplified interface for a TCP socket that connects to a server. This connection is typically accomplished by calling the TcpClient constructor that takes the string server name and port to establish a connection to. If a host name is specified in the constructor, only the first DNS resolved name is attempted. If that connection fails, an exception is thrown. This behavior is not desirable because, for example, the DNS server might return an IPv4 address but the server might be listening on IPv6. The TcpClient class also offers a Connect method if a connection is not established as part of the constructor. This method allows a client to be more robust by performing DNS resolution itself and attempting to connect to each address returned. Because the underlying socket object is not public, TcpClient exposes a number of properties (listed in Table 8-5) that map directly to socket options but remove the necessity of the application making the call to SetSocketOption .

Table 8-5: TcpClient Properties and Equivalent Socket Options

Property

Socket Option Equivalent

LingerState

SocketOptionName.Linger

NoDelay

SocketOptionName.NoDelay

ReceiveBufferSize

SocketOptionName.ReceiveBuffer

ReceiveTimeout

SocketOptionName.ReceiveTimeout

SendBufferSize

SocketOptionName.SendBuffer

SendTimeout

SocketOptionName.SendTimeout

Once a connection is established, the TcpClient class does not expose a send or receive method. Instead, it offers a NetworkStream object that s obtained via the GetStream method. The NetworkStream class was introduced in Chapter 2. To send and receive data, the NetworkStream object exposes Read and Write methods as well as asynchronous versions.

Finally, TcpClient does not offer a method to shut down the connection, but the Close method should be called when the connection is done to release the underlying network resources. It s questionable that the TcpClient by itself greatly simplifies a TCP client because an application running on an IPv4- or IPv6-enabled network will most likely have to implement DNS resolution to connect to a server.

TcpListener

The TcpListener class is very simple, but then again, setting up a TCP listening socket isn t too difficult to begin with. An instance of TcpListener is created by specifying an IPEndPoint or an IPAddress and a port on which to listen for incoming TCP connection requests . Once created, the TCP server must be started by invoking the Start method, which actually puts the server into the listening mode.

Once TcpListener is listening for client connections, there are two methods for accepting connections: AcceptSocket and AcceptTcpClient . The former accepts a connection and returns it as a Socket object, and the latter accepts the connection and returns it as a TcpClient object. When finished with the TcpListener, the Stop method will close the underlying listening socket and network resources.

The TcpListener class is ideal for simple applications that don t expect a high volume of client connections because there s no control over the listen backlog as well as no asynchronous method for accepting a connection. Chapter 9 will cover more details of accepting client connections.

UdpClient

The UdpClient class is a simplified interface for sending and receiving UDP data over IPv4 or IPv6. It differs from the Socket class in that the binding step is removed. The UdpClient object can be constructed two different ways: by specifying the local address and port to bind to or by specifying the remote destination to connect the UDP socket to. If UdpClient is to be used as a receiver, it should not be connected.

In addition to simplifying the socket creation step, UdpClient has a simplified interface for joining multicast groups. The JoinMulticastGroup method is overloaded to allow joining both IPv4 and IPv6 multicast groups. However, when joining IPv4 groups, the method does not allow you to specify the local interface to join, unlike the IPv6 version, which also takes the interface index on which the group is joined. This functionality makes the UdpClient class unsuitable for IPv4 multicasting because on a multihomed machine, the interface on which the group is actually joined can t be determined unless the routing table is consulted.

Once the socket is set up, the Send and Receive methods can be used for sending and receiving data. If UdpClient is not connected to an endpoint, the Send method is overloaded and offers an instance that specifies the destination host name and port. Of course, this class suffers the same problem as the TcpClient class in that the first DNS resolved name is used as the destination. This name might not correspond to the address family the receiver is on.

In truth, the UdpClient doesn t save too many steps over a direct Socket implementation, but it s useful when a quick-and-dirty UDP sender or receiver is required (but multicasting is not).




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

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