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

  1. /*
  2.     udp test translated into rexx
  3.     from Amiga Magazine n.93 udp.c test by Rudi Chiarito
  4.  
  5.     Usage: udp <host> <port> <localPort>
  6.  
  7.     i.e.:
  8.         shell1 udp localhost 4000 4001
  9.         shell2 udp localhost 4001 4000
  10.  
  11.     to stop push <ctrl-c> in the shells
  12.  
  13. */
  14.  
  15. parse arg host remotePort localPort .
  16.  
  17. if ~show("L","rxsocket.library") then
  18.     if ~addLib("rxsocket.library",0,-30) then do
  19.         say "can't find rxsocket.library"
  20.         exit
  21.     end
  22.  
  23. if IsDotAddr(host) then do
  24.     remote.addrFamily = "INET"
  25.     remote.addrAddr   = host
  26. end
  27. else do
  28.     if ~gethostbyname("H",host) then do
  29.         say "udp: host" host "non trovato."
  30.         exit
  31.     end
  32.     remote.addrFamily = h.hostAddrType
  33.     remote.addrAddr   = h.hostAddrList.0
  34. end
  35.  
  36. remote.addrPort = remotePort
  37.  
  38. sock = socket("INET","DGRAM","IP")
  39. if sock<0 then do
  40.     say "udp: non posso aprire il socket:" errno()
  41.     exit
  42. end
  43.  
  44. local.addrFamily = "INET"
  45. local.addrAddr     = 0
  46. local.addrPort   = localPort
  47. if bind(sock,"LOCAL")<0 then do
  48.     say "udp: non posso allocare la porta:" errno()
  49.     exit
  50. end
  51.  
  52. call IOCtlSocket(sock,"FIONBIO",1)
  53.  
  54. data = "Ciao"
  55. dataLen = length(data)
  56.  
  57. down = 0
  58. do while 1
  59.  
  60.     n = SendTo(sock,data,0,"REMOTE")
  61.     if n!=dataLen then do
  62.         say "udp errore di SendTo():" errno()
  63.         exit
  64.     end
  65.  
  66.     call delay(10)
  67.  
  68.     if down then call WriteCh("STDOUT",".")
  69.  
  70.     n = RecvFrom(sock,"BUFF",10,0,"REMOTE")
  71.     if n==-1 then do
  72.         err = errno()
  73.         if err==35 then do
  74.             if ~down then do
  75.                 call WriteCh("STDOUT","udp: non ricevo più dati .")
  76.                 down = 1
  77.             end
  78.         end
  79.         else do
  80.             say "udp: errore di RecvFrom():" err
  81.             exit
  82.         end
  83.     end
  84.     else do
  85.         say "udp: byte letti:" n
  86.         down = 0
  87.     end
  88.  
  89. end
  90.