Professional Documents
Culture Documents
NP Chapter 1 Part 1
NP Chapter 1 Part 1
An Overview
Welcome to network programming with the Microsoft Windows .NET Framework. The
.NET Framework is a platform for distributed computing that enables developers to
create powerful network-aware applications with a new level of simplicity and
interoperability. In this book, we’ll cover the details you need to know to get the most out
of your distributed development efforts as you target this new platform. This text is
intended to cover a range of network development scenarios, from writing your first
socket-based application to developing high performance n-tier Web applications.
Before we get too far into the network-specific elements, it’s important to understand the
basic principles behind the creation of the .NET Framework. This chapter will walk you
through the basics of the .NET Framework and the common language runtime (CLR),
with a particular focus on the network- related elements.
We’ll start with an overview of the CLR, the execution environment on which the .NET
Framework is based. Next we’ll explain the overall goals behind the .NET Framework
and define some key terms that will be used in this book. Then we’ll go over the
System.*-level namespaces, including a look at each of the common models that can be
used to create different types of applications such as Microsoft ASP.NET, Windows
Forms, Windows Services, and console-based applications. Code examples that
demonstrate a basic network application in the context of each application model are
included. With the groundwork in place, the remaining portion of this chapter introduces
the elements of the .NET Framework most commonly involved in network programming.
You’ll see that these pieces represent a layered, cohesive stack, as shown in Figure 1-
1. The information you get from reading this book will be applicable regardless of the
level at which you interface with the .NET Framework.
Throughout the course of this chapter, we’ll be referring to Figure 1-1 as we talk about
the different components that make up the .NET Framework and its related
components, such as the CLR.
The .NET Framework was designed with one key goal in mind: to increase the
productivity of software developers. This goal is accomplished through a number of
features that are built into the framework. Understanding these features will help you
maximize your development efforts when using the .NET Framework.
One of the critical ways that the .NET Framework makes the development environment
productive is by establishing common patterns for a particular development
task that are applied across a broad set of functionality. Error handling, for example, is
a task that developers must deal with regardless of the type of application programming
interfaces (APIs) used. The .NET Framework simplifies this task by defining one
general-purpose mechanism for handling errors so that you can learn it once and then
apply the pattern across the development of your application. Examples of patterns in
the framework include dealing with collections or groups of objects, interacting with
system resources, and handling application events.
Class-Based Model
The APIs in the .NET Framework are encapsulated in classes. A class in the
framework usually contains members such as properties and methods. Developers
can create an instance of a class in the framework and use it to perform tasks that are
required by the application. Developers can also extend classes to include additional
behaviors or combine them to define new classes. This object-oriented approach
offers a familiar model that you can quickly identify with and relate to as you explore the
functionality exposed by the framework.
Layered Architecture
The classes that make up the .NET Framework are layered, meaning that at the base
of the framework are simple types, which are built on and reused by more complex
types. The more complex types often provide the ability to perform more significant
operations. For example, the socket class provides raw access to the network. The
amount of code that you might write when using the socket class to download a file
from the Internet might be 50 lines. The HTTP classes in the framework build on top of
the socket class and make it easier to perform the same task. For example, the
example that downloads a file from the Internet could now be accomplished in 15 lines
of code. More generic URI resolution classes make it possible to download the same
file in one or two lines of code. This layered approach makes it possible to use the
classes that are most applicable to the task your application needs to accomplish.
Because one layer builds on top of the other, it also makes it possible for an
application to use the higher levels to do most of the work without blocking the ability to
access the lower levels for maximum control.
Now that we’ve covered a bit of the motivation behind the framework, let’s take a
deeper look at the classes. Classes in the .NET Framework are divided into logical
groups called namespaces. Most classes in the .NET Framework exist under a top-level
namespace known as System. The root System namespace contains the basic data
types in the framework, such as numbers, Booleans, Uniform Resource Identifiers
(URI), strings, and objects, the base type from which all other classes in the framework
derive. System also contains a broad array of second-level namespaces that represent
the core functionality in the framework, such as eventing, infrastructure, interfaces,
attributes, and exceptions.
It’s useful to understand the difference between the two so that you’ll better know when
to use classes from each group as you build your application.
General-purpose class libraries can be useful in almost any context. For example,
System.String represents an immutable fixed-length string of Unicode characters. String
manipulation is useful in a Web-based application that returns HTML content to the
browser, a GUI client application that runs on the end user’s computer, or a long-
running service that has no graphical representation at all.
Table 1-1 contains System namespaces along with a brief description and an indication
of whether the namespace represents a general-purpose base class library or is part of
a particular application model. Each namespace contains anywhere from 10 to more
than 100 classes, depending on the number of lower-level namespaces it contains.
ASP.NET
The steps:
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment
the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)
>_
<ToolboxItem(False)> _
Public Class Service1 ' This class exposes a Web Service method that
' takes a zip code and returns the forecast
' for that area.
Inherits System.Web.Services.WebService
' The logic for GetTodaysForecast is limited for the
' purposes of this example to check for
' zip = 11111 and return "rainy" if it matches,
' otherwise it will return "sunny".
<WebMethod()> _
Public Function GetTodaysForecast(ByVal zip As System.String) As System.String
Dim forecast As String = "sunny"
If zip = "11111" Then
forecast = "rainy"
End If
Return forecast
End Function
End Class
Build the project.
End Sub
</script>
<body style="font: 10pt verdana">
<h4>Weather Report </h4>
<form id="Form1" runat="server">
<div style="padding:15,15,15,15;background-color:Gray;width:300;border-
color:black;border-width:1;border-style:solid">
Zip Code: <br /><asp:TextBox id="ZipCode" Text="11111" runat="server"/><br />
<input type="submit" id="Add" value="Get Weather
Report" OnServerClick="Submit_Click" runat="server" />
<p />
<asp:Label id="Result" runat="server"/>
</div>
</form>
</body>
</html>
The ASP.NET weather display page provides a mechanism for entering a postal code
into a form and calling the weather service to retrieve and display the forecast, as
shown in the following code: