C# Класс BytesRoad.Net.Sockets.NetworkStreamEx

Provides the stream of data for network communication.
The NetworkStreamEx class provides methods for sending and receiving data over network. NetworkStreamEx may be used for both synchronous and asynchronous data transfer. In order to create an instance of the NetworkStreamEx class, you must provide a connected BytesRoad.Net.Sockets.SocketEx. You can also specify what System.IO.FileAccess permission the NetworkStreamEx has over the provided SocketEx. By default, closing the NetworkStreamEx does not close the provided SocketEx. If you want the NetworkStreamEx to have permission to close the provided SocketEx, you must specify true for the value of the ownsSocket constructor parameter.

Use the BytesRoad.Net.Sockets.NetworkStreamEx.Write and BytesRoad.Net.Sockets.NetworkStreamEx.Read methods for simple single thread synchronous blocking I/O. If you want to process your I/O using separate threads, consider using the BytesRoad.Net.Sockets.NetworkStreamEx.BeginWrite/BytesRoad.Net.Sockets.NetworkStreamEx.EndWrite and BytesRoad.Net.Sockets.NetworkStreamEx.BeginRead/BytesRoad.Net.Sockets.NetworkStreamEx.EndRead methods for communication.

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

Наследование: Stream
Показать файл Открыть проект

Открытые методы

Метод Описание
BeginRead ( byte buffer, int offset, int size, AsyncCallback callback, object state ) : IAsyncResult

Begins an asynchronous reading from the network stream.

The BeginRead method starts an asynchronous read operation from the network stream. It returns immediately and does not wait for the asynchronous call to complete.

The BytesRoad.Net.Sockets.NetworkStreamEx.EndRead method is used to retrieve the results of the asynchronous call. It can be called any time after BeginRead; if the asynchronous call has not completed, EndRead will block until it completes.

The read operation will not completed until the number of bytes specified by size parameter is read from the stream. If the remote host shuts down the BytesRoad.Net.Sockets.SocketEx connection with the BytesRoad.Net.Sockets.SocketEx.Shutdown method, and all available data has been received, the Read method will complete and return number of bytes was read.

The NetworkStreamEx should have access right to read data from the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanRead property to check this.
BeginWrite ( byte buffer, int offset, int size, AsyncCallback callback, object state ) : IAsyncResult

Begins an asynchronous write to the network stream.

The BeginWrite method starts an asynchronous write operation. It returns immediately and does not wait for the asynchronous call to complete.

The BytesRoad.Net.Sockets.NetworkStreamEx.EndWrite method is used to retrieve the results of the asynchronous call. It can be called any time after BeginWrite; if the asynchronous call has not completed, EndWrite will block until it completes.

Write operation will not completed until all data, specified by size parameter, are sent.

The NetworkStreamEx should have access right to write data to the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite property to check this.
Close ( ) : void

Releases the resources used by the BytesRoad.Net.Sockets.NetworkStreamEx.

Internally simply calls BytesRoad.Net.Sockets.NetworkStreamEx.Dispose, which depending on the ownership of underlying BytesRoad.Net.Sockets.SocketEx will call BytesRoad.Net.Sockets.SocketEx.Close method.

EndRead ( IAsyncResult asyncResult ) : int

Ends a pending asynchronous read.

EndRead is a blocking method that completes the asynchronous read operation started in the BytesRoad.Net.Sockets.NetworkStreamEx.BeginRead method.

The read operation will not completed until the number of bytes specified by size parameter (to BeginRead method) is read from the stream. If the remote host shuts down the BytesRoad.Net.Sockets.SocketEx connection with the BytesRoad.Net.Sockets.SocketEx.Shutdown method, and all available data has been received, the Read method will complete and return number of bytes was read.

The NetworkStreamEx should have access right to read data from the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanRead property to check this.
EndWrite ( IAsyncResult asyncResult ) : void

Ends a pending asynchronous write.

