A while ago I posted a script with functions to message back and forth
between two rebol scripts, but which didn't work reliably for some reason.
I don't know why it didn't work, but Bo sent me an example script which I
used to fix my problem. Since there was some interest I'll post this one.
The difference from my last attempt is that this one keeps the connections
open, while the last one closed them and reopened them all the time. Also
this one reads the messages one byte at a time. As a result this one is
much slower, but it's ok for simple messages at least.

REBOL[
 Title: "netmsgtest"
 Date: 2000-04-28
 Author: "Johan R�nnblom / Bohdan Lechnowsky"
 Purpose: {functions for sending messages between two rebolscripts}
]


transmit: func [info][
 append outport join mold info endchar
]

receive: func [][
 wait inport
 data: copy ""
 while [not (n: to-char pick inport 1) = endchar][
  append data n
 ]
 return do data
]

setup: func [][
 endchar: to-char 255
 otherurl: ask "Enter remote url: "
 either error? try [outport: open/binary/direct to-url rejoin [tcp://
otherurl ":8000"]][
  mode: "server"
  conn: open/binary/direct tcp://:8000
  wait conn
  inport: first conn 
  outport: open/binary/direct to-url rejoin [tcp:// otherurl ":8001"]
 ][
  mode: "client"
  conn: open/binary/direct tcp://:8001
  wait conn
  inport: first conn
 ]
]

setup

if mode = "client"  [transmit "1"]

forever[
 a: receive
 print a
 transmit ((to-integer a) + 1)
]

-- 
  /Johan R�nnblom, Team Amiga

Reply via email to