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


READ

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

Use the READ command to read data on the specified socket. The maximum amount of data to be read is specified by the maxlength parameter. If the socket is in blocking mode and data is not available on the socket, the command blocks until data arrives.

Consider the following additional information:
  • If the socket is a stream socket and the length of the data returned is 0, the remote peer has closed its side of the connection.
  • If the socket is a connected datagram socket, the command returns data up to the length specified by the maxlength parameter. The remainder of the datagram is discarded. To ensure that the entire datagram is received, set the maxlength parameter to 65535 or greater.
Guidelines:
  • For stream sockets, data is processed as streams of information with no boundaries separating the data. The application provides record management. Applications should place the command in a loop until all the data has been received.
  • For nonblocking sockets, use the SELECT command to determine whether there is data to be read on the socket.
Tip: If the SO_ASCII socket option is enabled, then the data received is translated from EBCDIC to ASCII.

Format

Read syntax diagramSkip visual syntax diagram
                                   .-,--10 000----.      
>>-SOCKET--(--"READ"--,--socketid--+--------------+--)---------><
                                   '-,--maxlength-'      

Parameters

socketid
The socket descriptor.
maxlength
The maximum amount of data (in bytes) to be returned. The maxlength parameter can be a number in the range 0-100 000. By default, this parameter is set to 10 000.

Returned value

The command returns a string that contains the return code, the maximum length of the data returned, and the data, for example, 0 19 This is sample data. The return code can be 0, a REXX socket API error number, or the REXX TCP/IP error number that is set by the socket command. The return code 0 indicates that the requested socket command was completed successfully. The data length 0 indicates that the connection was closed by the remote peer.

See Socket call error return codes for additional information about the numeric error codes that are returned by this command.

The following REXX TCP/IP error numbers can be returned:
  • 4 EITNR
  • 5 EIO
  • 9 EBADF
  • 22 EINVAL
  • 35 EWOULDBLOCK
  • 38 ENOTSOCK
  • 45 EOPNOTSUPP
  • 54 ECONNRESET
  • 57 ENOTCONN
  • 60 ETIMEDOUT
The following REXX socket API error numbers can be returned:
  • 2001 EINVALIDRXSOCKETCALL
  • 2005 ESUBTASKNOTACTIVE
  • 2009 ESOCKETNOTDEFINED

LE C/C++ equivalent

ssize_t read(int fs, void *buf, size_t N);

Code example

/* REXX EZARXR21 */
/*
 * This sample demonstrates the use of the READ and RECV
 * socket commands.
 *
 * To use the READ command, set the variable g_RECVCMD equal to "READ"
 * to use the RECV command, set the variable g_RECVCMD equal to "RECV"
 *
 * The program creates a listening socket and then goes into a
 * loop and blocks on the accept command. When a new connection is
 * ACCEPTED the program will issue the READ or RECV command until
 * the connection is terminated.
 *
 * If the data received is the string "DONE", then the
 * program will close the accepted socket and wait for a new
 * connection request.
 */
g_RECVCMD = "READ"
src = socket("INITIALIZE","MYSET01",10);
if perror(src,"INITIALIZE") = 0 then do
   src = socket("SOCKET","AF_INET6","STREAM");
   if perror(src,"SOCKET") = 0 then do
      parse var src . l_sockid
      l_name6 = "AF_INET6 54004 0 ::0 0";
      src = socket("BIND", l_sockid, l_name6);
      if perror(src,"BIND") = 0 then do
         src = socket("LISTEN", l_sockid);
         if perror(src,"LISTEN") = 0 then do
            say "Listening on socket "l_sockid;
            do forever
               src = socket("ACCEPT", l_sockid );
               if perror(src,"ACCEPT") = 0 then do
                  parse var src . l_newsockid . ;
                  l_datalen = -1;
                  l_Done = "FALSE";
                  l_totallen = 0;
                  l_packet = "";
                  /* **********************************************
                   * Loop around RECV|READ command until all data has
                   * has been received and the client closes the
                   * connection.
                   * **********************************************/
                  do until l_datalen = 0 | l_done = "TRUE"
                     src = socket(g_RECVCMD,l_newsockid,512);
                     if perror(src,g_RECVCMD) = 0 then do
                        parse var src l_retcode l_datalen l_data
                        if l_datalen > 0 then do
                           l_totallen = l_totallen + l_datalen;
                           if l_packet = "" then do
                              l_packet = l_data;
                              if l_packet = "DONE" then
                                 l_done = "TRUE";
                           end;
                           else l_packet = l_packet||l_data;
                        end;
                        else do
                        /* ***************************************
                         * A data length of zero indicates the
                         * connection has been closed by the
                         * remote side
                         * ***************************************/
                            Say "Connection has been closed",
                                "received "l_totallen" bytes";
                            l_done = "TRUE";
                        end;
                     end;
                     else do
                        l_done = "TRUE";
                     end;
                  end; /* DO READ */
                  src = socket("CLOSE",l_newsockid);
                  src = perror(src,"CLOSE");
               end; /* ACCEPT */
            end; /* DO FOREVER */
         end;
      end;
   end;
end; /* INITIALIZE */
src = perror(socket("TERMINATE","MYSET01"),"TERMINATE");
exit 0;

/* This routine returns -1 if the first word if arg 1 is not zero */
perror: if word(arg(1),1) = 0 then return 0; else
    Say arg(2) "Error : "arg(1);
    return -1;
Figure 2. READ command example

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014