EndWrite is a blocking method that completes the asynchronous write operation started in the BytesRoad.Net.Sockets.NetworkStreamEx.BeginWrite method.

EndWrite will block until all data, specified by size parameter to BeginWrite method, are sent.

The NetworkStreamEx should have access right to write data to the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite property to check this.
Flush ( ) : void

Flushes data from the stream. Has no effect when applied to BytesRoad.Net.Sockets.NetworkStreamEx.

Has no effect when applied to BytesRoad.Net.Sockets.NetworkStreamEx.

NetworkStreamEx ( SocketEx socket ) : System

Creates the network stream for the specified BytesRoad.Net.Sockets.SocketEx.

The BytesRoad.Net.Sockets.NetworkStreamEx is created with read/write access to the specified BytesRoad.Net.Sockets.SocketEx. The NetworkStreamEx does not own the underlying SocketEx, so calling the BytesRoad.Net.Sockets.NetworkStreamEx.Close method will not close the SocketEx.

NetworkStreamEx ( SocketEx socket, FileAccess access ) : System

Creates the network stream for the BytesRoad.Net.Sockets.SocketEx with the specified access rights.

When BytesRoad.Net.Sockets.NetworkStreamEx created with this constructor, it does not own the underlying BytesRoad.Net.Sockets.SocketEx, so calling the NetworkStreamEx's BytesRoad.Net.Sockets.NetworkStreamEx.Close method will not close the underlying SocketEx.

The access parameter sets the BytesRoad.Net.Sockets.NetworkStreamEx.CanRead and BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite properties of the NetworkStreamEx. If you specify System.IO.FileAccess.Write, then the NetworkStreamEx will allow writing operations only. If you specify System.IO.FileAccess.Read, then the NetworkStreamEx will allow read operations only. If you specify System.IO.FileAccess.ReadWrite, both types of operations will be allowed.

NetworkStreamEx ( SocketEx socket, FileAccess access, bool ownsSocket ) : System

Creates the network stream for the BytesRoad.Net.Sockets.SocketEx with the specified access rights and ownership.

If ownsSocket is true, the NetworkStreamEx takes ownership of the underlying BytesRoad.Net.Sockets.SocketEx, and calling the NetworkStreamEx's BytesRoad.Net.Sockets.NetworkStreamEx.Close method will also close the underlying SocketEx.

The access parameter sets the BytesRoad.Net.Sockets.NetworkStreamEx.CanRead and BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite properties of the NetworkStreamEx. If you specify System.IO.FileAccess.Write, then the NetworkStreamEx will allow writing operations only. If you specify System.IO.FileAccess.Read, then the NetworkStreamEx will allow read operations only. If you specify System.IO.FileAccess.ReadWrite, both types of operations will be allowed.

NetworkStreamEx ( SocketEx socket, bool ownsSocket ) : System

Creates the network stream for the BytesRoad.Net.Sockets.SocketEx with the specified ownership.

The BytesRoad.Net.Sockets.NetworkStreamEx is created with read/write access to the specified BytesRoad.Net.Sockets.SocketEx. If ownsSocket is true, the NetworkStreamEx takes ownership of the underlying BytesRoad.Net.Sockets.SocketEx, and calling the NetworkStreamEx's BytesRoad.Net.Sockets.NetworkStreamEx.Close method will also close the underlying SocketEx.

Read ( byte buffer, int offset, int size ) : int

Reads data from the network stream.

If no data is available for reading, the Read method will block until data is available. You can use the BytesRoad.Net.Sockets.NetworkStreamEx.DataAvailable property to determine if data is available for reading. When BytesRoad.Net.Sockets.NetworkStreamEx.DataAvailable is non-zero, retry the receive operation.

The Read method will read as much data as specified by size parameter. If the remote host shuts down the BytesRoad.Net.Sockets.SocketEx connection with the BytesRoad.Net.Sockets.SocketEx.Shutdown method, and all available data has been received, the Read method will complete and return number of bytes was read.

