Hello,

here's my "multiserver" script, which I did for my friend, it is also 
well commented. You need to experiment along the lines of creating a 
wait-list. Events might happen both on the already established 
connection side, as well as on the listener side. You need to "wait" to 
all of those. And to not block, you can put also time into wait list. So 
if there is no event on your ports, you get interrupted by the time 
event. My script is async, non blocking ....

--------------------------------------
REBOL [
    Title: "Multiserver"
  Version:  0.2
   Author: "Petr Krenzelok"
]


;--- open listen server in an async manner
listen-port: 9005
server: open/direct/no-wait/binary join tcp://: listen-port
print ["Server running on port: " listen-port]

;--- set-up a wait-list - rebol will wait for every item contained ...
wait-list: copy []

;--- set-up a block to store open connections in there ...
;--- along with info - remote IP, port ...
conn-list: copy []


;--- insert time and server into wait-list
insert wait-list 0.001
insert wait-list server

;--- forever= endless loop ....
forever [

 event-port: wait wait-list

 ;--- if more events, let's pick first one (maybe not needed?)
 if block? event-port [event-port: first event-port]

 ;--- has event happened on our listening server? (new connection request)
 ;--- if so, accept an event, insert the connection into wait-list
 if event-port == server [
        ;--- accept connection ...
        time: now/time/precise
        connection: first server

        print join "New connection from: " [connection/remote-ip ":" 
connection/remote-port " at " time]
       
        ;--- append it into list of ports we wait for events to happen 
on (wait-list)
        insert wait-list connection

        ;--- append into our internal register of connections and its 
parameters
        insert conn-list reduce [connection connection/remote-ip 
connection/remote-port]

        write/append join %data- reduce [connection/remote-ip "-" 
connection/remote-port".txt"]
        join "New connection from: " [
             connection/remote-ip ":" connection/remote-port
            " at " time
             newline
            "---------------------------------------------------------" 
newline
        ]

        ;--- uncomment to see the data in console ...
        ;probe connection
 ]


 ;--- if event received is not new connection, it might be data on 
already existing connection
 if found? conn-port: find conn-list event-port [

    ;--- let's copy data ...
    time: now/time/precise
    data: copy event-port

    ;--- close request (none) or real data arriving?
    either none? data [
        print join "Closing connection request from: " [conn-port/2 ":" 
conn-port/3 " at " time]

        ;--- Write close even request into file ...
        write/append join %data- reduce [conn-port/2 "-" conn-port/3 ".txt"]
        join newline [
            "---------------------------------------------------------" 
newline
            "Closing connection request from: "
             conn-port/2 ":" conn-port/3
            " at " time
             newline newline
            "=========================================================" 
newline
             newline
       ]
 
        close event-port
        remove/part conn-port 3
    ][
        ;--- if we are here, real data arrived ...
        print join "Data from: " [conn-port/2 ":" conn-port/3 " at " time]

        ;--- write data into file ...
        write/append join %data- reduce [conn-port/2 "-" conn-port/3 
".txt"] to-string data
      
        ;--- uncomment to see data printed to console
        ;print mold to-string data
        ;print mold data   ; data as binary, not string

   ] ;end 'either
 ] ;end 'if

] ;end 'forever

-- 
To unsubscribe from the list, just send an email to 
lists at rebol.com with unsubscribe as the subject.

Reply via email to