1

When binding 2 event handlers on 2 different UDP sockets on the same UDP port, one configured for multicast, one configured for unicast, both event handlers are fired up when a multicast packet is received, which is not expected behaviour. When an unicast packet is received, only the unicast handler is fired up, which is expected behaviour.

What could be wrong in my code ?

package require udp
package require logger

set log [logger::init m5140]
proc logger::tree::m5140::stdoutcmd {level text} {
        variable service
        puts "\[[clock format [clock seconds] -format {%Y%m%d-%H%M%S}]\] \[$service\] \[$level\] \'$text\'"
    }
${log}::notice "[info script] Started"
set group 224.1.5.14
set port   5140

Unicast 
    proc mcastEvent {chan} {
        variable mcount
        incr mcount
        lassign [chan configure $chan -peer] host port
        set data [read $chan]   
        if { $data ne {} } {
            incr mcount
            after idle ${::log}::notice [list [format {multicast (%08d) from %s |%s| } $mcount $host $data]]
        }
    }
    
    proc ucastEvent {chan} {
        variable ucount
        incr ucount
        set data [read $chan]   
        lassign [chan configure $chan -peer] host port
        if { $data ne {} } { 
            incr ucount
            after idle ${::log}::notice [list [format {unicast (%08d) from %s |%s| } $ucount $host $data]] 
        }
    }
    
    ######################## MULTICAST ###################################
    # Create a listening Multicast socket
    set msock [udp_open $port reuse]
    ${log}::notice "[info script] mcast_port $msock"
    chan configure  $msock -buffering line -blocking 0 -encoding utf-8 \
                -mcastadd $group -remote [list $group $port]
    
    ######################## UNICAST ###################################
    # Create a listening Unicast socket
    set usock [udp_open $port reuse]
    ${log}::notice "[info script] ucast_port $port"
    chan configure  $usock -buffering line -blocking 0 -encoding utf-8 
    
    
    # Start multicast event handler
    fileevent $msock readable [list mcastEvent $msock]
    
    # Start Unicast event handler            
    fileevent $usock readable [list ucastEvent $usock]

dbush
  • 205,898
  • 23
  • 218
  • 273
M1larepa
  • 21
  • 1
  • What are the OS and TclUDP version used? – mrcalvin Aug 03 '22 at 08:24
  • Hello, OS is Debian GNU/Linux 11 (bullseye) – M1larepa Aug 07 '22 at 20:47
  • (Tcl8.6.11 / Tk8.6.11) package require udp 1.0.11 – M1larepa Aug 07 '22 at 20:48
  • I'm pretty sure that the OS only normally allows one socket to have a particular address bound to it (considering IP and port for *both* ends of the socket) but I'm not sure how multicast interacts with that; I've only ever written code with unicast sockets. – Donal Fellows Aug 10 '22 at 07:52
  • Hi Donal, well, I agree with you, but lsof only show one line with *::5140. This may be normal, as the lsof entry is just the result of [udp_open $port] Further fconfigure commands does not change this... – M1larepa Aug 10 '22 at 09:52

0 Answers0