9.6 Using TCP, UDP, and Sockets

The System.Net.Sockets namespace includes types that provide protocol-level support for TCP and UDP. These types are built on the underlying Socket type, which is itself directly accessible for transport-level access to the network.

Two classes provide the TCP support: TcpListener and TcpClient . TcpListener listens for incoming connections, creating Socket instances that respond to the connection request. TcpClient connects to a remote host, hiding the details of the underlying socket in a Stream -derived type that allows stream I/O over the network.

A class called UdpClient provides the UDP support. UdpClient serves as both a client and a listener, and includes multicast support, allowing individual datagrams to be sent and received as byte arrays.

Both the TCP and the UDP classes help to access the underlying network socket (represented by the Socket class). The Socket class is a thin wrapper over the native Windows sockets functionality and is the lowest level of networking accessible to managed code.

The following example is a simple implementation of the Quote of the Day (qotd) protocol, as defined by the IETF in RFC 865. It demonstrates the use of a TCP listener to accept incoming requests and the use of the lower-level Socket type to fulfill the request:

 // QOTDListener.cs  // Run QOTDListener.exe to service incoming QOTD requests using System; using System.Net; using System.Net.Sockets; using System.Text; class QOTDListener {   static string[ ] quotes =  {@"Sufficiently advanced magic is indistinguishable from technology -- Terry Pratchett",  @"Sufficiently advanced technology is indistinguishable from magic --  Arthur C Clarke" };   static void Main( ) {        // Start a TCP listener on port 17     TcpListener l = new TcpListener(17);     l.Start( );     Console.WriteLine("Waiting for clients to connect");     Console.WriteLine("Press Ctrl+C to quit...");     int numServed = 1;     while (true) {          // Block waiting for an incoming socket connect request       Socket s = l.AcceptSocket( );          // Encode alternating quotes as bytes for sending        Char[ ] carr = quotes[numServed%2].ToCharArray( );       Byte[ ] barr = Encoding.ASCII.GetBytes(carr);          // Return data to client, then clean up socket & repeat       s.Send(barr, barr.Length, 0);       s.Shutdown(SocketShutdown.Both);       s.Close( );       Console.WriteLine("{0} quotes served...", numServed++);     }   } } 

To test this example, run the listener and try connecting to port 17 on localhost using a telnet client. Under Windows 2000, this can be done from the command line by entering:

 telnet localhost 17 

Notice the use of Socket.Shutdown and Socket.Close at the end of the while loop; this is required to flush and close the socket immediately, rather than wait for the garbage collector to finalize and collect unreachable Socket objects later.



C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

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