C$SOCKET AGS-READ with length zero doesn't work

0 Likes

Problem:

A call to the C$SOCKET library routine with the AGS-READ opcode and a read length of zero should populate RETURN-CODE with the number of bytes of data available on the socket.

That value can then be used for the read length to obtain all the data in a single read (if the buffer is large enough).

If the call to C$SOCKET AGS-READ with length zero doesn't occur within an ACCEPT then RETURN-CODE is always zero even if there is data available on the socket.

Resolution:

First call C$SOCKET AGS-READ with a length of negative 1 (-1) then call C$SOCKET AGS-READ with length zero.  This will ensure that RETURN-CODE contains the number of bytes of data available in the socket regardless of whether the call is made within an ACCEPT statement.

It will look something like this:

     **** read with length -1 to set up the read with length zero ****

               move -1 to read-length.

               call "c$socket" using ags-read, socket-handle,

                                      data-buffer, read-length.

      **** read with length zero to get # of bytes of data available ****

               move zero to read-length.

               call "c$socket" using ags-read, socket-handle,

                                      data-buffer, read-length.

      **** now we can get all of the data in one read ****

               move return-code to read-length.

               move space to data-buffer.

               call "c$socket" using ags-read, socket-handle,

                                      data-buffer, read-length.

The working-storage definitions are:

       77  socket-handle   usage handle.

       77  data-buffer       &nbsp ; pic x(500).

       77  read-length       &nbsp ;pic s9(3) comp-5.

Old KB# 2662
Comment List
Related
Recommended