home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / comms / other / rxsocket / examples / revserv.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1998-05-09  |  1.6 KB  |  88 lines

  1. /*
  2.     A simple stand alone service example.
  3.     It waits for connections on port 4000.
  4.     After a connection is made,
  5.     it waits for a string of max 256 char len and send it back reversed.
  6.     To stop it:
  7.  
  8.     rx "shell 'REVSERVPORT' 'QUIT'"
  9.  
  10. */
  11.  
  12. if ~show("L","rexxsupport.library") then
  13.     if ~addlib("rexxsupport.library",0,-30) then do
  14.         say "no rexxsupport.library"
  15.         exit
  16.     end
  17.  
  18. if ~show("L","rxsocket.library") then
  19.     if ~addlib("rxsocket.library",0,-30) then do
  20.         say "no rxsocket.library"
  21.         exit
  22.     end
  23.  
  24.  
  25. if ~Open("STDERR","*","W") | ~OpenPort("REVSERVPORT") then exit
  26.  
  27. sock = socket("INET","STREAM","IP")
  28. if sock<0 then do
  29.     say "Non posso aprire il socket:" errno()
  30.     exit
  31. end
  32.  
  33. local.ADDRFAMILY = "INET"
  34. local.ADDRADDR    = 0
  35. local.ADDRPORT   = 4000
  36. if bind(sock,"LOCAL")<0 then do
  37.     say "Non posso allocare la porta:" errno()
  38.     exit
  39. end
  40.  
  41. sig = 2**PortSignal("REVSERVPORT")
  42. SEL.READ.0=sock
  43. open = 1
  44. do while open
  45.  
  46.     if Listen(sock,5)<0 then do
  47.         say "Errore Listen():" errno()
  48.         exit
  49.     end
  50.  
  51.     res = WaitSelect("SEL",,,sig)
  52.  
  53.     pkt = GetPkt("REVSERVPORT")
  54.     if pkt ~= null() then do
  55.         comm= GetArg(pkt)
  56.         call reply(pkt)
  57.         if upper(comm) == "QUIT" then open = 0
  58.     end
  59.  
  60.     if sel.0.read then do
  61.  
  62.         lsock = accept(sock,"REMOTE")
  63.         if lsock<0 then do
  64.             say "Errore accept:" errno()
  65.             exit
  66.         end
  67.  
  68.         auth = AuthFun(remote.addrAddr,4000,remote.addrPort)
  69.         say auth
  70.  
  71.         len = recv(lsock,"BUF",256)
  72.         if ( len > 0 ) then do
  73.             buf = reverse(buf)
  74.             if send(lsock,buf) ~= length(buf) then do
  75.                 say "send: error" errno())
  76.             end
  77.         end
  78.         if ( len < 0 ) & ( errno() ~= 35 )
  79.             then call say "recv: error" errno()
  80.  
  81.         call CloseSocket(lsock)
  82.  
  83.     end
  84.  
  85. end
  86.  
  87. exit
  88.