Search code examples
tcprebolrebol2

Client disconnect results in a out of range error


Using One of the cookbook examples, I am trying to make a server like so in Rebol2:

listen: open tcp://:9999
waitports: [listen]
forever [
data: wait waitports
either same? data listen [
        active-port: first listen
        append waitports active-port
][
        incoming-from-remote: first data
        print incoming-from-remote
    ]     
]

With a client I can connect and send messages by inserting them, but when I close the port from the client side, I get the following error on the server:

** Script Error: Out of range or past end
** Where: forever
** Near: incoming-from-remote: first data
print incoming-from-remote

How can I handle this better?


Solution

  • put this in your either block

    either any [ 
       not incoming-from-remote: copy data
       "" = incoming-from-remote
    ] [
       attempt [
          close data
          remove find waitports data
       ]
    ] [
      print ..
    ]