[REBOL] Re: Understanding tcp
From: Rebolinth:nodep:dds:nl at: 15-Aug-2003 19:39
--
Conversation/lunch: "How do you Eat your Rebol in the Morning?"
Quoting patrick
Hello Patrick,
Here some tips programming servers under rebol, maybe usefull i dont know,
but what i know is dat i had to rethink the rebol logic befor i was able
to program servers in rebol...but finaly its easy ;-)
Make sure what yuo want with your server when opening it: ie.
lines based?
binary based?
blocking or non-blocking? (no-wait)
direct? (which is default)
Then when you setup a server under rebol like:
server: open tcp://:listenport
the server will listen on ALL nic's on your machine
so the connection coming in could be on ANY ip address on that NIC.
the server 'port is now opened and is ready to read incoming sessions,
BUT when yuo have a tcp server make a selection to distinguish between
the SERVER itself and or a NEW connection.
that looks like this:
connection: wait server ;; this wait for events on the server 'port
when a session comes INto server the 'connection will be SERVER-PORT itself !!!
(Depends if yuo have it all inside a forever loop with a wait inside!!
because then you might split it with a "either equal? server conenction [yes
then][ no then] or use dispatch or port/awake)
do a "first" on "connection" and you will get the 'port information from the
new remote-client!! like ->
client: first connection
This client 'port has information like client/remote-ip client/remote-port
client/local-ip client/local-port and ie. client/state/flags
Now you can read or write to THAT specific client, Still !!
If client: first connection
you can do read-io write-io on the client-server or insert copy anyting with
block related ...
STILL keep in mind, that tracking a remote-port disconnect is not possible
in rebol, you can only check if the remote hung up by doing am action like
insert, first copy on the client port befor you know its closed (irritating..)
Anyway.. here my MULTI SESSION ECHHO SERVER ...
enjoy, Norman.
rebol [ author: "rebolinth nodep dds nl" version: "1.0" ]
ports: [] ; ports we listen on
clients: [] ; ip:port dbase
listener: open/lines/direct tcp://:2000
ports: reduce [ 0:01 listener ]
forever [
;;; wait for 0:01 or ports event, use timer to prevent deadlocks,
dispatch
;;; is an option perhpas here, or even WAIT awake
port: wait ports
if port [
either port = listener [
append ports new: first port
print rejoin [ now " [NEW] " new/remote-ip ":"
new/remote-port ]
append/only clients rejoin [ new/remote-ip ":"
new/remote-port ]
insert new rejoin [ "Welcome " new/remote-ip ":"
new/remote-port ]
][
;;; try to read a line from the port, if error? then
client is gone
either error? try [ line: first port ] [
find clients rejoin [ port/remote-ip ":"
port/remote-port ]
print rejoin [ now " [DIS] " port/remote-ip ":"
port/remote-port ]
remove find ports port
remove find clients rejoin [ port/remote-ip ":"
port/remote-port ]
][
print rejoin [ now " [INC] " port/remote-ip ":"
port/remote-port " > " line ]
;;; send all ports, because we read a line from
one, to all
foreach x next next ports [ insert x line ]
]
]
]
]
close listener