question on the copy function and tcp ports
[1/5] from: gabillon::univ-pau::fr at: 28-Nov-2002 19:16
Hi ALL,
I have a small tcp serveur
port_serveur: open tcp://:8000
forever [
port_client: first port_serveur
print copy port_client
insert port_client "I am fine"
close port_client
]
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
close 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
copy function.
Hope I am clear
AG
[2/5] from: petr:krenzelok:trz:cz at: 28-Nov-2002 20:01
Alban Gabillon wrote:
>Hi ALL,
>I have a small tcp serveur
<<quoted lines omitted: 12>>
>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
copy function.
>Hope I am clear
Hi ....
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
forever [
port_client: first port_serveur
while [not none? data: first port_client][prin data]
insert port_client "I am fine"
close port_client
]
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
cheers,
-pekr-
[3/5] from: g:santilli:tiscalinet:it at: 28-Nov-2002 19:51
Hi Alban,
On Thursday, November 28, 2002, 7:16:39 PM, you wrote:
AG> The copy function on the server side does work. it cannot manage to read
AG> the data sent by the client.
The reason is that you're waiting on both ends. Instead of using
READ-IO, you can open the port as /NO-WAIT.
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[4/5] from: ingo:2b1 at: 28-Nov-2002 20:10
Hi Alban,
Alban Gabillon wrote:
> Hi ALL,
> I have a small tcp serveur
<<quoted lines omitted: 17>>
> read-io function in the server then the client can read the data sent by
> the server with the copy function.
It's the way network works. The copy will only return, when it is sure
to have copied _all_ data from the port. Now, how do you tell, if you've
got everything? The only way to be sure, is wait until the port is
closed. So, the copy in your server waits until the client closes port,
while the client waits until the server closes the port. A traditional
deadlock scenario.
Now, what can you do about it? Tell the server to _not_ wait until the
client has sent all the data, like this:
port_serveur: open/no-wait tcp://:8000
; CHANGES HERE! --------
forever [
port_client: first port_serveur
print copy port_client
insert port_client "I am fine"
close port_client
]
Now, the 'copy just gets what it finds in the port, and returns it. But
you don't know, if this is everything, the client sent you, just that it
is some of it.
To be sure you get everything, you need to do the copy in a loop, until
copy returns an empty string (but beware, it might be a very slow
connection, too, I guess).
port_serveur: open/no-wait tcp://:8000
; CHANGES HERE! --------
forever [
data: copy ""
port_client: first port_serveur
while [not empty? new-data: copy port_client][
append data new-data
]
print data
insert port_client "I am fine"
close port_client
]
; No changes in the client code needed
Of course, someone like Holger could have explained much better, I hope
it helped, anyway.
Ingo
[5/5] from: alban:gabillon:univ-pau at: 29-Nov-2002 10:58
Hi Ingo, Petr and Gabriele
Thanks for your explanations. I got it
I agree, Pekr, async mode behaviour should be the default...
I also find the core guide very poor on this matter
==============================================
Alban Gabillon
University of Pau
LIUPPA/Connected System SECurity
Antenne de Mont de Marsan
371, rue du Ruisseau BP 201
40004 Mont de Marsan Cedex France
Tel: (33) 5 58 05 76 05
Fax: (33) 5 58 06 83 70
email: [gabillon--univ-pau--fr]
http://csysec.univ-pau.fr/
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted