Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Working with nonblocking sockets
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

Report this thread to moderator Post Follow-up to this message
Old Post
Michiel
07-29-04 02:08 AM


Re: Working with nonblocking sockets
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Darren New
07-29-04 02:08 AM


Re: Working with nonblocking sockets
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

Report this thread to moderator Post Follow-up to this message
Old Post
Reinhard Max
07-29-04 02:08 AM


Re: Working with nonblocking sockets
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 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

Report this thread to moderator Post Follow-up to this message
Old Post
Michiel
07-29-04 02:08 AM


Re: Working with nonblocking sockets
In article <4106960e$0$93324$e4fe514c@news.xs4all.nl>,
Michiel  <storkey@glas.its.tudelft.nl> wrote:
>Reinhard Max wrote:
> 

Report this thread to moderator Post Follow-up to this message
Old Post
Cameron Laird
07-29-04 02:08 AM


Re: Working with nonblocking sockets
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

Report this thread to moderator Post Follow-up to this message
Old Post
Michiel
07-29-04 02:08 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Tcl archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:30 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.