AltME: Rebol School

Messages

Reichart
What is the smallest simplest example script for "port" .  
Hopefully something that shows off how easy it is to connect two computers and send information back and forth.  Perhaps a file.  Although a message system is ok too of course.
Gregg
Something like this? Run once as server, then again as client.
role: ask "Run as [C]lient or [S]erver? "
; Server
if role = "s" [
    port: tcp://:9090
    server: open/direct/no-wait port
    wait server
    client: first server
    print "Client connected."
    forever [
        wait any [client .01]
        if msg: copy client [
            print [now/precise msg]
            insert client "pong"
        ]
    ]        
]
; Client
if role = "c" [
    port: tcp://127.0.0.1:9090
    server: open/direct/no-wait port
    forever [
        insert server "ping"
        if msg: copy server [
            print [now/precise msg]
        ]
        wait .1
    ]
]
halt
Or there are slightly more involved examples out there. Like http://www.rebol.net/cookbook/recipes/0028.html
The WAITs in mine aren't needed, just there to slow things down a bit for human consumption.
Reichart
In these examples, it is assuming a server and a client, as opposed to two clients, and then a way to get them to find each other?
Maxim
either attempt[port: open tcp://:33101] [
    print "Running as server^/----^/"
    forever [ probe copy client: first wait port   close client]
][
    print "running as client^/----^/"
    forever [ insert (server: open tcp://localhost:33101) ask "message to send: "  close server]
]
a much simpler server/client example...
am building a google wave equivalent peer to peer example using console port... should be ready in a few minutes

Maxim
almost done, but I've got an errand to do, will be back with the solution in about 1h30
Reichart
:)
Maxim
I had a LOT of fun building this... its not perfect (backspace and delete cannot function in this mode cause I am not storing the console's screen buffer, but it goes a long way with just 40 lines... the vast majority of the code is actually managing the console's low level port interface.
rebol []
either attempt [lcl-port: open/lines tcp://:33101] [
    rmt-port: 33102
][
    lcl-port: open/lines tcp://:33102
    rmt-port: 33101
]
con: open/binary [scheme: 'console]
remote-port: none
digit: charset "0123456789"
forever [
    p: wait [lcl-port con]
    either p == con [
        key: first con
        either key = 27 [
            read-io con io: head clear "" 3
            cmd: copy "K [27 "
            foreach c io [ append cmd to-integer c append cmd " "]
            append cmd "]^/"
            chars: join "^[" io
        ][
            cmd: rejoin [ "K " key "^/" ]
            chars: to-char key
        ]
        
        insert ( rp: open rejoin [tcp://localhost: rmt-port] ) cmd
        close rp
    ][
        cmd: first first lcl-port
        parse cmd [
            "K " here:
            "[" (chars: to-string to-binary load here)
            
            | ( chars: to-char load here )
        ]
    ]
    prin chars
]
basically, store the script and run it twice,  both consoles are linked as if you where typing in anyone of them.  type in any of the two, and the other types the same thing!
we could modify the script a little to make it work across two different machines.  in that case, instead of changing the port number, we'd change the remote ip address instead.
DocKimbel
Instead of:
    insert ( rp: open rejoin [tcp://localhost: rmt-port] ) cmd
    close rp
you could use simply:
    write rejoin [tcp://localhost: rmt-port] cmd
Gregg
That's very cool Max.
Reichart, you just asked for smallest and simplest. :-)
Maxim
doc, yeah, true, I forgot we could use write on tcp ports.
for those who did not try the above script... do it (in 2.7.8) its pretty fun.
Reichart
"we could modify the script a little to make it work across two different machines.
That is the KEY, add that :)

Gabriele
Arnold
Johnk replied pointing to so chat, where my answer is too. Not going to subscribe to yet another site, there's too many already.
I have another nice question. I have 6 integers from 0 to about 23000 and I like to hash these. I calculated the 6 integer would fit into 96 (3 x 32 bit) bits. Any FUN ideas on howto do this?
Gregg
What is the context and the goal?

Last message posted 157 weeks ago.