The NetworkStreamEx should have access right to read data from the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanRead property to check this.
Seek ( long offset, SeekOrigin origin ) : long

Changes the current position in the stream. This method always throws a System.NotSupportedException.

Always throws a System.NotSupportedException.

SetLength ( long value ) : void

Sets the length of the stream. This method always throws a System.NotSupportedException.

Always throws a System.NotSupportedException.

Write ( byte buffer, int offset, int size ) : void

Writes data to the network stream.

Write method will block until all data, specified by size parameter, are sent. The NetworkStreamEx should have access right to write data to the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite property to check this.

mDispose ( ) : void

Releases all resources used by the BytesRoad.Net.Sockets.NetworkStreamEx.

Call Dispose when you are finished using the BytesRoad.Net.Sockets.NetworkStreamEx. If the NetworkStreamEx owns the underlying BytesRoad.Net.Sockets.SocketEx the Dispose method will call BytesRoad.Net.Sockets.SocketEx.Close method to release SocketEx object used for network communications. The Dispose method leaves the NetworkStreamEx in an unusable state. After calling Dispose, you must release all references to the NetworkStreamEx so the garbage collector can reclaim the memory that the NetworkStreamEx was occupying.

Защищенные методы

Метод Описание
mDispose ( bool disposing ) : void

Releases the unmanaged resources used by the BytesRoad.Net.Sockets.NetworkStreamEx and optionally releases the managed resources.

This method is called by the public Dispose() method and the Finalize method. Dispose() invokes the protected Dispose(Boolean) method with the disposing parameter set to true. Finalize invokes Dispose with disposing set to false. When the disposing parameter is true, this method releases all resources held by any managed objects that this NetworkStreamEx references. This method invokes the Dispose() method of each referenced object. If the NetworkStreamEx owns the underlying BytesRoad.Net.Sockets.SocketEx the Dispose method will call BytesRoad.Net.Sockets.SocketEx.Close method to release SocketEx object used for network communications.

Приватные методы

Метод Описание
CheckDisposed ( ) : void
GetDisposedException ( ) : Exception
Read_End ( IAsyncResult ar ) : void
Send_End ( IAsyncResult ar ) : void
ThrowMetUnsupported ( string metName ) : void
ThrowPropUnsupported ( string propName ) : void

Описание методов

BeginRead() публичный Метод

Begins an asynchronous reading from the network stream.
The BeginRead method starts an asynchronous read operation from the network stream. It returns immediately and does not wait for the asynchronous call to complete.

The BytesRoad.Net.Sockets.NetworkStreamEx.EndRead method is used to retrieve the results of the asynchronous call. It can be called any time after BeginRead; if the asynchronous call has not completed, EndRead will block until it completes.

The read operation will not completed until the number of bytes specified by size parameter is read from the stream. If the remote host shuts down the BytesRoad.Net.Sockets.SocketEx connection with the BytesRoad.Net.Sockets.SocketEx.Shutdown method, and all available data has been received, the Read method will complete and return number of bytes was read.

The NetworkStreamEx should have access right to read data from the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanRead property to check this.
/// The object was disposed. /// /// buffer is a null reference (Nothing in Visual Basic). /// /// offset is less than 0. /// -or- /// offset is greater than the length of buffer. /// -or- /// size is less than 0. /// -or- /// size is greater than the length of buffer minus /// the value of the offset parameter. ///
public BeginRead ( byte buffer, int offset, int size, AsyncCallback callback, object state ) : IAsyncResult
buffer byte The buffer to store data to.
offset int The location in the buffer where to start storing the received data.
size int The number of bytes to read.
callback AsyncCallback /// The AsyncCallback delegate. ///
state object /// An object containing state information for this request. ///
Результат IAsyncResult

BeginWrite() публичный Метод

Begins an asynchronous write to the network stream.
The BeginWrite method starts an asynchronous write operation. It returns immediately and does not wait for the asynchronous call to complete.

