[REBOL] Re: Listener process
From: greggirwin:mindspring at: 3-Sep-2003 18:03
Hi Ashley,
AT> I am only interested in pretty basic stuff at the moment (more
AT> command / response orientated than code passing, not worrying
AT> about multiple callers).
Here's the basic idea:
;; Server ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
REBOL []
listen-port-num: 2003
process-client-request: func [port /local cmd][
if error? set/any 'err try [
wait port
cmd: copy port ; get cmd
print [now/time/precise tab "client request:" cmd]
result: switch/default load cmd [
0 [quit]
1 ["one"]
2 ["two"]
][none]
insert port result ; send result
close port ; service only one request
][print mold disarm err]
]
; Open our main port that listens for client connections.
if error? set/any 'err try [
server-port: open/no-wait join tcp://: listen-port-num
][
print ["Error opening listen port:" mold disarm err]
halt
]
print "Simple Server Now Listening..."
forever [
process-client-request first wait reduce [server-port]
;-- The longer version
;wake-port: wait reduce [server-port]
;if wake-port = server-port [
; process-client-request first wake-port
;]
]
;; Client ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
REBOL []
print "Simple Client"
server-port: 2003
forever [
wait 2
port: open join tcp://127.0.0.1: server-port
insert port first random [1 2]
wait port
print copy port
close port
]
If you want to handle multiple requests from a client, without them
opening the port each time, you'll have to do things a little
differently, but hopefully this will get you started.
-- Gregg