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 |