The BytesRoad.Net.Sockets.NetworkStreamEx.EndWrite method is used to retrieve the results of the asynchronous call. It can be called any time after BeginWrite; if the asynchronous call has not completed, EndWrite will block until it completes.

Write operation will not completed until all data, specified by size parameter, are sent.

The NetworkStreamEx should have access right to write data to the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite property to check this.
/// The object was disposed. /// /// buffer is a null reference (Nothing in Visual Basic). /// /// offset is less than 0. /// -or- /// offset is greater than the length of buffer. /// -or- /// size is less than 0. /// -or- /// size is greater than the length of buffer minus /// the value of the offset parameter. ///
public BeginWrite ( byte buffer, int offset, int size, AsyncCallback callback, object state ) : IAsyncResult
buffer byte Data to write.
offset int The position in the data buffer from which to begin writing.
size int The number of bytes to write.
callback AsyncCallback /// The AsyncCallback delegate. ///
state object /// An object containing state information for this request. ///
Результат IAsyncResult

Close() публичный Метод

Releases the resources used by the BytesRoad.Net.Sockets.NetworkStreamEx.
Internally simply calls BytesRoad.Net.Sockets.NetworkStreamEx.Dispose, which depending on the ownership of underlying BytesRoad.Net.Sockets.SocketEx will call BytesRoad.Net.Sockets.SocketEx.Close method.
public Close ( ) : void
Результат void

EndRead() публичный Метод

Ends a pending asynchronous read.
EndRead is a blocking method that completes the asynchronous read operation started in the BytesRoad.Net.Sockets.NetworkStreamEx.BeginRead method.

The read operation will not completed until the number of bytes specified by size parameter (to BeginRead method) is read from the stream. If the remote host shuts down the BytesRoad.Net.Sockets.SocketEx connection with the BytesRoad.Net.Sockets.SocketEx.Shutdown method, and all available data has been received, the Read method will complete and return number of bytes was read.

The NetworkStreamEx should have access right to read data from the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanRead property to check this.
/// The object was disposed. /// /// asyncResult is a null reference /// (Nothing in Visual Basic). /// /// asyncResult was not returned by a call to the /// /// method. /// /// EndRead was previously called for the /// asynchronous read. /// /// An error occurred when attempting to access /// the socket which is used to complete the requested operation. ///
public EndRead ( IAsyncResult asyncResult ) : int
asyncResult IAsyncResult /// An /// IAsyncResult /// that stores state information for this asynchronous operation. ///
Результат int

EndWrite() публичный Метод

Ends a pending asynchronous write.
EndWrite is a blocking method that completes the asynchronous write operation started in the BytesRoad.Net.Sockets.NetworkStreamEx.BeginWrite method.

EndWrite will block until all data, specified by size parameter to BeginWrite method, are sent.

The NetworkStreamEx should have access right to write data to the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite property to check this.
/// The object was disposed. /// /// asyncResult is a null reference /// (Nothing in Visual Basic). /// /// asyncResult was not returned by a call to the /// /// method. /// /// EndWrite was previously called for the /// asynchronous write. /// /// An error occurred when attempting to access /// the socket which is used to complete the requested operation. ///
public EndWrite ( IAsyncResult asyncResult ) : void
asyncResult IAsyncResult /// An /// IAsyncResult /// that stores state information for this asynchronous operation. ///
Результат void

Flush() публичный Метод

Flushes data from the stream. Has no effect when applied to BytesRoad.Net.Sockets.NetworkStreamEx.
Has no effect when applied to BytesRoad.Net.Sockets.NetworkStreamEx.
public Flush ( ) : void
Результат void

NetworkStreamEx() публичный Метод

