Example

Two Simple Scripts Talking Over TCP

Author: Cal Dixon
This example requires REBOL/View
Return to REBOL Cookbook

This pair of scripts shows how to send messages between two REBOL scripts. When you try running these, be sure to start the server first. There are four major steps for the server and three for the client.

On the server you start by creating a "listen" port with the REBOL OPEN function. In this example we are making a connection on port 9097. You specify that this is a listen port by omitting the machine name part of the URL you are opening. Next we get a connection from the "listen" port. Then we can send data with INSERT and receive data with COPY.

The example is shown below with a description of each line (then as the full script at the end of this document.)

This is the server, so start by creating a listen port for the other script to connect to:


    listen: open/direct/lines/no-wait tcp://:9097

Now, wait for a connection to come in on the listen port:


    wait listen

When the script awakes, get the first connection:


    connect-in: first listen

Create a new window to use to talk to the other script:


    view/new win: layout/size [
        backdrop red
        text "type a message then press [ENTER] to send to the green window"
        entry: field [ insert connect-in entry/text focus entry ]
        display: info "" 300x200
        button "quit" [ close connect-in close listen unview halt ]
    ] 320x300
    win/offset: 0x0
    show win

In this window, when you press enter you will send the text through the port and put the cursor back in the text entry field. Below it is where text is put when it is received from the client script. When the button is pressed, close the connection and the listen port before quitting.

The loop below waits for data from the other script, but processes GUI events while we wait:


    forever [
        wait connect-in
        foreach msg any [copy connect-in []] [
            append display/text join msg newline
        ]
        show display
    ]

This code uses COPY to read from the connection and display it. If the other script closes the connection, COPY will return NONE so we use ANY to make sure FOREACH has a block to work with.

On the client we start with opening a connection to the listen port we created in the server script. Note that here we specify that we are connecting to a machine called "localhost" - this is a special name that always refers to the computer you are using. Now we can again use INSERT and COPY to send and recieve data.

First open a connection to the server script:


    connect-out: open/direct/lines/no-wait tcp://localhost:9097

Now, create a new window:


    view/new win: layout/size [
        backdrop green
        text "type a message then press [ENTER] to send to the red window"
        entry: field [ insert connect-out entry/text focus entry ] 
        display: info "" 300x200
        button "quit" [ close connect-out unview halt ]
    ] 320x200
    win/offset: 340x0
    show win

When you enter text and press enter, the text is sent through the port and the cursor is put back in the text field. The reply from the server is displayed in the INFO text area. When the button is pressed, the connection is closed before quitting.

Here is the loop that waits for data from the other script:


    forever [
        wait connect-out
        foreach msg any [copy connect-out []] [
            append display/text join msg newline
        ]
        show display
    ]

This loop works identically to that found in the server script.

Note that the server and client scripts are nearly the same. You can make a single script that works for both. To start the server, just run the script. To start the client, provide a -c argument to the script.


    REBOL [ Title: "Example TCP Server & Client" ]

    either all [args: system/options/args find args "-c"] [
        port: open/direct/lines/no-wait tcp://localhost:9097
        args: true
    ][
        listen: open/direct/lines/no-wait tcp://:9097
        wait listen
        port: first listen
        args: false
    ]

  view/new win: layout/size [
        backdrop either args [green][red]
        text "type a message then press [ENTER] to send to the green window"
        entry: field [ insert port entry/text focus entry ]
        display: info "" 300x200
        button "quit" [
            close port
            if not args [close listen]
            unview halt
        ]
    ] 320x300
    win/offset: 0x0
    show win

    forever [
        wait port
        foreach msg any [copy port []] [
            append display/text join msg newline
        ]
        show display
    ]

To provide the -c as an argument to the script, run the script from a shell or shortcut such as:


    rebol example.r -c

Or run it from REBOL console with:


    do/args %example.r "-c"

2006 REBOL Technologies REBOL.com REBOL.net