I've written a few programs that use UDP broadcast, this is how I use it
-- client
BROADCAST_IPv4 := net.IPv4(255, 255, 255, 255)
socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
IP: BROADCAST_IPv4,
Port: port,
})
-- server
socket, err := net.ListenUDP("udp4", &net.UDPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: port,
})
for {
data := make([]byte, 4096)
read, remoteAddr, err := socket.ReadFromUDP(data)
}
Cheers
Dave
A potential use case where such behavior could seem counterintuitive
is in a P2P application over UDP, for example. In such a setting, all
peers may wish to initially just listen for incoming packets using
ListenUDP(). The sending of a message is only triggered by (say) user
intervention on a particular host. Call this host A. Then, in this use
case, host A would want to use WriteTo() in order to broadcast
something. The benefit of this approach includes the possibility of
(re)using the socket for other transmissions such as a unicast to a
particular peer in the network if necessary.
Alex
Notes to remember w/r/t broadcast: Your os MUST permit traffic to be
routed to the address (ip route get 255.255.255.255), if it doesn't,
you'll need a more specific broadcast address (network broadcast might
be something like 10.255.255.255 or 192.168.123.255, etc).
I'm binding to a specific interface (udpconn.BindToInterface) which
(IIRC my C) makes broadcasts easier to accomplish on linux.
You still have to bind to a local address (0.0.0.0 is fine, but the
responder must broadcast as well if your client has no IP.)
If you need more ptrs, I'll try and work up some test cases, but
hopefully something here might jog a successful broadcast for you.
-James
that's an interesting statement. have you got a reference for it?
oh, i'm sorry, your statement seemed to refer to UDP in general,
not particularly UDP broadcast.
Still, why are you resolving broadcast?
--- ../foo_test.go 2011-01-14 12:47:12.000000000 -0800
+++ t_test.go 2011-01-14 12:46:14.000000000 -0800
@@ -14,13 +14,9 @@
c := make(chan bool)
go respond(t, responder)
go receive(t, requester, c)
- addr, err := ResolveUDPAddr(net.IPv4bcast.String(), 8200)
- if err != nil {
- t.Fatalf("Cannot resolve broadcast address: %s", err)
- }
request := []byte("Some request")
- _, err = requester.WriteTo(request, addr)
+ _, err := requester.WriteTo(request, &net.UDPAddr{IP:net.IPv4bcast,
Port:8200})
if err != nil {
t.Fatalf("Unable to respond to request: %s", err)
}
@@ -71,3 +67,4 @@
return conn
}
+
passes on my system.
- Double check that 'ip route get 255.255.255.255' works, and does
what you think it should.
- tcpdump both the routed interface (above) and the interface you
think it should be on, see which one actually gets the traffic
- tcpdump from another machine on the same physical subnet if possible
- check iptables & firewalling
I've gone ahead and published my GoDHCP[1] library ahead of time, as
this does a decent amount of work to get broadcast on (unbound)
interfaces correct without raw sockets, and might help you. While
it has no tests, and produces no stand-alone (normally), there is a
test_agent.go (you'll need to change the IFNAME and possibly the
HWADDR used to make it useful) that should at least work if you're on
any interface that doesn't have an existing dhclient. (It doesn't do
anything to your local host, simply does the DHCP negotiation, but an
existing client will probably catch at least some of the negotiation
replies).
Give it a try if you can, it's fairly well error-catching, and I'd
like to see what it spits out if nothing else in your environment.
[1] https://github.com/abneptis/GoDHCP
func main() {
port := 8001
fmt.Println("Listening for UDP Broadcast Packet")
socket, err := net.ListenUDP("udp4", &net.UDPAddr{
IP: net.IPv4(0, 0, 0, 0),
//IP: net.IPv4( 192, 168, 1, 255 ),
Port: port,
})
if err != nil {
fmt.Println("Error listen: " , err)
}
for {
data := make([]byte, 4096)
read, remoteAddr, err := socket.ReadFromUDP(data)
if err != nil {}
fmt.Println("readfromudp: ", err)
}
for i :=0; i<read; i++{
fmt.Println(data[i])
}
fmt.Printf("Read from: %v\n", remoteAddr)
}