rebolrebol3rebol2

How to open/write/read port in REBOL3?


I have this code in REBOL2:

port: open/direct tcp://localhost:8080
insert port request
result: copy port
close port

What would be equivalent in REBOL3?


Solution

  • REBOL3 networking is async by default, so the code in REBOL3 must look like:

    client: open tcp://localhost:8080
    client/awake: func [event /local port] [
        port: event/port
        switch event/type [
            lookup  [open port]
            connect [write port to-binary request]
            read [
               result: to-string port/data
               close port
               return true
            ]
            wrote [read event/port]
        ]
        false
    ]
    wait [client 30] ;the number is a timeout in seconds
    close client 
    

    Based on: http://www.rebol.net/wiki/TCP_Port_Examples

    EDIT: above link does not exists anymore, but here is it transferred to GitHub's wiki: https://github.com/revault/rebol-wiki/wiki/TCP-Port-Examples