For Programmers: Free Programming Magazines  


Home > Archive > Tcl > July 2004 > Working with nonblocking sockets









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Working with nonblocking sockets
Michiel

2004-07-28, 9:08 pm

Hi,

I've been trying and trying but can't get things working like i want to.
I'm trying to get my script connected to a FTP server using a
nonblocking and nonbuffered socket. At the moment I use a while
statement to fetch the data from the socket as it comes in. This works
just fine but the problem is that my script simply comes to a dead end
when the server stops sending data. I need some kind of timer which
'sees' that the server does not send any data and then aborts the while
statement.

while { ![eof $site(control_socket)] } {

if { [gets $site(control_socket) buf] < 0 } {
continue
}
puts $buf
}

My question is; how can I tell the while statement to stop if the server
does not send any data? (e.g. [gets $site(control_socket) buf] gives
me -1 for more than 100 milliseconds in a row)

Thank you for you help,

-Michiel
Darren New

2004-07-28, 9:08 pm

Michiel wrote:
> My question is; how can I tell the while statement to stop if the server
> does not send any data? (e.g. [gets $site(control_socket) buf] gives me
> -1 for more than 100 milliseconds in a row)


Check out [fileevent] and [after]. If you're non-blocking, there's no
real need to sit in a tight loop looking for data to come in, is there?

Basically, read only when data is available, and if no data is available
after some number of miliseconds, abort your program.
Reinhard Max

2004-07-28, 9:08 pm

Hi,

Michiel <storkey@glas.its.tudelft.nl> wrote:

> I'm trying to get my script connected to a FTP server using a
> nonblocking and nonbuffered socket. At the moment I use a while
> statement to fetch the data from the socket as it comes in. This works
> just fine but the problem is that my script simply comes to a dead end
> when the server stops sending data. I need some kind of timer which
> 'sees' that the server does not send any data and then aborts the while
> statement.
>
> while { ![eof $site(control_socket)] } {
>
> if { [gets $site(control_socket) buf] < 0 } {
> continue
> }
> puts $buf
> }


never read from a nonblocking socket in a busy loop,
use the Tcl event loop instead.

Assuming you only have to handle one ftp connection at a time you could do
it like this:

--- snip ---
proc handler {socket} {
global continue

after cancel "set continue timeout"
if {[gets $socket buf] >= 0} {
puts $buf
} elseif {[eof $socket]} {
close $socket
set continue eof
}
after 1000 "set continue timeout"
}

fconfigure $socket -buffering line -blocking 0
fileevent $socket readable [list handler $socket]
vwait continue
puts "reason: $continue"
--- snap ---

This sets up the handler proc as an eventhandler that gets called whenever
data arrives on the socket. It also sets up a timer that fires after one
second and gets refreshed whenever data comes in from the socket.

The script then enters the event loop waiting for the variable "continue"
to change. This happens when either the remote end closes the connection or
one second has passed since the last line of data was received.

Does this help you? If not we might need some more details on your
application to be able to give better advice.

cu
Reinhard
Michiel

2004-07-28, 9:08 pm

Reinhard Max wrote:

> Hi,
>
> Michiel <storkey@glas.its.tudelft.nl> wrote:
>
>
>
>
> never read from a nonblocking socket in a busy loop,
> use the Tcl event loop instead.
>
> Assuming you only have to handle one ftp connection at a time you could do
> it like this:
>
> --- snip ---
> proc handler {socket} {
> global continue
>
> after cancel "set continue timeout"
> if {[gets $socket buf] >= 0} {
> puts $buf
> } elseif {[eof $socket]} {
> close $socket
> set continue eof
> }
> after 1000 "set continue timeout"
> }
>
> fconfigure $socket -buffering line -blocking 0
> fileevent $socket readable [list handler $socket]
> vwait continue
> puts "reason: $continue"
> --- snap ---
>
> This sets up the handler proc as an eventhandler that gets called whenever
> data arrives on the socket. It also sets up a timer that fires after one
> second and gets refreshed whenever data comes in from the socket.
>
> The script then enters the event loop waiting for the variable "continue"
> to change. This happens when either the remote end closes the connection or
> one second has passed since the last line of data was received.
>
> Does this help you? If not we might need some more details on your
> application to be able to give better advice.
>
> cu
> Reinhard


Thank you this already helped quite a lot. However, you mentioned this
would only work with one ftp connection at a time. I'm not quite there
yet ofcourse but eventually I am planning to make the program
multithreaded and handle various FTP connections at the same time. Do
you foresee any problems if I continued using the method you just
described ?

Thanks,

-Michiel
Cameron Laird

2004-07-28, 9:08 pm

In article <4106960e$0$93324$e4fe514c@news.xs4all.nl>,
Michiel <storkey@glas.its.tudelft.nl> wrote:[color=darkred]
>Reinhard Max wrote:
>
Michiel

2004-07-28, 9:08 pm

Cameron Laird wrote:
> In article <4106960e$0$93324$e4fe514c@news.xs4all.nl>,
> Michiel <storkey@glas.its.tudelft.nl> wrote:
>
>
> .
> .
> .
>
>
> .
> .
> .
> I wonder if this is *much* more complicated than necessary;
> Michiel, do you realize tcllib <URL: http://wiki.tcl.tk/tcllib >
> already includes a functional FTP client package that you can
> use immediately <URL: http://wiki.tcl.tk/ftp >?


I've had a brief look at it and it looks like it probably could do the
job but I wanted to take the opportunity to learn more about programming
with sockets and the FTP protocol since I havn't done this before.

-Michiel
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com