Networking


Out-of-the-box networking support in the .NET FCL enables developers to develop network-aware and network-centric distributed applications. As noted earlier, networking is a low-level distribution mechanism available in the .NET Framework. Capabilities such as .NET Remoting and ASP.NET Web services further abstract the use of sockets and provide a higher level of developer abstraction, ease-of-use, and management for developing distributed applications.

Key classes in the System.Net and System.Net.Socket namespaces include the following:

  • AuthenticationManager ” Manages the client authentication for connecting with protected Web resources

  • Authorization ” Provides a container for an authorization message for a server

  • Cookie ” Provides the capability to create and use cookies for maintaining stateful conversations with stateless HTTP servers

  • Dns ” A class for performing DNS (Domain Names Resolution) lookups

  • FileWebRequest/FileWebResponse ” Provides file system implementation WebRequest/WebResponse for using the resources with the file:// prefix

  • HttpWebRequest/HttpWebResponse ”Provides HTTP implementation of WebRequest/WebResponse for using the http:// addressed resources

  • IPAddress ” Represents an IP address

  • WebClient ” Provides an implementation of a client to communicate with resources identified by a URI

  • WebRequest/WebResponse ” Abstract classes that represent a request/response pair for communication with Web resources

  • NetworkStream ” Represents an underlying stream for networked communications, typically used in conjunction with a reader/writer pair of classes

  • Socket ” Provides an interface for generic network communications

  • TcpClient ” Provides a client for TCP (Transfer Control Protocol) communication services

  • TcpListener ” Listener/server for TCP clients

  • UdpClient ” Provides a client for UDP (User Datagram Protocol) communication services

Basic TCP Client

As the first example of using the networking classes in the .NET Framework (shown in Listing 4.15), you will use the TcpClient class to connect with a Web resource (Port 80).

Listing 4.15 Basic TCP Client
 using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; public class Yahoo2 {    public static void Main()    {       String ticker = "MSFT";       String url = "/d/quotes.csv?s="+ticker+"&f=sl1d1t1c1ohgv&e=.csv";       TcpClient sock = new TcpClient("finance.yahoo.com",80);       Stream stream = sock.GetStream();       Byte[] req = Encoding.ASCII.GetBytes("GET "+url+" HTTP/1.0\n\n");       stream.Write(req,0,req.Length);       stream.Flush();       StreamReader inp = new StreamReader(stream);       String line;       while ((line = inp.ReadLine())!=null)       {          Console.WriteLine(line);       }       inp.Close();    } } 

Running the Yahoo2 example (using TcpClient) should provide the current stock price information for MSFT with the basic HTTP headers.

 
 HTTP/1.1 200 OK Date: Thu, 04 Sep 2003 17:19:23 GMT P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="..." Cache-Control: private Connection: close Content-Type: application/octet-stream "MSFT",... 

In Listing 4.16, you will utilize the abstracted WebRequest to perform the same operation. Notice the simplicity achieved with the following application:

