Peek Method

Return data read from the socket, but do not remove it from the socket buffer.

Syntax

object.Peek( Buffer [, Length] )

Remarks

The Peek method reads the specified number of bytes from the socket and copies them into the buffer, but it does not remove the data from the internal socket buffer. Note that it is possible for the returned data to contain embedded null characters.

The Buffer argument specifies the variable that the data will be stored in. If the variable is a String then the data will be returned as a string of characters. This is the most appropriate data type to use if the remote host is sending data that consists of printable characters. If the remote host is sending binary data, it is recommended that a Byte array be used instead. It is also possible to read the data into an array of 16-bit or 32-bit integer values. In this case, the value of the ByteOrder property determines if those integers are converted from network to host byte order or not.

The Length argument specifies the number of bytes to read. Its maximum value is 231-1 = 2147483647. This argument is required to be present for string data. If a value is specified for this argument for other permissible types of data, and it is less than number of bytes that is determined by the control, then Length will override the internally-computed value.

The data returned by the Peek 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, up to the specified buffer size. 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 may cause the application to become non-responsive. To determine if there is data available to be read, use the IsReadable property.  

Return Value

If the method succeeds, it will return the number of bytes available to read from the socket without causing the thread to block. A return value of zero indicates that there is no data available to read at that time. If an error occurs, a value of -1 is returned.

See Also

IsReadable Property, Read Method, ReadLine Method, Write Method, WriteLine Method, OnRead Event, OnWrite Event


Copyright © 2008 Catalyst Development Corporation. All rights reserved.