[REBOL] Re: question on the copy function and tcp ports
From: petr:krenzelok:trz:cz at: 28-Nov-2002 20:01
Alban Gabillon wrote:
>I have a small tcp serveur
>port_serveur: open tcp://:8000
>port_client: first port_serveur
>print copy port_client
>insert port_client "I am fine"
>and a small tcp client on the same machine
>port_client: open tcp://badeche:8000
>insert port_client "how are you"
>print copy port_client
>The copy function on the server side does work. it cannot manage to read the data sent
by the client. I know how to solve this problem. I can use instead the read-io function
for instance. But I do not understand why the copy function does not work in the server
whereas it works in the client. Indeed If I replace the copy function with the read-io
function in the server then the client can read the data sent by the server with the
>Hope I am clear
I am pressed for the time so I hope others will explain better, but -
You are using blocking synchronous mode. I don't like it. I think that
scenario should be reversed and Rebol should teach users to think in
async manner by default. But we have to accept what the situation
actually is. Saying this, try following - go to your client console,
press escape, and type "close port_client" - you will see server will
print your "how are you" message. Why is it so? - In sync mode, 'copy on
port simply blocks ... it waits either for time-out or till other side
closes the connection. You can still read your message even in sync
mode, but you either would have to know its length and use copy/part
1234, or simply read port by one char at a time, but even 'first and
'pick block, so I don't know how to efectively escape the loop even if I
try following on server side ...
port_serveur: open tcp://:8000
port_client: first port_serveur
while [not none? data: first port_client][prin data]
insert port_client "I am fine"
Got to go, hope someone throws here async mode behavior. The basic is
port: open/direct/binary/no-wait tcp ..... the most bug free behavior
:-) Maybe you can ommit /binary mode ...
That way, copy will not block, and the usage is e.g. as follows:
while [wait port data: copy port][print data]
- it is kind of an forever loop - wait is there to wait for data to
arrive, copy copies them, but does NOT wait till other side closes
connection or timeout occures ... How to escape it? With our device, I
use e.g. following code ...
port: open/direct/binary/no-wait tcp://12345:55
buffer: copy ""
while [wait port data: copy port][
append buffer data
if (length? data) == 1234 [break] ; because we wait for
known amount of data
] close port