Read Method

Return data read from the socket with additional options.

Syntax

object.Read( Buffer, [Length], [Options], [RemoteAddress], [RemotePort] )

Remarks

The Read method returns data that has been read from the socket, up to the number of bytes specified. If no data is available to be read, an error will be generated if the control is non-blocking mode. If the control is in blocking mode, the program will stop until data is returned by the server or the connection is closed. 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 Options argument is reserved for future functionality and should either be omitted, or specified with a value of zero. Specifying a non-zero value will cause the method to fail and return an error.

The RemoteAddress argument specifies a string passed by reference which will contain the IP address of the remote host that sent the data. For a TCP connection, the IP address is the same value that was used to establish the connection. When reading data from a UDP socket, this is the IP address of the peer that sent the datagram. This information can be used in conjunction with the Write method to send a datagram back to that host. If the remote peer's IP address is not required, this argument may be omitted.

The RemotePort argument specifies an long integer passed by reference which will contain the port number of the remote host that sent the data. When reading data from a UDP socket, this is the port number used by the peer who sent the datagram. This information can be used in conjunction with the Write method to send a datagram back to that host. If the remote peer's port number is not required, this argument may be omitted.

Return Value

The number of bytes actually read from the socket is returned by this method. If an error occurs, a value of -1 is returned.

See Also

IsReadable Property, Peek Method, Read Method, Write Method, OnRead Event, OnWrite Event


Copyright © 2008 Catalyst Development Corporation. All rights reserved.