Release 10.1A: OpenEdge Development:
Programming Interfaces

> ## Detecting data on a socket

> You can detect data on a socket using either the procedural or event-driven models.

> ##### Data detection using the procedural model

> To detect data procedurally, you can simply read whatever data is available on the socket object using the `READ( )` method. Using this model, you do not use the `READ-RESPONSE` event, and Progress does not automatically notify your application when data is available to read on the socket. You can also:

> - Check whether the socket is connected using the `CONNECTED( )` method.

> - Check for how many bytes are available to read, using the `GET-BYTES-AVAILABLE( )` method.

> How you use these methods depends on your application requirements and the options that you choose on the `READ( )` method. For more information on `READ( )` method options, see the "Reading data on a socket" section.

> ##### Data detection using the event-driven model

> To detect data using events, you set up an event handler for `READ-RESPONSE` events posted on a connected socket object. You can set up this event handler as follows:

> 1. Define an internal procedure that takes no arguments to serve as an event procedure. You can define this procedure in any procedure context that is active during the connection. When this procedure executes in response to a `READ-RESPONSE` event, the `SELF` system handle returns the handle of the connected socket.

> 2. Specify the procedure you defined in Step 1 as a `READ-RESPONSE` event procedure by invoking the `SET-CONNECT-PROCEDURE( )` method on the socket.

> 3. Include blocking I/O statements (such as `WAIT-FOR`) or `PROCESS EVENTS` statements in your code to initiate the handling of events. When any `READ-RESPONSE` event is received in the context of one of these statements, the event procedure specified in Step 2 executes.

> In the event-driven model, Progress can post a `READ-RESPONSE` event on a connected socket object for two reasons:

> - Data has arrived on the socket — You can then read this data during execution of the event procedure (or any time while the socket remains connected) using the `READ( )` method. You do not have to read all the bytes available on the socket. If any data remains after reading on the socket, Progress immediately posts another `READ-RESPONSE` event on the socket object for the available data.

> Note: After Progress posts the first `READ-RESPONSE` event for a new socket, Progress does not post another `READ-RESPONSE` event for the socket until you call the `READ( )` method on the socket object. Thus, if you do not read data on the socket in the event procedure, you must make sure to do so elsewhere in your application if you want the application to respond to any additional events for the socket.

> - The socket is disconnected — During execution of an event procedure called for a socket disconnection:

> - Calling the `READ( )` method on the socket object returns an error.
> - The value returned by a `GET-BYTES-AVAILABLE( )` method invoked on the socket object is zero (0).
> - The value returned by a `CONNECTED( )` method invoked on the socket object is `FALSE`.

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