public class NetworkStream : Stream
Object
MarshalByRefObject
Stream
NetworkStreamThis type implements IDisposable.
System
Networking
Implements the standard stream mechanism to read and write network data through an instance of the Socket class.
The NetworkStream class allows network data to be read and written in the same manner as the Stream class.This class supports simultaneous synchronous and asynchronous access to the network data. Random access is not supported and thus the System.Net.Sockets.NetworkStream.CanSeek property always returns
false.
The following properties and methods inherited from the Stream class are not supported and throw a NotSupportedException exception when accessed:
The System.Net.Sockets.NetworkStream.Flush method is reserved for future use but does not throw an exception.
- System.Net.Sockets.NetworkStream.Length
- System.Net.Sockets.NetworkStream.Position
- System.Net.Sockets.NetworkStream.Seek(System.Int64,System.IO.SeekOrigin)
- System.Net.Sockets.NetworkStream.SetLength(System.Int64)
System.Net.Sockets Namespace
NetworkStream Constructors
NetworkStream(System.Net.Sockets.Socket, System.IO.FileAccess, bool) Constructor
NetworkStream(System.Net.Sockets.Socket, System.IO.FileAccess) Constructor
NetworkStream(System.Net.Sockets.Socket) Constructor
NetworkStream(System.Net.Sockets.Socket, bool) Constructor
NetworkStream Methods
NetworkStream.BeginRead Method
NetworkStream.BeginWrite Method
NetworkStream.Close Method
NetworkStream.Dispose Method
NetworkStream.EndRead Method
NetworkStream.EndWrite Method
NetworkStream.Finalize Method
NetworkStream.Flush Method
NetworkStream.Read Method
NetworkStream.Seek Method
NetworkStream.SetLength Method
NetworkStream.Write Method
NetworkStream Properties
NetworkStream.CanRead Property
NetworkStream.CanSeek Property
NetworkStream.CanWrite Property
NetworkStream.DataAvailable Property
NetworkStream.Length Property
NetworkStream.Position Property
public NetworkStream(Socket socket, FileAccess access, bool ownsSocket);
Constructs and initializes a new instance of the NetworkStream class.
- socket
- An instance of the Socket class.
- access
- One of the values of the FileAccess enumeration.
- ownsSocket
true
if socket is owned by the current instance; otherwise,false
.
Exception Type Condition ArgumentNullException socket is null
.IOException The System.Net.Sockets.Socket.Blocking property of socket is false
.-or-
The System.Net.Sockets.Socket.Connected property of socket is
false
.-or-
The System.Net.Sockets.Socket.SocketType property of socket is not System.Net.Sockets.SocketType.Stream.
socket is required to be an instance of the Socket class with its System.Net.Sockets.Socket.Connected property equal totrue
, System.Net.Sockets.Socket.Blocking property equal totrue
, and SocketType equal to System.Net.Sockets.SocketType.Stream.When ownsSocket is
true
, the current instance owns socket, meaning the System.Net.Sockets.NetworkStream.Close and System.Net.Sockets.NetworkStream.Dispose(System.Boolean) methods call the System.Net.Sockets.Socket.Close method of socket.
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public NetworkStream(Socket socket, FileAccess access);
Constructs and initializes a new instance of the NetworkStream class.
- socket
- An instance of the Socket class.
- access
- One of the values of the FileAccess enumeration.
Exception Type Condition ArgumentNullException socket is null
.IOException The System.Net.Sockets.Socket.Blocking property of socket is false
.-or-
The System.Net.Sockets.Socket.Connected property of socket is
false
.-or-
The System.Net.Sockets.Socket.SocketType property of socket is not System.Net.Sockets.SocketType.Stream.
This constructor is equivalent to System.Net.Sockets.NetworkStream.NetworkStream(socket, access,false
).
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public NetworkStream(Socket socket);
Constructs and initializes a new instance of the NetworkStream class.
- socket
- An instance of the Socket class.
Exception Type Condition ArgumentNullException socket is null
.IOException The System.Net.Sockets.Socket.Blocking property of socket is false
.-or-
The System.Net.Sockets.Socket.Connected property of socket is
false
.-or-
The System.Net.Sockets.Socket.SocketType property of socket is not System.Net.Sockets.SocketType.Stream.
This constructor is equivalent to System.Net.Sockets.NetworkStream.NetworkStream(socket, System.IO.FileAccess.ReadWrite,false
).
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public NetworkStream(Socket socket, bool ownsSocket);
Constructs and initializes a new instance of the NetworkStream class.
- socket
- An instance of the Socket class.
- ownsSocket
true
if socket is owned by the current instance; otherwise,false
.
Exception Type Condition ArgumentNullException socket is null
.IOException The System.Net.Sockets.Socket.Blocking property of socket is false
.-or-
The System.Net.Sockets.Socket.Connected property of socket is
false
.-or-
The System.Net.Sockets.Socket.SocketType property of socket is not System.Net.Sockets.SocketType.Stream.
This constructor is equivalent to System.Net.Sockets.NetworkStream.NetworkStream(socket, System.IO.FileAccess.ReadWrite, ownsSocket).
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state);
Begins an asynchronous operation to read data from the current instance.
- buffer
- A Byte array to store data read from the stream.
- offset
- A Int32 containing the zero-based position in buffer at which to begin storing the data.
- size
- A Int32 containing the number of bytes to read.
- callback
- A AsyncCallback delegate, or
null
.- state
- An application-defined object, or
null
.
A IAsyncResult instance that contains information about the asynchronous operation.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException offset < 0. -or-
offset > buffer.Length.
-or-
size < 0.
-or-
size > buffer.Length - offset.
IOException An error occurred while accessing the underlying socket. [Note: Any exception thrown by the System.Net.Sockets.Socket.BeginReceive(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.AsyncCallback,System.Object) method is caught and rethrown as an
IOException
with the original exception stored in the System.Exception.InnerException property.]
ObjectDisposedException The current instance has been disposed.
To retrieve the results of the operation and release resources allocated by the System.Net.Sockets.NetworkStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method, call the System.Net.Sockets.NetworkStream.EndRead(System.IAsyncResult) method, and specify the IAsyncResult object returned by this method.[Note: The System.Net.Sockets.NetworkStream.EndRead(System.IAsyncResult) method should be called exactly once for each call to the System.Net.Sockets.NetworkStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method.]
If the callback parameter is not
null
, the method referenced by callback is invoked when the asynchronous operation completes. The IAsyncResult object returned by this method is passed as the argument to the method referenced by callback. The method referenced by callback can retrieve the results of the operation by calling the System.Net.Sockets.NetworkStream.EndRead(System.IAsyncResult) method.The state parameter can be any object that the caller wishes to have available for the duration of the asynchronous operation. This object is available via the System.IAsyncResult.AsyncState property of the object returned by this method.
[Note: This method overrides System.IO.Stream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object).
]
For an outline of an asynchronous operation, see the System.Net.Sockets.Socket.BeginAccept(System.AsyncCallback,System.Object) method. For the complete example, see the Socket class overview.
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state);
Begins an asynchronous operation to write data to the current instance.
- buffer
- A Byte array containing data to write to the stream.
- offset
- A Int32 containing the zero-based position in buffer containing the starting location of the data to write.
- size
- A Int32 containing the number of bytes to write to the stream.
- callback
- A AsyncCallback delegate, or
null
.- state
- An application-defined object, or
null
.
A IAsyncResult instance that contains information about the asynchronous operation.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException offset < 0. -or-
offset > buffer.Length.
-or-
size < 0.
-or-
size > buffer.Length - offset.
IOException An error occurred while accessing the underlying socket. [Note: Any exception thrown by the System.Net.Sockets.Socket.BeginSend(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.AsyncCallback,System.Object) method is caught and rethrown as an
IOException
with the original exception stored in the System.Exception.InnerException property.]
ObjectDisposedException The current instance has been disposed.
To release resources allocated by the System.Net.Sockets.NetworkStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method, call the System.Net.Sockets.NetworkStream.EndWrite(System.IAsyncResult) method, and specify the IAsyncResult object returned by this method.[Note: The System.Net.Sockets.NetworkStream.EndWrite(System.IAsyncResult) method should be called exactly once for each call to the System.Net.Sockets.NetworkStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method.]
If the callback parameter is not
null
, the method referenced by callback is invoked when the asynchronous operation completes. The IAsyncResult object returned by this method is passed as the argument to the method referenced by callback. The method referenced by callback can retrieve the results of the operation by calling the System.Net.Sockets.NetworkStream.EndWrite(System.IAsyncResult) method.The state parameter can be any object that the caller wishes to have available for the duration of the asynchronous operation. This object is available via the System.IAsyncResult.AsyncState property of the object returned by this method.
[Note: This method overrides System.IO.Stream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object).
]
For an outline of an asynchronous operation, see the System.Net.Sockets.Socket.BeginAccept(System.AsyncCallback,System.Object) method. For the complete example, see the Socket class overview.
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override void Close();
Closes the stream and, if owned by the current instance, the underlying socket.
This method calls System.Net.Sockets.NetworkStream.Dispose(System.Boolean)(true
), which frees both managed and unmanaged resources used by the current instance. When the underlying socket is owned by the current instance, the System.Net.Sockets.Socket.Close method of the socket is called, which frees both managed and unmanaged resources used by the socket.[Note: Ownership of a socket is specified using the NetworkStream constructor.
This method overrides System.IO.Stream.Close.
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
protected virtual void Dispose(bool disposing);
Releases the unmanaged resources used by the current instance and optionally releases the managed resources.
- disposing
- A Boolean. Specify
true
to release both managed and unmanaged resources; specifyfalse
to release only unmanaged resources.
[Note: Ownership of a socket is specified using the NetworkStream constructor.The System.Net.Sockets.NetworkStream.Close method calls this method with the disposing parameter set to
true
. The finalizer calls this method with the disposing parameter set tofalse
.]
[Behaviors: This method closes the current NetworkStream instance releasing all unmanaged resources allocated by the current instance. When the underlying socket is owned by the current instance, the System.Net.Sockets.Socket.Close method of the socket is called, which frees the managed and unmanaged resources used by the socket. When the disposing parameter is
true
, this method also releases all resources held by any other managed objects allocated by the current instance. ]
[Default: This method closes the current NetworkStream instance releasing all unmanaged resources allocated by the current instance. When the underlying socket is owned by the current instance, the System.Net.Sockets.Socket.Close method of the socket is called, which frees the managed and unmanaged resources used by the socket. ]
[Overrides: The System.Net.Sockets.Socket.Dispose(System.Boolean) method can be called multiple times by other objects. When overriding this method, do not reference objects that have been previously disposed in an earlier call. ]
[Usage: Use this method to release resources allocated by the current instance. ]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override int EndRead(IAsyncResult asyncResult);
Ends an asynchronous call to read data from the current instance.
- asyncResult
- A IAsyncResult object that holds the state information for the asynchronous operation.
A Int32 containing the number of bytes read from the stream.
Exception Type Condition ArgumentNullException asyncResult is null
.IOException An error occurred while accessing the underlying socket. [Note: This method catches all exceptions thrown by the System.Net.Sockets.Socket.EndReceive(System.IAsyncResult) method.]
ObjectDisposedException The current instance has been disposed.
This method blocks if the asynchronous operation has not completed.The System.Net.Sockets.NetworkStream.EndRead(System.IAsyncResult) method completes an asynchronous request that was started with a call to the System.Net.Sockets.NetworkStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method. The object specified for the asyncResult parameter is required to be the same object as was returned by the System.Net.Sockets.NetworkStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method call that began the request.
If the System.Net.Sockets.NetworkStream.EndRead(System.IAsyncResult) method is invoked via the AsyncCallback delegate specified to the System.Net.Sockets.NetworkStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method, the asyncResult parameter is the IAsyncResult argument passed to the delegate's method.
[Note: This method overrides System.IO.Stream.EndRead(System.IAsyncResult).
]
For an outline of an asynchronous operation, see the System.Net.Sockets.Socket.BeginAccept(System.AsyncCallback,System.Object) method. For the complete example, see the Socket class overview.
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override void EndWrite(IAsyncResult asyncResult);
Ends an asynchronous call to write data to the current instance.
- asyncResult
- A IAsyncResult object that holds the state information for the asynchronous operation.
Exception Type Condition ArgumentNullException asyncResult is null
.IOException An error occurred while accessing the underlying socket. [Note: This method catches all exceptions thrown by the System.Net.Sockets.Socket.EndSend(System.IAsyncResult) method.]
ObjectDisposedException The current instance has been disposed.
This method blocks if the asynchronous operation has not completed.The System.Net.Sockets.NetworkStream.EndWrite(System.IAsyncResult) method completes an asynchronous request that was started with a call to the System.Net.Sockets.NetworkStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method. The object specified for the asyncResult parameter is required to be the same object as was returned by the System.Net.Sockets.NetworkStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method call that began the request.
If the System.Net.Sockets.NetworkStream.EndWrite(System.IAsyncResult) method is invoked via the AsyncCallback delegate specified to the System.Net.Sockets.NetworkStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) method, the asyncResult parameter is the IAsyncResult argument passed to the delegate's method.
[Note: This method overrides System.IO.Stream.EndWrite(System.IAsyncResult).
]
For an outline of an asynchronous operation, see the System.Net.Sockets.Socket.BeginAccept(System.AsyncCallback,System.Object) method. For the complete example, see the Socket class overview.
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
~NetworkStream();
Frees unmanaged resources used by the current instance.
[Note: Application code does not call this method; it is automatically invoked during garbage collection unless finalization by the garbage collector has been disabled. For more information, see System.GC.SuppressFinalize(System.Object), and System.Object.Finalize.This method calls System.Net.Sockets.NetworkStream.Dispose(System.Boolean)(
false
), which frees unmanaged resources used by the current instance. When the underlying socket is owned by the current instance, it is closed and the managed and unmanaged resources used by the socket are freed.Ownership of a socket is specified using the NetworkStream constructor.
This method overrides System.Object.Finalize.
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override void Flush();
This method is reserved for future use.
Calling this method does not throw an exception.[Note: This method overrides System.IO.Stream.Flush.
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override int Read(byte[] buffer, int offset, int size);
Reads data from the current instance and stores it in a data buffer.
- buffer
- A Byte array to store data read from the stream.
- offset
- A Int32 containing the zero-based position in buffer at which to begin storing the data.
- size
- A Int32 containing the number of bytes to read.
A Int32 containing the number of bytes read from the stream.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException offset < 0. -or-
offset > buffer.Length.
-or-
size < 0.
-or-
size > buffer.Length - offset.
IOException An error occurred while accessing the underlying socket. [Note: This method catches all exceptions thrown by the System.Net.Sockets.Socket.Receive(System.Byte[],System.Int32,System.Net.Sockets.SocketFlags) method.]
ObjectDisposedException The current instance has been disposed.
When no incoming data is available, this method blocks and waits for data to arrive.If the remote socket was shut down gracefully (System.Net.Sockets.Socket.Shutdown(System.Net.Sockets.SocketShutdown) was called on the socket or the System.Net.Sockets.SocketOptionName.Linger option was enabled and System.Net.Sockets.Socket.Close was called on the socket) and all data was received, this method immediately returns zero.
[Note: This method overrides System.IO.Stream.Read(System.Byte[],System.Int32,System.Int32).
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override long Seek(long offset, SeekOrigin origin);
Throws a NotSupportedException.
- offset
- This parameter is not used.
- origin
- This parameter is not used.
Exception Type Condition NotSupportedException Any call to this method.
[Note: The Stream base class uses this method to set the current position in the stream. This functionality is not supported in the NetworkStream class.This method overrides System.IO.Stream.Seek(System.Int64,System.IO.SeekOrigin).
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override void SetLength(long value);
Throws a NotSupportedException.
- value
- This parameter is not used.
Exception Type Condition NotSupportedException Any call to this method.
[Note: The Stream base class uses this method to set the length of the data available on the stream. This functionality is not supported in the NetworkStream class.This method overrides System.IO.Stream.SetLength(System.Int64).
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override void Write(byte[] buffer, int offset, int size);
Writes data from a specific area of a data buffer to the current instance.
- buffer
- A Byte array containing data to write to the stream.
- offset
- A Int32 containing the zero-based position in buffer containing the starting location of the data to write.
- size
- A Int32 containing the number of bytes to write to the stream.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException offset < 0. -or-
offset > buffer.Length.
-or-
size < 0.
-or-
size > buffer.Length - offset.
IOException An error occurred while accessing the underlying socket. [Note: This method catches all exceptions thrown by the System.Net.Sockets.Socket.Send(System.Byte[],System.Int32,System.Net.Sockets.SocketFlags) method.]
ObjectDisposedException The current instance has been disposed.
When no buffer space is available within the underlying protocol, this method blocks unless the socket is in non-blocking mode.[Note: This method overrides System.IO.Stream.Write(System.Byte[],System.Int32,System.Int32).
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override bool CanRead { get; }
Gets a Boolean value indicating whether the current stream supports reading.
true
indicates that the current stream supports reading;false
. indicates that the current stream does not support reading.
This property is read-only.The value of this property is initially set by the NetworkStream constructors.
[Note: This property overrides System.IO.Stream.CanRead.
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override bool CanSeek { get; }
Returns the Boolean valuefalse
to indicate that the NetworkStream class cannot access a specific location in the data stream.
false
.
This property is read-only.[Note: This property overrides System.IO.Stream.CanSeek.
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override bool CanWrite { get; }
Gets a Boolean value indicating whether the current stream supports writing.
true
indicates that the current stream supports writing;false
indicates that the current stream does not support writing.
This property is read-only.The value of this property is set by the NetworkStream constructors.
[Note: This property overrides System.IO.Stream.CanWrite.
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public virtual bool DataAvailable { get; }
Gets a Boolean value indicating whether data is available to be read from the underlying socket buffer.
true
indicates that data is available to be read;false
indicates that there is no data available to be read.
Exception Type Condition ObjectDisposedException The current instance has been disposed.
This property is read-only.[Behaviors: As described above. ]
[Default: Accessing this property causes a call to the System.Net.Sockets.Socket.Available method of the underlying Socket instance. If the
Available
method returns a non-zero value, indicating data is available to be read, this property returnstrue
; otherwise, this property returnsfalse
. ]
[Overrides: Override this property to determine if data is available to be read in the underlying socket buffer. ]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override long Length { get; }
Throws a NotSupportedException.
Exception Type Condition NotSupportedException Any attempt to access this property.
[Note: The Stream base class implements this property to return the length of the data available on the stream. This functionality is not supported in the NetworkStream class.This property overrides System.IO.Stream.Length.
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace
public override long Position { get; set; }
Throws a NotSupportedException.
Exception Type Condition NotSupportedException Any attempt to access this property.
[Note: The Stream base class implements this property to return or set the current position in the stream. This functionality is not supported in the NetworkStream class.This property overrides System.IO.Stream.Position.
]
System.Net.Sockets.NetworkStream Class, System.Net.Sockets Namespace