proc udpEvent {chan} {
set data [read $chan]
set peer [fconfigure $chan -peer]
- puts "$peer [string length $data] '$data'"
+ set group [lindex [fconfigure $chan -remote] 0]
+ puts "$peer ($group) [string length $data] '$data' {[fconfigure $chan]}"
if {[string match "QUIT*" $data]} {
close $chan
set ::forever 1
}
# Select a multicast group and the port number.
-set group 224.5.1.21
-set port 7771
+#
+# We have two groups here to show that it's possible.
+#
+set group1 224.5.1.21
+set group2 224.5.2.21
+set port 7771
# Create a listening socket and configure for sending too.
set s [udp_open $port]
fconfigure $s -buffering none -blocking 0
-fconfigure $s -mcastadd $group -remote [list $group $port]
+fconfigure $s -mcastadd $group1 -remote [list $group1 $port]
+fconfigure $s -mcastadd $group2 -remote [list $group2 $port]
fileevent $s readable [list udpEvent $s]
# Announce our presence and run
CONST84 char *optionName, Tcl_DString *optionValue)
{
UdpState *statePtr = (UdpState *)instanceData;
- CONST84 char * options = "myport remote peer mcastgroups broadcast ttl";
+ const char * options[] = { "myport", "remote", "peer", "mcastgroups", "broadcast", "ttl", NULL};
int r = TCL_OK;
if (optionName == NULL) {
-
- Tcl_DStringAppend(optionValue, " -myport ", -1);
- udpGetOption(instanceData, interp, "-myport", optionValue);
- Tcl_DStringAppend(optionValue, " -remote ", -1);
- udpGetOption(instanceData, interp, "-remote", optionValue);
- Tcl_DStringAppend(optionValue, " -peer ", -1);
- udpGetOption(instanceData, interp, "-peer", optionValue);
- Tcl_DStringAppend(optionValue, " -mcastgroups ", -1);
- udpGetOption(instanceData, interp, "-mcastgroups", optionValue);
- Tcl_DStringAppend(optionValue, " -broadcast ", -1);
- udpGetOption(instanceData, interp, "-broadcast", optionValue);
- Tcl_DStringAppend(optionValue, " -ttl ", -1);
- udpGetOption(instanceData, interp, "-ttl", optionValue);
+ Tcl_DString ds;
+ const char **p;
+
+ Tcl_DStringInit(&ds);
+ for (p = options; *p != NULL; p++) {
+ char op[16];
+ sprintf(op, "-%s", *p);
+ Tcl_DStringSetLength(&ds, 0);
+ udpGetOption(instanceData, interp, op, &ds);
+ Tcl_DStringAppend(optionValue, " ", 1);
+ Tcl_DStringAppend(optionValue, op, -1);
+ Tcl_DStringAppend(optionValue, " ", 1);
+ Tcl_DStringAppendElement(optionValue, Tcl_DStringValue(&ds));
+ }
} else {
}
} else {
- r = Tcl_BadChannelOption(interp, optionName, options);
+ CONST84 char **p;
+ Tcl_DString tmp;
+ Tcl_DStringInit(&tmp);
+ for (p = options; *p != NULL; p++)
+ Tcl_DStringAppendElement(&tmp, *p);
+ r = Tcl_BadChannelOption(interp, optionName, Tcl_DStringValue(&tmp));
+ Tcl_DStringFree(&tmp);
}
if (r == TCL_OK) {
- Tcl_DStringAppendElement(optionValue, Tcl_DStringValue(&ds));
+ Tcl_DStringAppend(optionValue, Tcl_DStringValue(&ds), -1);
}
Tcl_DStringFree(&dsInt);
Tcl_DStringFree(&ds);