From: Charles McGarvey Date: Thu, 13 May 2010 16:51:13 +0000 (-0600) Subject: better broadcast support X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=61e441a6b4b7c4b2b14b12a1a2545a04155c719b;p=chaz%2Fyoink better broadcast support --- diff --git a/src/Main.cc b/src/Main.cc index c6df957..4d88a70 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -280,7 +280,7 @@ void goodbye() int main(int argc, char* argv[]) { - Mf::ResolverTask task("4950", "255.255.255.255", SOCK_DGRAM); + Mf::ResolverTask task("4950", "compy", SOCK_DGRAM); task.run(); int i = task.wait(); @@ -335,27 +335,26 @@ int main(int argc, char* argv[]) Mf::logInfo << "packet size: " << packet.size() << std::endl; - Mf::SocketAddress addr("634", "lappy"); - Mf::logInfo << "local addr: " << addr.name() << std::endl; + Mf::SocketAddress addr = Mf::SocketAddress::broadcast("4950"); + //Mf::logInfo << "local addr: " << addr.name() << std::endl; - for (it = task.addresses().begin(); it != task.addresses().end(); ++it) - { + //for (it = task.addresses().begin(); it != task.addresses().end(); ++it) + //{ int bcast = 0; - Mf::SocketAddress addr = *it; + //Mf::SocketAddress addr = *it; Mf::Socket sock(addr); sock.get(SO_BROADCAST, bcast); Mf::logInfo << "bcast: " << bcast << std::endl; - sock.set(SO_REUSEADDR, 1); sock.set(SO_BROADCAST, 1); sock.get(SO_BROADCAST, bcast); Mf::logInfo << "bcast: " << bcast << std::endl; sock.write(packet); - } + //} return 0; diff --git a/src/Moof/Socket.hh b/src/Moof/Socket.hh index f160466..6b0fa88 100644 --- a/src/Moof/Socket.hh +++ b/src/Moof/Socket.hh @@ -90,7 +90,16 @@ public: static SocketAddress broadcast(const std::string& service) { - return SocketAddress(service, "255.255.255.255", SOCK_DGRAM); + std::istringstream stream(service); + unsigned short port; + stream >> port; + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = INADDR_BROADCAST; + memset(&addr.sin_zero, 0, sizeof(addr.sin_zero)); + return SocketAddress((sockaddr*)&addr, sizeof(addr), SOCK_DGRAM); }