Progress
External Program
Interfaces


Reading Data On a Socket

To read data from a connected TCP/IP socket, you invoke the READ( ) method on the corresponding socket object (SELF:READ( ) within the corresponding READ-RESPONSE event procedure). You can invoke this method on a connected socket at any time to read data. However, the method blocks depending on the amount of data available on the socket, the reading mode that you use, and the timeout value (set by the SO-RCVTIMEO option of the SET-SOCKET-OPTION() method).

The syntax of the READ( ) method follows:

SYNTAX
READ(
      memptr-expression 
    , position 
    , bytes-to-read 
      [ , mode ]
    ) 

The READ( ) method transfers data from the socket to the specified MEMPTR variable, memptr-expression, at the byte position in the MEMPTR region specified by the INTEGER expression, position. Exactly how much data the READ( ) method blocks to read depends on:

Specifying the Read Mode

You can specify mode using a compiler constant as shown in Table 10–3.

Table 10–3: Socket Reading Modes 
Compiler Constant
Value
Description
READ-AVAILABLE
1
The READ( ) method blocks until at least one (1) byte is available on the socket and reads the number of bytes that are available up to a maximum of bytes-to-read bytes.
READ-EXACT-NUM
2
(Default) The READ( ) method blocks until bytes-to-read bytes are available to read from the socket.

Thus, you can have the READ( ) method block until exactly the specified number of bytes are read (the default), or until all available bytes are read up to a maximum number allowed.

The appropriate reading mode to use depends on your application requirements. Note, however, that if you specify READ-EXACT-NUM, the READ( ) method blocks until it reads the specified number of bytes (no matter how long it takes) or until the socket is disconnected (whatever happens first).

Specifying the Timeout Length

Besides setting the read mode, you can also set the amount of time READ() waits before timing out. To do so, use the SO-RCVTIMEO option of the SET-SOCKET-OPTION() method. If you do not set a timeout value, the default is for READ() to wait forever.

READ()’s timeout behavior is affected by the interaction of the read mode and the timeout value, as Table 10–4 illustrates.

Table 10–4: Effect Of Read Mode and Timeout Value On READ()’s
Timeout Behavior

Read Mode

With a Timeout Value
Without a Timeout Value
READ-AVAILABLE
Assuming there are no connection failures, READ() blocks until one for the following occurs:
  • The timeout expires.
  • There is at least one byte available to read on the socket.
Assuming there are no connection failures, READ() blocks until there is at least one byte available to read on the socket.
READ-EXACT-NUM
Assuming there are no connection failures:
If there is any data on the socket, READ() blocks until there are bytes-to-read bytes available to read on the socket.
Else, READ() blocks until the timeout expires.
Assuming there are no connection failures, READ() blocks until there are bytes-to-read bytes available to read on the socket.

Verifying the Number Of Bytes Actually Read

You can verify the number of bytes actually read by the READ( ) method. The number of bytes read by the last READ( ) method invoked on a socket object is equal to the value of the BYTES-READ attribute invoked on the same socket object.


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095