System.Net.Sockets.NetworkStream Class

Provides the underlying stream of data for network access.

See Also: NetworkStream Members

Syntax

public class NetworkStream : System.IO.Stream

Remarks

The System.Net.Sockets.NetworkStream class provides methods for sending and receiving data over SocketType.Stream sockets in blocking mode. For more information about blocking versus nonblocking System.Net.Sockets.Sockets, see Using an Asynchronous Client Socket. You can use the System.Net.Sockets.NetworkStream class for both synchronous and asynchronous data transfer. For more information about synchronous and asynchronous communication, see Sockets.

To create a System.Net.Sockets.NetworkStream, you must provide a connected System.Net.Sockets.Socket. You can also specify what System.IO.FileAccess permission the System.Net.Sockets.NetworkStream has over the provided System.Net.Sockets.Socket. By default, closing the System.Net.Sockets.NetworkStream does not close the provided System.Net.Sockets.Socket. If you want the System.Net.Sockets.NetworkStream to have permission to close the provided System.Net.Sockets.Socket, you must specify true for the value of the ownsSocket parameter.

Use the NetworkStream.Write(Byte[], int, int) and NetworkStream.Read(Byte[], int, int) methods for simple single thread synchronous blocking I/O. If you want to process your I/O using separate threads, consider using the NetworkStream.BeginWrite(Byte[], int, int, AsyncCallback, object) and NetworkStream.EndWrite(IAsyncResult) methods, or the NetworkStream.BeginRead(Byte[], int, int, AsyncCallback, object) and NetworkStream.EndRead(IAsyncResult) methods for communication.

The System.Net.Sockets.NetworkStream does not support random access to the network data stream. The value of the NetworkStream.CanSeek property, which indicates whether the stream supports seeking, is always false; reading the NetworkStream.Position property, reading the NetworkStream.Length property, or calling the NetworkStream.Seek(long, System.IO.SeekOrigin) method will throw a NotSupportedException.

Read and write operations can be performed simultaneously on an instance of the System.Net.Sockets.NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.x.x, 1.0.5000.0, 2.0.0.0, 4.0.0.0