Flylib.com

Books Software

 
 
 

Network Programming for the Microsoft?® .NET Framework (Pro-Developer) - page 15


Summary

Weve now gone through an overview of the .NET Framework and the CLR. Weve talked about the motivating factors behind these technologies and the key benefits that they provide. Finally, weve discussed the elements contained within the .NET Framework that are most applicable to network development scenarios. Hopefully, by now youll agree that you can build just about any type of network application using the .NET Framework. In the chapters ahead, well be going through the specifics of exactly how to build the best distributed applications using each of the key technologies that make up network programming for the .NET Framework.



Chapter 2: Managed I/O: Streams, Readers, and Writers

Overview

The Microsoft Windows .NET Framework offers a unified way to access operating system resources such as files, memory, and network resources through the stream I/O pattern. Streams provide a common way to perform I/O operations regardless of the operating system resource. In general, streams allow you to read and write data sequentially to a container (which we call a backing store ) as if the data is a continuous sequence of bytes where the first byte that s written becomes the first byte to be read, and so on. The container is called a backing store because it will typically hold or store the data that s written to the stream. Figure 2-1 describes a stream as a sheet of paper where the paper represents a backing store that can hold letters of the alphabet. A supplier or writer can write the letters of the alphabet sequentially on the paper. A consumer or reader can eventually read the alphabetic characters from the paper in the order in which they were originally written.

click to expand
Figure 2-1: The stream I/O process

In this chapter, you ll discover how to perform I/O on files, memory, and networks using stream classes in the .NET Framework. We ll start out describing the Stream class that s inherited by several classes that allow you to perform I/O in a common way across different operating system resources. We ll focus mostly on performing I/O on files and network resources because this is a network programming book. Once we have described the core stream classes, we ll talk about other stream classes that interact with the core stream classes. Finally, we ll introduce stream readers and writers that allow you to read and write formatted text and binary data types to streams.



Stream Types

There are two types of streams available: base and composable . Base streams are streams that work directly with a backing store such as a file. Composable streams , on the other hand, are streams that operate on top of other streams. Composable streams by their nature have constructors that accept a stream as a parameter, whereas base streams do not. Therefore, a composable stream relies on the base stream you provide as input. For example, you can have a stream that handles encryption and decryption of data. If you want to write encrypted data to a file, you can write to a composable encryption stream that must call a base stream that s capable of writing the data from the encryption stream to a file. You can layer as many composable streams as you want on top of one another until you reach a base stream.

Figure 2-2 shows a stream relationship diagram that describes how base streams and composable streams interact with one another. At the top of the diagram, you ll see stream readers and writers that are essentially classes specifically designed to read or write formatted data to a stream. Stream readers and writers are described later in the chapter, and you ll find that they make stream programming easier because they allow you to work with data using a friendlier format, such as strings. It s important to note that the readers and writers can interact with either composable streams or base streams to perform I/O. The composable stream box shows that a composable stream must interact with a base stream to perform I/O on a system resource. A composable stream can also interact with another composable stream. The dashed line between the composable and base stream boxes illustrate this interaction. The base stream box shows that base streams are the only interfaces that actually interact directly with system resources.


Figure 2-2: Stream relationship diagram