Listing 4.16 Using WebRequest Class
 using System; using System.Net; using System.IO; public class Yahoo {    public static void Main()    {       String ticker = "MSFT";       WebRequest req = WebRequest.Create(http://finance.yahoo.com        +"/d/quotes.csv?s="+ticker+"&f=sl1d1t1c1ohgv&e=.csv");       StreamReader inp = new StreamReader(           req.GetResponse().GetResponseStream());       String line;       while ((line = inp.ReadLine())!=null)       {          Console.WriteLine(line);       }       inp.Close();    } } 

A similar output is obtained running the modified Yahoo example. However, the HTTP headers won't be printed this time.

So far what you have created using the networking classes are networking clients. Next, you will build a networking service that can provide access to other TCP clients (Listing 4.17). You will build a basic date server, which will provide the current date information when connected with port 777. Note the usage of multithreading to provide a service that can service multiple client requests at the same time.

Listing 4.17 Creating a Multithreaded TCP Server
 using System; using System.Net; using System.Net.Sockets; using System.IO; using System.Threading; public class DateServer {    private String name;    private int port;    public static void Main()    {       DateServer ds = new DateServer("Hitesh's Date Server",777);       ds.Start();    }    public DateServer(String name, int port)    {       this.name = name;       this.port = port;    }    public void Start()    {       TcpListener tcpListener = new TcpListener(         IPAddress.Parse("127.0.0.1"),port);       tcpListener.Start();       Console.WriteLine("Date Server Started");       while (true)       {          TcpClient client = tcpListener.AcceptTcpClient();          DateThread dt = new DateThread(client,name);          Thread thread = new Thread(new ThreadStart(dt.Run));          thread.Start();       }    } } public class DateThread {    private TcpClient client;    private String name;    public DateThread(TcpClient client, String name)    {       this.client = client;       this.name = name;    }    public void Run()    {       try       {          Thread.Sleep(5000);          StreamWriter output = new StreamWriter(client.GetStream());          DateTime now = DateTime.Now;          output.WriteLine(name+":"+now.ToString());          output.Flush();          output.Close();          client.Close();       }       catch (Exception ex)       {          Console.WriteLine(ex.StackTrace);       }    } } 

To connect with your newly developed networked data service, you need to implement a basic client (Listing 4.18) that connects to the service and then echoes back the response received.

Listing 4.18 Network Client to Read Date from Date Server
 using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; public class DateClient {    public static void Main()    {       TcpClient sock = new TcpClient("localhost",777);       Stream stream = sock.GetStream();       StreamReader inp = new StreamReader(stream);       String line;       while ((line = inp.ReadLine())!=null)       {          Console.WriteLine(line);       }       sock.Close();    } } 

Next you will combine the two network application applications to implement a multithreaded quote server that, in turn , uses a WebRequest to communicate with the Yahoo HTTP-based "Get Stock Quotes" Web page (Listing 4.19).

Listing 4.19 Multithreaded Quote Server
 using System; using System.Net; using System.Net.Sockets; using System.IO; using System.Threading; public class QuoteServer {    private String name;    private int port;    public static void Main()    {       QuoteServer ds = new QuoteServer("Hitesh's Quote Server",888);       ds.Start();    }    public QuoteServer(String name, int port)    {       this.name = name;       this.port = port;    }    public void Start()    {       TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"),port);       tcpListener.Start();       Console.WriteLine("Quote Server Started");       while (true)       {          TcpClient client = tcpListener.AcceptTcpClient();          QuoteThread dt = new QuoteThread(client,name);          Thread thread = new Thread(new ThreadStart(dt.Run));          thread.Start();       }    } } public class QuoteThread {    private TcpClient client;    private String name;    public QuoteThread(TcpClient client, String name)    {       this.client = client;       this.name = name;    }    public void Run()    {       try       {          Stream stream = client.GetStream();          byte[] bytes = new Byte[256];          int inbytes = stream.Read(bytes,0,bytes.Length);          String ticker =              System.Text.Encoding.ASCII.GetString(bytes, 0, inbytes);          WebRequest req = WebRequest.Create(             "http://finance.yahoo.com/d/quotes.csv?s="             +ticker+"&f=sl1d1t1c1ohgv&e=.csv");          StreamReader inp = new StreamReader(             req.GetResponse().GetResponseStream());          String line = inp.ReadLine();          byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(line);          stream.Write(outbytes,0,outbytes.Length);          client.Close();       }       catch (Exception ex)       {          Console.WriteLine(ex.StackTrace);       }    } } 

Listing 4.20 shows a modified client for your own quote server:

Listing 4.20 Networking Client for Quote Server in Listing 4.18
 using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; public class QuoteClient {    public static void Main(String[] args)    {       TcpClient sock = new TcpClient("localhost",888);       Stream stream = sock.GetStream();       String ticker = args[0];       byte[] bytes = System.Text.Encoding.ASCII.GetBytes(ticker);       stream.Write(bytes,0,bytes.Length);       byte[] outbytes = new byte[1024];       int count = stream.Read(outbytes,0,outbytes.Length);       sock.Close();       String quote = System.Text.Encoding.ASCII.GetString(outbytes,0,count);       Console.WriteLine(quote);    } } 

SHOP TALK : WHY DEVELOP YOUR OWN QUOTE SERVER WHEN YAHOO'S WEB PAGE PROVIDES THE SAME FUNCTIONALITY?

If you are wondering why you have implemented your own quote server when Yahoo provides the same information using an HTTP Web page, you are thinking on the right track. The main reason for building your own quote server is so that you can provide additional enhanced functionality, such as data caching (for instance, you can provide a 15-minute delayed stock quote by getting a stock quote for a client only once in 15 minutes and providing cached data otherwise ). Also, a custom quote server used within the context of a broader financial services application provides not only a stock quote but an aggregate set of information with the quote client ”for example, any major activity on the stock ticker, such as upgrades or downgrades by key analysts.




Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

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