SocketTools .NET Edition

SocketWrench.Peek Method (Byte[], Int32)

Read data from the socket and store it in a byte array, but do not remove the data from the socket buffers.

[Visual Basic]
Overloads Public Function Peek( _
   ByVal buffer As Byte(), _
   ByVal length As Integer _
) As Integer
[C#]
public int Peek(
   byte[] buffer,
   int length
);

Parameters

buffer

A byte array that the data will be stored in.

length
An integer value 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.

Return Value

An integer value which specifies the number of bytes actually read from the socket. A return value of zero specifies that there is no data available to be read. If an error occurs, a value of -1 is returned and the application should check the value of the LastError property to determine the cause of the failure.

Remarks

The Peek method returns data that is available to read from the socket, up to the number of bytes specified. The data returned by this method is not removed from the socket buffers. It must be consumed by a subsequent call to the Read method. The return value indicates the number of bytes that can be read in a single operation. However, it is important to note that it may not indicate the total amount of data available to be read from the socket at that time.

If no data is available to be read, the method will return a value of zero. Using this method in a loop to poll a non-blocking socket can cause the application to become non-responsive. To determine if there is data available to be read, use the IsReadable property.

See Also

SocketWrench Class | SocketTools Namespace | SocketWrench.Peek Overload List