Creates the network stream for the specified BytesRoad.Net.Sockets.SocketEx.
The BytesRoad.Net.Sockets.NetworkStreamEx is created with read/write access to the specified BytesRoad.Net.Sockets.SocketEx. The NetworkStreamEx does not own the underlying SocketEx, so calling the BytesRoad.Net.Sockets.NetworkStreamEx.Close method will not close the SocketEx.
/// socket is null reference (Nothing in Visual Basic). /// /// specified socket is not connected. ///
public NetworkStreamEx ( SocketEx socket ) : System
socket SocketEx /// The /// /// that the /// /// will use to send and receive data over network. ///
Результат System

NetworkStreamEx() публичный Метод

Creates the network stream for the BytesRoad.Net.Sockets.SocketEx with the specified access rights.
When BytesRoad.Net.Sockets.NetworkStreamEx created with this constructor, it does not own the underlying BytesRoad.Net.Sockets.SocketEx, so calling the NetworkStreamEx's BytesRoad.Net.Sockets.NetworkStreamEx.Close method will not close the underlying SocketEx.

The access parameter sets the BytesRoad.Net.Sockets.NetworkStreamEx.CanRead and BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite properties of the NetworkStreamEx. If you specify System.IO.FileAccess.Write, then the NetworkStreamEx will allow writing operations only. If you specify System.IO.FileAccess.Read, then the NetworkStreamEx will allow read operations only. If you specify System.IO.FileAccess.ReadWrite, both types of operations will be allowed.

/// socket is null reference (Nothing in Visual Basic). /// /// specified socket is not connected. ///
public NetworkStreamEx ( SocketEx socket, FileAccess access ) : System
socket SocketEx /// The /// /// that the /// /// will use to send and receive data over network. ///
access FileAccess /// A bitwise combination of the /// /// values, specifying the type of access given to the /// /// over the provided . ///
Результат System

NetworkStreamEx() публичный Метод

Creates the network stream for the BytesRoad.Net.Sockets.SocketEx with the specified access rights and ownership.
If ownsSocket is true, the NetworkStreamEx takes ownership of the underlying BytesRoad.Net.Sockets.SocketEx, and calling the NetworkStreamEx's BytesRoad.Net.Sockets.NetworkStreamEx.Close method will also close the underlying SocketEx.

The access parameter sets the BytesRoad.Net.Sockets.NetworkStreamEx.CanRead and BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite properties of the NetworkStreamEx. If you specify System.IO.FileAccess.Write, then the NetworkStreamEx will allow writing operations only. If you specify System.IO.FileAccess.Read, then the NetworkStreamEx will allow read operations only. If you specify System.IO.FileAccess.ReadWrite, both types of operations will be allowed.

/// socket is null reference (Nothing in Visual Basic). /// /// specified socket is not connected. ///
public NetworkStreamEx ( SocketEx socket, FileAccess access, bool ownsSocket ) : System
socket SocketEx /// The /// /// that the /// /// will use to send and receive data over network. ///
access FileAccess /// A bitwise combination of the /// /// values, specifying the type of access given to the /// /// over the provided . ///
ownsSocket bool /// true to indicate that the /// /// will take ownership of the ; otherwise, false. ///
Результат System

NetworkStreamEx() публичный Метод

Creates the network stream for the BytesRoad.Net.Sockets.SocketEx with the specified ownership.
The BytesRoad.Net.Sockets.NetworkStreamEx is created with read/write access to the specified BytesRoad.Net.Sockets.SocketEx. If ownsSocket is true, the NetworkStreamEx takes ownership of the underlying BytesRoad.Net.Sockets.SocketEx, and calling the NetworkStreamEx's BytesRoad.Net.Sockets.NetworkStreamEx.Close method will also close the underlying SocketEx.
/// socket is null reference (Nothing in Visual Basic). /// /// specified socket is not connected. ///
public NetworkStreamEx ( SocketEx socket, bool ownsSocket ) : System
socket SocketEx /// The /// /// that the /// /// will use to send and receive data over network. ///
ownsSocket bool /// true to indicate that the /// /// will take ownership of the ; otherwise, false. ///
Результат System

Read() публичный Метод

