15.5. The NetworkStream Class

A network stream is a stream whose backing store is the network. A network stream is the stream used by network sockets to communicate with each other.

To illustrate the use of the NetworkStream class, let's look at a simple client/server chat application in which the server listens to an open socket at a port for incoming messages from the client. The server socket provides hooks for both the incoming network stream and the outgoing network stream. The client sends messages to the server using its version of the network stream. The following code snippet shows how the server end uses NetworkStream:

 IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0]; TcpListener listener = new TcpListener(ipAddress, 1234); ...

Get .NET for Java Developers: Migrating to C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.