home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 28
/
amigaformatcd28.iso
/
-seriously_amiga-
/
comms
/
other
/
rxsocket
/
examples
/
ntblockecho.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
1998-05-09
|
2KB
|
126 lines
/*
Name: ntblockecho.rexx
Version: 2.0
Author: alfie
Date: 28.04.98
Description: shows how to make a non blocking connction;
the stopping-connetction event is a msg on
port "PORT";
Usage: rx ntblockecho <host>
Note: it is in itself a simple echo client;
*/
parse arg host .
if (host=="") | (host=="?") then do
say "Usage: ntblockecho <host>"
exit
end
addr = resolve(host)
if addr=="-1" then do
say "host <" || host || "> not found"
exit
end
sin.addrFamily = "INET"
sin.addrAddr = addr
if GetServByName("SE","echo","tcp") then sin.addrPort = SE.servPort
else sin.addrPort = 7
if ~OpenPort("PORT") then do
say "can't open my port"
exit
end
portSig = 2**PortSignal("PORT")
sigBit=AllocSignal()
if sigBit==-1 then do
say "can't allocate signal bit"
exit
end
sig=2**sigBit
sock = socket("INET","STREAM","IP")
if sock<0 then do
say "can't create socket (" || errno() || ")"
exit
end
if IOCtlSocket(sock,"FIONBIO",1)<0 then do
say "can't set socket as non blocking (" || errno() || ")"
exit
end
if SetSockOpt(sock,"SOCKET","EVENTMASK","CONNECT")<0 then do
say "can't set async events (" || errno() || ")"
exit
end
SET.SIGEVENTMASK = sig
if SetSocketBase("SET")>0 then do
say "can't set async events signal (" || errno() || ")"
exit
end
res = connect(sock,"SIN")
err=errno()
if (res<0) && (err~=35) && (err~=36) then do
say "connection error (" || errno() || ")"
exit
end
timeout = 10
signal = or(portSig,sig)
SEL.WRITE.0 = sock
con = 0
time = time("R")
do while ~con & (time("E")<timeout)
res = WaitSelect("SEL",timeout,0,signal)
if and(SEL.SIGNALS,portSig)~=0 then do
pkt = GetPkt("PORT")
if pkt~=null() then do
m = GetArg(pkt)
call reply(pkt)
say "message on PORT:" m
end
end
if res==1 then con = 1;
end
if ~con then do
say "timeout"
exit
end
say "connected"
if IOCtlSocket(sock,"FIONBIO",0)<0 then do
say "can't set socket as blocking (" || errno() || ")"
exit
end
request = "echo service test"
res = send(sock,request)
if res~=length(request) then do
say "send errore (" || errno() || ")"
exit
end
len = recv(sock,"BUFF",256)
if len<0 then do
say "recv error (" || errno() || ")"
exit
end
say buff