• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
UDP Broadcasts Don't Work
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

UDP Broadcasts Don't Work


  • Subject: UDP Broadcasts Don't Work
  • From: Brian Barnes <email@hidden>
  • Date: Sat, 18 Aug 2007 17:14:53 -0400

This has to be something simple because I know other programs successfully do this.  I have a server/client, and the client does a broadcast of a small message to find servers.

The code is simple, open a UDP datagram socket, set the SO_BROADCAST flag, and then use sendto on INADDR_BROADCAST.  No machine (including itself) on my small network gets the packet.  Again, other programs do this successfully on my network, so it's got to be my bug instead of some network setup issue.

Weird thing -- localhost doesn't work either.  BUT, if I use the direct IP of the server, everything works perfectly.  So the socket code seems fine, it's just 255.255.255.255 that's the problem.

Simple code:

bool network_udp_send_broadcast(int port)
{
int opt,err;
d3socket sock;
struct sockaddr_in addr;


// open socket and allow broadcasting


sock=socket(AF_INET,SOCK_DGRAM,0)
if (sock==D3_NULL_SOCKET) return(FALSE);


opt=1;
setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(int));


// send broadcast


memset(&addr,0x0,sizeof(struct sockaddr_in));


addr.sin_family=AF_INET;
addr.sin_port=htons((short)port);
addr.sin_addr.s_addr=INADDR_BROADCAST; // 255.255.255.255, put the direct server IP instead and it works fine


err=sendto(sock,"dim3",4,0,(struct sockaddr*)&addr,sizeof(struct sockaddr_in)); // send simple packet, the return IP is the important part

network_close_socket(&sock);


return(err==4);
}

Note that works WITHOUT error even though no data seems to be broadcast.  What have I done wrong here?  Any ideas?

[>] Brian
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: UDP Broadcasts Don't Work
      • From: "Peter Sichel" <email@hidden>
  • Prev by Date: Re: Developing drivers for a USB-based network device. . .?
  • Next by Date: NSURLDownload - multiple requests not working
  • Previous by thread: How to bind to a specific port in NKE?
  • Next by thread: Re: UDP Broadcasts Don't Work
  • Index(es):
    • Date
    • Thread