Reads data from the network stream.
If no data is available for reading, the Read method will block until data is available. You can use the BytesRoad.Net.Sockets.NetworkStreamEx.DataAvailable property to determine if data is available for reading. When BytesRoad.Net.Sockets.NetworkStreamEx.DataAvailable is non-zero, retry the receive operation.

The Read method will read as much data as specified by size parameter. If the remote host shuts down the BytesRoad.Net.Sockets.SocketEx connection with the BytesRoad.Net.Sockets.SocketEx.Shutdown method, and all available data has been received, the Read method will complete and return number of bytes was read.

The NetworkStreamEx should have access right to read data from the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanRead property to check this.
/// The object was disposed. /// /// buffer is a null reference (Nothing in Visual Basic). /// /// offset is less than 0. /// -or- /// offset is greater than the length of buffer. /// -or- /// size is less than 0. /// -or- /// size is greater than the length of buffer minus /// the value of the offset parameter. /// /// An error occurred when attempting to access /// the socket which is used to complete the requested operation. ///
public Read ( byte buffer, int offset, int size ) : int
buffer byte The buffer to store data to.
offset int The location in the buffer where to start storing the received data.
size int The number of bytes to read.
Результат int

Seek() публичный Метод

Changes the current position in the stream. This method always throws a System.NotSupportedException.
Always throws a System.NotSupportedException.
/// any access to the method. ///
public Seek ( long offset, SeekOrigin origin ) : long
offset long not used.
origin SeekOrigin not used.
Результат long

SetLength() публичный Метод

Sets the length of the stream. This method always throws a System.NotSupportedException.
Always throws a System.NotSupportedException.
/// any access to the method. ///
public SetLength ( long value ) : void
value long not used.
Результат void

Write() публичный Метод

Writes data to the network stream.
Write method will block until all data, specified by size parameter, are sent. The NetworkStreamEx should have access right to write data to the network stream. You may use BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite property to check this.
/// The object was disposed. /// /// buffer is a null reference (Nothing in Visual Basic). /// /// offset is less than 0. /// -or- /// offset is greater than the length of buffer. /// -or- /// size is less than 0. /// -or- /// size is greater than the length of buffer minus /// the value of the offset parameter. /// /// An error occurred when attempting to access /// the socket which is used to complete the requested operation. ///
public Write ( byte buffer, int offset, int size ) : void
buffer byte Data to write.
offset int The position in the data buffer from which to begin writing.
size int The number of bytes to write.
Результат void

mDispose() публичный Метод

Releases all resources used by the BytesRoad.Net.Sockets.NetworkStreamEx.
Call Dispose when you are finished using the BytesRoad.Net.Sockets.NetworkStreamEx. If the NetworkStreamEx owns the underlying BytesRoad.Net.Sockets.SocketEx the Dispose method will call BytesRoad.Net.Sockets.SocketEx.Close method to release SocketEx object used for network communications. The Dispose method leaves the NetworkStreamEx in an unusable state. After calling Dispose, you must release all references to the NetworkStreamEx so the garbage collector can reclaim the memory that the NetworkStreamEx was occupying.
public mDispose ( ) : void
Результат void

mDispose() защищенный Метод

Releases the unmanaged resources used by the BytesRoad.Net.Sockets.NetworkStreamEx and optionally releases the managed resources.
This method is called by the public Dispose() method and the Finalize method. Dispose() invokes the protected Dispose(Boolean) method with the disposing parameter set to true. Finalize invokes Dispose with disposing set to false. When the disposing parameter is true, this method releases all resources held by any managed objects that this NetworkStreamEx references. This method invokes the Dispose() method of each referenced object. If the NetworkStreamEx owns the underlying BytesRoad.Net.Sockets.SocketEx the Dispose method will call BytesRoad.Net.Sockets.SocketEx.Close method to release SocketEx object used for network communications.
protected mDispose ( bool disposing ) : void
disposing bool /// true to release both managed and unmanaged resources; /// false to release only unmanaged resources. ///
Результат void