Network Programming with Sockets
The .NET class library provides a rich set of APIs for performing general network programming. Relevant classes can be found in the System.Net and System.Net.Sockets namespaces. The Socket class is the primary work horse for sending and receiving data for both connection-oriented and connection-less communication. The NetworkStream class provides the ability to read and write from a socket using an I/O stream interface. The TcpClient, TcpListener, and UdpClient classes provide more simplified interfaces which are often adequate for common tasks. For working with hostnames and IP addresses, the Dns and IPAddress classes are useful. The IPEndPoint classes combines an IPAddress and a port number to represent a destination for transmitted data.
Working with Network Addresses
This chapter will first discuss the APIs available for working with network addresses as these will be useful in most of the remaining code examples.
Hostname to IP address mapping (and vice versa) is accesses through the Dns class. This class has methods for returning lists of IP addresses given a host name or returning host names given an IP address. IPv4 and IPv6 addresses are both supported. The class also provides a method for returning host entries given a string that may be either an IP address or a host name.
Connectionless Communication with UDP
The UDP (User Datagram Protocol) provides a connectionless datagram-oriented communication system without guarenteed delivery or guarenteed recept order. It is often a faster protocol than connection-oriented protocols since there is generally less overhead and is useful for applications where an occasional missed message does not render an application unusable. The UDP protocol is supported in the .NET class library through the both the Socket class and the UdpClient class. The UDPClient class provides a simpler interface, while the Socket class supports all UDP capabilities available in the .NET class library.
The model for a UDP application involves a participant creating a Socket bound to a particular local port (and a particular local IP address if the local host is multi-homed). After the Socket is bound it can be used to send or receive chunks of data called datagrams. Both blocking and asynchronous APIs are available for both reading and writing. Write operations must specify a remote end point to receive the data. Read operations can select a particular remote end point to receive data from or can specify that data will be read from any sender. Blocking writes return once the data has been placed on a local network. Blocking reads block until data is available to return to the caller. Asynchronous reads and writes return to the caller immediately and a specified call back is called by the .NET framework when the requested operation is complete.
Connection-Oriented Communication with TCP
The TCP (Transmission Control Protocol) provides a reliable connection-oriented communication system. In this paradigm, streams of information flow between two end points. All bytes are received in the order that they were sent. The connection-oriented nature means that a connection must be established before communication can occur, and if one end point breaks the connection (e.g. closes communication, crashes, etc) the other end point will know.
The model for TCP communication involves a server process creating a socket bound to a specific port, then setting the socket to listening mode. The server can then either poll the socket to see if any connections are ready to be accepted or can enter a blocking call that only returns when a new connection has been received. The act of accepting a connection creates a new socket for communications to occur between the client and the server. This new socket is bound to a different port on the server than the listening socket, and so multiple connection can be serviced simultaneously by the server application. Typically after communications have occurred, the server will close the socket over which client server communications are occurring. When the server wishes to stop accepting new connections, it can close the original listening socket.
A TCP client application will first create a socket (usually bound to an arbitrary local port) and use the socket to connect to an end point where it expects to receive services from. If the connection is successfully established communication can occur over the socket. The client can at anytime and thereafter any server read or write operations will result in an exception being thrown.
Multicast Communication with UDP
All the communication systems discussed so far involve two specific end points. Multicast communication involves communication among a group of processes and is supported by the UDP protocol in the .NET class library. Multicast communication systems may require sophisticated coordination algorithms. This chapter will focus only on the mechanics of sending and receiving multicast data and not coordination algorithms.
The UdpClient class may be useful when there will be only one member of the multicast group per host. Often, this is not a valid assumption (particularly when there is a desire to develop or test software with both a provider and a consumer of information running on the same host).