home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / RXSOCK.ZIP / test_s.cmd < prev    next >
OS/2 REXX Batch file  |  1993-07-28  |  5KB  |  174 lines

  1. /*------------------------------------------------------------------
  2.  * test_s.cmd :
  3.  *------------------------------------------------------------------
  4.  * 08-05-92 originally by Patrick J. Mueller
  5.  *------------------------------------------------------------------*/
  6.  
  7. trace off
  8.  
  9. /*------------------------------------------------------------------
  10.  * choose the port
  11.  *------------------------------------------------------------------*/
  12. port = 1923
  13.  
  14. /*------------------------------------------------------------------
  15.  * initialize socket package
  16.  *------------------------------------------------------------------*/
  17. parse source os .
  18.  
  19. if (os = "OS/2") then
  20.    do
  21.    if RxFuncQuery("SockLoadFuncs") then
  22.       do
  23.       rc = RxFuncAdd("SockLoadFuncs","RxSock","SockLoadFuncs")
  24.       rc = SockLoadFuncs()
  25.       end
  26.    end
  27.  
  28. if (os = "AIX/6000") then
  29.    do
  30.    rc = SysAddFuncPkg("rxsock.dll")
  31.    end
  32.  
  33. /*------------------------------------------------------------------
  34.  * create the initial socket
  35.  *------------------------------------------------------------------*/
  36. s  = SockSocket("AF_INET","SOCK_STREAM",0)
  37. if (s = -1) then
  38.    do
  39.    say "Error on SockSocket:" errno
  40.    exit
  41.    end
  42.  
  43. /*------------------------------------------------------------------
  44.  * catch breaks
  45.  *------------------------------------------------------------------*/
  46. signal on halt
  47.  
  48. /*------------------------------------------------------------------
  49.  * bind socket to port
  50.  *------------------------------------------------------------------*/
  51. server.!family = "AF_INET"
  52. server.!port   = port
  53. server.!addr   = "INADDR_ANY"
  54.  
  55. rc = SockBind(s,"server.!")
  56. if (rc = -1) then
  57.    do
  58.    say "Error on SockBind:" errno
  59.    exit
  60.    end
  61.  
  62. /*------------------------------------------------------------------
  63.  * set queue size
  64.  *------------------------------------------------------------------*/
  65. rc = SockListen(s,10)
  66. if (rc = -1) then
  67.    do
  68.    say "Error on SockListen:" errno
  69.    exit
  70.    end
  71.  
  72. /*------------------------------------------------------------------
  73.  * infinite loop to handle requests ...
  74.  *------------------------------------------------------------------*/
  75. do forever
  76.    say "Waiting for client"
  77.  
  78.    /*---------------------------------------------------------------
  79.     * accept a connection
  80.     *---------------------------------------------------------------*/
  81.    ns = SockAccept(s,"client.!")
  82.    if (ns = -1) then
  83.       do
  84.       say "Error on SockAccept:" errno
  85.       exit
  86.       end
  87.  
  88.    /*---------------------------------------------------------------
  89.     * get clients host name
  90.     *---------------------------------------------------------------*/
  91.    if SockGetHostByAddr(client.!addr,"host.!") then
  92.       clientName = host.!name
  93.    else
  94.       clientName = "Unknown"
  95.  
  96.    say "Accepted client:" client.!addr clientName
  97.  
  98.    /*---------------------------------------------------------------
  99.     * get peer host name
  100.     *---------------------------------------------------------------*/
  101.    rc = SockGetPeerName(ns,"peer.!")
  102.    if (rc = -1) then
  103.       do
  104.       say "Error on SockGetPeerName:" errno
  105. /*    exit */
  106.       end
  107.  
  108.    say "PeerName:" peer.!addr
  109.  
  110.    /*---------------------------------------------------------------
  111.     * get socket host name
  112.     *---------------------------------------------------------------*/
  113.    rc = SockGetSockName(ns,"sock.!")
  114.    if (rc = -1) then
  115.       do
  116.       say "Error on SockGetSockName:" errno
  117. /*    exit */
  118.       end
  119.  
  120.    say "SockName:" sock.!addr
  121.  
  122.    /*---------------------------------------------------------------
  123.     * receive data from client
  124.     *---------------------------------------------------------------*/
  125.    rc = SockRecv(ns,"data",1000)
  126.    if (rc = -1) then
  127.       do
  128.       say "Error on SockRecv:" errno
  129.       exit
  130.       end
  131.  
  132.    say "Received:" data
  133.  
  134.    data = reverse(data)
  135.  
  136.    /*---------------------------------------------------------------
  137.     * send data back
  138.     *---------------------------------------------------------------*/
  139.    rc = SockSend(ns,data)
  140.    if (rc = -1) then
  141.       do
  142.       say "Error on SockSend:" errno
  143.       exit
  144.       end
  145.  
  146.    /*---------------------------------------------------------------
  147.     * close the new socket from client
  148.     *---------------------------------------------------------------*/
  149.    rc = SockSoClose(ns)
  150.    ns = ""
  151.    if (rc = -1) then
  152.       do
  153.       say "Error on SockSoClose:" errno
  154.       exit
  155.       end
  156.  
  157.    say "Closing connection"
  158.    say
  159. end
  160.  
  161. /*------------------------------------------------------------------
  162.  * handle break by closing sockets
  163.  *------------------------------------------------------------------*/
  164. halt:
  165.  
  166. say
  167. say "Quitting ..."
  168.  
  169. rc = SockSoClose(s)
  170.  
  171. if datatype(ns,"W") then
  172.    rc = SockSoClose(ns)
  173.  
  174.