Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageMichiel 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.
Post Follow-up to this messageHi,
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
Post Follow-up to this messageReinhard 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 o
r
> 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
Post Follow-up to this messageIn article <4106960e$0$93324$e4fe514c@news.xs4all.nl>, Michiel <storkey@glas.its.tudelft.nl> wrote: >Reinhard Max wrote: >
Post Follow-up to this messageCameron 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.