SocketTools .NET Edition

SocketWrench.ReadStream Method (Byte[], Int32, Byte[], SocketStream)

Read a data stream from the socket and store it in the specified byte array.

[Visual Basic]
Overloads Public Function ReadStream( _
   ByVal buffer As Byte(), _
   ByRef length As Integer, _
   ByVal marker As Byte(), _
   ByVal options As SocketStream _
) As Boolean
[C#]
public bool ReadStream(
   byte[] buffer,
   ref int length,
   byte[] marker,
   SocketStream options
);

Parameters

buffer

A byte array that the data will be stored in.

length
An integer value passed by reference which specifies the maximum number of bytes of data to read. This value cannot be larger than the size of the buffer specified by the caller. When the method returns, this value will be updated with the actual number of bytes read from the socket.
marker
An array of bytes which is used to designate the logical end of the data stream. When this byte sequence is encountered by the method, it will stop reading and return to the caller. The buffer will contain all of the data read from the socket up to and including the end-of-stream marker.
options
One of the SocketStream enumeration values which specifies how the data is processed.

Return Value

This method returns a boolean value. If the method succeeds, the return value is true. If the method fails, the return value is false. To get extended error information, check the value of the LastError property.

Remarks

This method will force the current thread to block until the operation completes. If this method is called and the Blocking property is set to false, it will automatically switch the socket into a blocking mode, read the data stream and then restore the socket to non-blocking mode when it has finished. If another socket operation is attempted while ReadStream is blocked waiting for data from the remote host, an error will occur. It is recommended that this method only be used with blocking (synchronous) socket connections; if the application needs to establish multiple simultaneous connections, it should create worker threads to manage each connection.

It is possible for data to be returned in the buffer even if the method returns false. Applications should also check the value of the length parameter to determine if any data was copied into the buffer. For example, if a timeout occurs while the method is waiting for more data to arrive on the socket, it will return zero; however, data may have already been copied into the buffer prior to the error condition. It is the responsibility of the application to process that data, regardless of the method return value.

Because ReadStream can potentially cause the current thread to block for long periods of time as the data stream is being read, the class will periodically generate OnProgress events. An application can use this event to update the user interface as the data is being read. Note that an application should never perform a blocking operation inside the event handler.

See Also