Home > Archive > Tcl > October 2005 > detecting network connection and tcludp
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 |
detecting network connection and tcludp
|
|
| shamil 2005-10-23, 7:56 am |
| Hello,
I'm trying to use tcludp to send data and also trying to detect when
network connection is lost by periodically checking for variable
"connect" and re-setting it. If its value is 1, network connection
exists, otherwise it got lost or the plug was pulled. A small message
on a label should indicate if data is being sent or not. In case no
connection, I'm looping every second closing previous socket, opening a
new one, do fconfigure, and fileevent (all this in proc
startBroadcast).
However, in the proc loop below, the line:
puts -nonewline $sock {whatever I want to send...}
for some reason is not invoking proc receiveBroadcast! Which should be
invoked every time data is put into the socket (as defined by the
fileevent). Could it be I didn't properly define my fileevent? Or what
could it be? Any help would be greatly appreciated.
Shamil
#--------------- Begin Script ----------------#
package require udp
pack [label .l -text {display status}]
#############################
proc startBroadcast {} {
variable connected
variable address 224.1.1.1
variable port 1234
variable sock
catch {close $sock} ;# close previous socket, if exists. This is to
;# prevent from crashing while trying again to
;# connect again
set sock [udp_open $port]
if {[catch {fconfigure $sock -blocking 0 -buffering none -translation
binary -mcastadd $address -remote [list $address $port]}]} {
set connected 0
puts "fconfigure failed"
.l configure -text "Lost connection. Current time is \
[clock format [clock seconds] -format %r]"
} else {set connected 1
puts "fconfigure passed"}
fileevent $sock readable receiveBroadcast
}
##############################
proc receiveBroadcast {} {
variable sock
variable connected
puts "received data: [read $sock]"
set connected 1
..l configure -text "Sending...[clock format [clock seconds] -format
%r]"
return
}
##############################
proc loop {} {
variable connected
variable sock
set connected 0
puts -nonewline $sock {whatever I want to send...}
;# at this point, I would've thought "connected" = 1 as long as
;# network connection exists because it would be set from the
;# receiveBroadcast procedure. But this is not the case. WHY??
if {!$connected} {startBroadcast} ;# if no connection, try again
after 1000 loop
}
#############################
startBroadcast
loop
#--------------- End Script ----------------#
| |
| Gerald W. Lester 2005-10-23, 7:56 am |
| I do not believe the receive fileevent will fire for things you yourself
send out on the port.
shamil wrote:
> Hello,
>
> I'm trying to use tcludp to send data and also trying to detect when
> network connection is lost by periodically checking for variable
> "connect" and re-setting it. If its value is 1, network connection
> exists, otherwise it got lost or the plug was pulled. A small message
> on a label should indicate if data is being sent or not. In case no
> connection, I'm looping every second closing previous socket, opening a
> new one, do fconfigure, and fileevent (all this in proc
> startBroadcast).
>
> However, in the proc loop below, the line:
>
> puts -nonewline $sock {whatever I want to send...}
>
> for some reason is not invoking proc receiveBroadcast! Which should be
> invoked every time data is put into the socket (as defined by the
> fileevent). Could it be I didn't properly define my fileevent? Or what
> could it be? Any help would be greatly appreciated.
>
> Shamil
>
>
>
> #--------------- Begin Script ----------------#
> package require udp
>
> pack [label .l -text {display status}]
>
> #############################
> proc startBroadcast {} {
>
> variable connected
> variable address 224.1.1.1
> variable port 1234
> variable sock
>
> catch {close $sock} ;# close previous socket, if exists. This is to
> ;# prevent from crashing while trying again to
> ;# connect again
>
> set sock [udp_open $port]
>
> if {[catch {fconfigure $sock -blocking 0 -buffering none -translation
> binary -mcastadd $address -remote [list $address $port]}]} {
> set connected 0
> puts "fconfigure failed"
> .l configure -text "Lost connection. Current time is \
> [clock format [clock seconds] -format %r]"
>
> } else {set connected 1
> puts "fconfigure passed"}
>
> fileevent $sock readable receiveBroadcast
> }
>
> ##############################
> proc receiveBroadcast {} {
>
> variable sock
> variable connected
>
> puts "received data: [read $sock]"
> set connected 1
> .l configure -text "Sending...[clock format [clock seconds] -format
> %r]"
> return
> }
>
> ##############################
> proc loop {} {
> variable connected
> variable sock
>
> set connected 0
> puts -nonewline $sock {whatever I want to send...}
>
> ;# at this point, I would've thought "connected" = 1 as long as
> ;# network connection exists because it would be set from the
> ;# receiveBroadcast procedure. But this is not the case. WHY??
>
> if {!$connected} {startBroadcast} ;# if no connection, try again
>
> after 1000 loop
> }
> #############################
>
> startBroadcast
> loop
> #--------------- End Script ----------------#
>
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
| |
| shamil 2005-10-23, 6:59 pm |
| I'm affraid I don't understand... what other options do I have in
sending data through a socket?
| |
|
| shamil wrote:
> I'm affraid I don't understand... what other options do I have in
> sending data through a socket?
>
you misunderstand the response.
you are correctly sending data through the socket.
but a readable fileevent is used for *reading* data from a socket.
to keep your flag current, check the return code from your puts
statement and see if it fails. If it succeeds you are connected.
if you have a slow link and/or sending so much that buffers can
get full and cause unnecessary blocking, then you can use a writable
fileevent to notify you when the socket is available for writing.
Bruce
| |
| shamil 2005-10-25, 9:57 pm |
| Thank you Gerald and Bruce.
My problem was that I was incorrectly expecting my variable "connected"
to be instantly updated from proc receiveBroadcast upon invoking "puts
-nonewline $sock..." from proc loop. This case proved true even when
the data I was sending was very small
I suppose one should not expect the fileevent handler to fire up the
very moment data is put into a socket.
|
|
|
|
|