I'm very late to respond to this but it still seems to be a very minimal 
discussed topic.

I am trying to bind to a specific Interface (i have 4 on my server) and 
cannot use 0.0.0.0.
But when i try to use any other form of addr such as the enclosed 
192.168.1.255 (class C) it runs but i never get messages from client 
broadcast 255.255.255.255 ??

Anyone have example like James mentioned on bindToAddress ?? 


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)
   }

}

On Thursday, January 13, 2011 at 1:57:49 AM UTC-5, James Nurmi wrote:
>
> A little late to the game, but I have successfully used listenudp, but
> I'm explicitly using readfromudp, rather than readfrom (sorry, my
> api-docs are away from me).  I'm also doing this as root (low port #),
> so I have 'extra' permissions when testing.
>
> 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
>
> On Wed, Jan 12, 2011 at 1:31 PM, Alex Horn <alex...@gmail.com 
> <javascript:>> wrote:
> > Based on Dave's generous feedback, I have been able to verify that I
> > can now successfully broadcast a message provided that I open the
> > socket with DialUDP() instead of ListenUDP(). Initially, I was under
> > the impression that I could also simply open the socket with
> > ListenUDP() and use WriteTo() with a broadcast address. Instead, the
> > suggestion involving DialUDP() and Write() seems to be the only way to
> > broadcast a message. Is this the intentional library behavior?
> >
> > 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
> >
> > On 12 January 2011 20:28, Dave Cheney <da...@cheney.net <javascript:>> 
> wrote:
> >> Hi Alex,
> >>
> >> 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
> >>
> >> On Thu, Jan 13, 2011 at 6:53 AM, Mr Horn <alex...@gmail.com 
> <javascript:>> wrote:
> >>> I am successfully using unicast with the net package's ListenUDP(),
> >>> WriteTo() and ReadFrom() API [1]. However, when I switch to broadcast
> >>> addresses such as 255.255.255.255 and the correct port number, the
> >>> receiver does not pick up the messages anymore.
> >>>
> >>> Has anyone run into similar issues?
> >>>
> >>> To investigate the problem, I have looked at fd.go [2] where the flags
> >>> for the Recvfrom syscall [3] are zero. Is it possible that the
> >>> receiving of broadcast messages should be explicitly enabled as part
> >>> of the system call?
> >>>
> >>> Cheers,
> >>> Alex
> >>>
> >>> [1] http://golang.org/pkg/net/#UDPConn
> >>> [2] http://golang.org/src/pkg/net/fd.go#L371
> >>> [3] http://golang.org/pkg/syscall/#Recvfrom
> >>>
> >>
> >
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to