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

  1. /*
  2.     Name:        ntblockecho.rexx
  3.  
  4.     Version:     2.0
  5.  
  6.     Author:         alfie
  7.  
  8.     Date:        28.04.98
  9.  
  10.     Description: shows how to make a non blocking connction;
  11.                  the stopping-connetction event is a msg on
  12.                  port "PORT";
  13.  
  14.     Usage:          rx ntblockecho <host>
  15.  
  16.     Note:         it is in itself a simple echo client;
  17. */
  18.  
  19. parse arg host .
  20.  
  21. if (host=="") | (host=="?") then do
  22.     say "Usage: ntblockecho <host>"
  23.     exit
  24. end
  25.  
  26. addr = resolve(host)
  27. if addr=="-1" then do
  28.     say "host <" || host || "> not found"
  29.     exit
  30. end
  31.  
  32. sin.addrFamily = "INET"
  33. sin.addrAddr   = addr
  34. if GetServByName("SE","echo","tcp") then sin.addrPort = SE.servPort
  35. else sin.addrPort = 7
  36.  
  37. if ~OpenPort("PORT") then do
  38.     say "can't open my port"
  39.     exit
  40. end
  41. portSig = 2**PortSignal("PORT")
  42.  
  43. sigBit=AllocSignal()
  44. if sigBit==-1 then do
  45.     say "can't allocate signal bit"
  46.     exit
  47. end
  48. sig=2**sigBit
  49.  
  50. sock = socket("INET","STREAM","IP")
  51. if sock<0 then do
  52.     say "can't create socket (" || errno() || ")"
  53.     exit
  54. end
  55.  
  56. if IOCtlSocket(sock,"FIONBIO",1)<0 then do
  57.     say "can't set socket as non blocking (" || errno() || ")"
  58.     exit
  59. end
  60.  
  61. if SetSockOpt(sock,"SOCKET","EVENTMASK","CONNECT")<0 then do
  62.     say "can't set async events (" || errno() || ")"
  63.     exit
  64. end
  65.  
  66. SET.SIGEVENTMASK = sig
  67. if SetSocketBase("SET")>0 then do
  68.     say "can't set async events signal (" || errno() || ")"
  69.     exit
  70. end
  71.  
  72. res = connect(sock,"SIN")
  73. err=errno()
  74. if (res<0) && (err~=35) && (err~=36) then do
  75.     say "connection error (" || errno() || ")"
  76.     exit
  77. end
  78.  
  79. timeout = 10
  80. signal = or(portSig,sig)
  81. SEL.WRITE.0 = sock
  82. con = 0
  83. time = time("R")
  84. do while ~con & (time("E")<timeout)
  85.  
  86.     res = WaitSelect("SEL",timeout,0,signal)
  87.  
  88.     if and(SEL.SIGNALS,portSig)~=0 then do
  89.         pkt = GetPkt("PORT")
  90.         if pkt~=null() then do
  91.             m = GetArg(pkt)
  92.             call reply(pkt)
  93.             say "message on PORT:" m
  94.         end
  95.     end
  96.  
  97.     if res==1 then con = 1;
  98.  
  99. end
  100.  
  101. if ~con then do
  102.     say "timeout"
  103.     exit
  104. end
  105.  
  106. say "connected"
  107.  
  108. if IOCtlSocket(sock,"FIONBIO",0)<0 then do
  109.     say "can't set socket as blocking (" || errno() || ")"
  110.     exit
  111. end
  112.  
  113. request = "echo service test"
  114. res = send(sock,request)
  115. if res~=length(request) then do
  116.     say "send errore (" || errno() || ")"
  117.     exit
  118. end
  119.  
  120. len = recv(sock,"BUFF",256)
  121. if len<0 then do
  122.     say "recv error (" || errno() || ")"
  123.     exit
  124. end
  125. say buff
  126.