z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Reading and writing data from and to a socket

z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
SC27-3660-00

Stream sockets during read and write calls might behave in a way that you would expect to be an error. The read() call might return fewer bytes, and the write() call may write fewer bytes, than requested. This is not an error, but a normal situation that your programs must deal with when they read or write data over a socket.

You might need to use a series of read calls to read a given number of bytes from a stream socket. Each successful read() call returns in the retcode field the number of bytes actually read. If you know you have to read, for example, 4000 bytes and the read call returns 2500, you have to reissue the read call with a new requested length of 4000 minus the 2500 already received (1500).

If you develop your program in COBOL, the following example shows an implementation of such logic. In this example, the message to be read has a fixed size of 8192 bytes:

 *---------------------------------------------------------------*   
 * Variables used by the READ call                               * 
 *---------------------------------------------------------------*  
 01  read-request-read              pic 9(8) binary value 0.
 01  read-request-remaining         pic 9(8) binary value 0.
 01  read-buffer. 
     05  read-buffer-total          pic x(8192) value space. 
     05  read-buffer-byte redefines read-buffer-total    
                                    pic x occurs 8192 times.
*---------------------------------------------------------------*
* Read 8K block from server                                     *    
*---------------------------------------------------------------*  
    move zero to read-request-read.
    move 8192 to read-request-remaining.
    Perform until read-request-remaining = 0 
       call 'EZASOKET' using soket-read 
          socket-descriptor 
          read-request-remaining
          read-buffer-byte(read-request-read + 1) 
          errno
          retcode
       if retcode < 0 then
          - process error and exit -
       end-if 
       add retcode to read-request-read
       subtract retcode from read-request-remaining
       if retcode = 0 then 
          Move zero to read-request-remaining
       end-if
    end-perform.
        

An actual execution of the program, following the above logic, used four read calls to retrieve 8K of data. The first call returned 1960 bytes, the second call 3920 bytes, the third call 1960 bytes and the final call 352 bytes. It is not possible to predict how many calls will be needed to retrieve the message. That depends on the internal buffer utilization of a TCP/IP. In some cases, only two calls were needed to retrieve 8K of data.

It is good programming practice, whenever you know the number of bytes to read, to issue read calls imbedded in logic, which is similar to the method described above.

If you work with short messages, you usually receive the full message on the first read() call, but there is no guarantee.

The behavior of a write() call is similar to that of a read() call. You might need to repeat more write() calls to write out all the data you want written. The following example illustrates this technique.

*---------------------------------------------------------------*
* Buffer and length fields for write operation                  * 
*---------------------------------------------------------------*
01  send-request-sent              pic 9(8) binary value 0.
01  send-request-remaining         pic 9(8) binary value 0.
01  send-buffer. 
    05  send-buffer-total          pic x(8192) value space.
    05  send-buffer-byte redefines send-buffer-total
                                   pic x occurs 8192 times.
*---------------------------------------------------------------* 
* Send 8K data block                                            * 
*---------------------------------------------------------------*
    move 8192 to send-request-remaining.
    move 0 to send-request-sent.
    Perform until send-request-remaining = 0 
       call 'EZASOKET' using soket-write 
            socket-descriptor  
            send-request-remaining
            send-buffer-byte(send-request-sent + 1)  
            errno 
            retcode 
       if retcode < 0 then  
            - process error and exit -
       end-if
       add retcode to send-request-sent
       subtract retcode from send-request-remaining 
    if retcode = 0 then 
       Move zero to send-request-remaining 
    end-if
 end-perform.
 

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014