Sample REXX Client Application

ooREXX on Windows has builtin functions for sockets support, effectively its sockets API. Support is via a group of functions of the form sockRequ() where Requ is the type of socket request. This example is the full program from which snippets have been taken as examples in the Sockets Programming Tutorial. The application connects to a web server and uses the HTTP 1.0 protocol to request the root page of the web site.

Sockets
Contents
Contents
Sockets
Examples
Prev

 

The Code


/** REXX *************************************************************/
/*                                                                   */
/* A sample ooREXX IP V4 client application that uses the HTTP 1.0   */
/* protocol to GET a pre-defined page from a web server. NB. with    */
/* HTTP 1.0 the server will break the connection after sending the   */
/* requested page.                                                   */
/*                                                                   */
/* Note: although this is the object oriented REXX implementation    */
/* for windows, this sample is written in a simple procedural manner */
/*                                                                   */
/* This example program is provided to illustrate use of the ooREXX  */
/* sockets API on a best effort basis. There is no warranty to its   */
/* functioning in any environment nor to its suitability for any     */
/* express function. You are free to include the entirety or         */
/* portions of this code into your own environment/application as    */
/* you wish and under your own responsibility with regard            */
/* suitability of purpose.                                           */
/*********************************************************************/

  ipAddr = '127.0.0.1'
  tcpPort = '80'
  data = 'GET / HTTP/1.0' || '0D0A0D0A'x

/*********************************************************************/
/*  Initialise the REXX API interface                                */
/*********************************************************************/
  rcde = RxFuncAdd("SockLoadFuncs","rxSock","SockLoadFuncs")
  if rcde <> 0 then do
      say "Unable to add SockLoadFuncs, RxFuncAdd returned:" rcde
      exit
  end
  SockLoadFuncs()
  if SockInit() <> 0 then do
      say 'SockInit failed:' errno
      exit
  end

/*********************************************************************/
/*  Open the socket                                                  */
/*********************************************************************/
  socNum = SockSocket("AF_INET", "SOCK_STREAM", "IPPROTO_TCP")
  if ( socNum < 0) then do
      say 'Error opening socket, ERRNO:' errno
      exit
  end

/*********************************************************************/
/*  Connect to remote server                                         */
/*********************************************************************/
  address.family = "AF_INET"
  address.port = tcpPort
  address.addr = ipAddr
  if SockConnect(socNum, address.) = -1 then do
      say 'SockConnect to' ipAddr':'tcpPort 'failed, ERRNO:' errno
      SockClose( socket)
      exit
  end
  say 'Connection established with' ipAddr':'tcpPort 'using socket' socNum

/*********************************************************************/
/*  Send data                                                        */
/*********************************************************************/
  SndLen = SockSend( socNum, data)
  if sndLen < 0 then do
      say 'Error sending data, ERRNO:' errno
      exit
  end
  if sndLen <> LENGTH(data) then do
      say 'SockSend Partially Complete, Sent:' sndLen 'bytes'
      exit
  end

/*********************************************************************/
/*  Wait up to 30 seconds for data response                          */
/*********************************************************************/
  rSocks.1 = socNum
  rSocks.0 = 1
  rcde = SockSelect( "rSocks.", "", "", 10)
  if rcde < 0 then do
      say "Error on SockSelecet, ERRNO:" errno
      exit;
  end;
  if rcde = 0 then do
      say "Server failed to return data, TIMEOUT"
      rcvLen = 0
  end
  else
      rcvLen = 1

/*********************************************************************/
/*  Receive data                                                     */
/*********************************************************************/
  do while rcvLen <> 0
      drop buffer
      RcvLen = SockRecv(socNum, buffer, 8192)
      if rcvLen <= 0 then do
          if rcvLen = -1 then do
              if errno <> 'ECONNRESET' then do
                  say "Error receiving data, ERRNO:" errno
                  rcvLen = 0
              end
          end
          say 'Connection on socket' sockNum 'terminated'
      end
      else
          call DATA_TRACE buffer
  end

/*********************************************************************/
/* Close the socket                                                  */
/*********************************************************************/
  retc = SockClose( socNum)

  exit

/*********************************************************************/
/* Character trace of passed data                                    */
/*********************************************************************/
DATA_TRACE: procedure
  Pad = '      '
  parse arg data
  do i = 1 to LENGTH( data) by 16
      Lc = RIGHT( D2X( i-1), 4, '0')
      w1 = C2X( SUBSTR( data, i, 4))
      w2 = C2X( SUBSTR( data, i+4, 4))
      w3 = C2X( SUBSTR( data, i+8, 4))
      w4 = C2X( SUBSTR( data, i+12, 4))
      if i+15 >= LENGTH( data) then do
          X = (LENGTH(data)-i)+1
          X = x * 2
          if X > 24 then
              X = x + 1
          if X > 16 then
              X = x + 1
          if X > 8 then
              X = x + 1
          Dat1 = LEFT( LEFT(w1 w2 w3 w4, x), 35)
          say pad lc'+' dat1 '+',
              || TRANSLATE( SUBSTR(data,i,16), '...', '0D0A09'x)'+'
      end
      else
          say pad lc'+' w1 w2 w3 w4 '+',
              || TRANSLATE( SUBSTR(data,i,16), '...', '0D0A09'x)'+'
  end
  return