Prev: UDP broadcast
Next: Create staple diagrams from a text file (containing numbers)from command line?
From: Peter van Hooft on 4 Feb 2010 14:28 On 2010-02-03, Andrew Poelstra <apoelstra(a)localhost.localdomain> wrote: > On 2010-02-03, Peter van Hooft <pjvh(a)xs4all.nl> wrote: >> >> Perhaps you need to be root to send to broadcast addresses? >> >> peter >> > > I thought of this, but given that he successfully opened the > socket, I assumed that he was root. In my experience socket() > will fail if you pass it SO_BROADCAST and are not root. > > Andrew > I tried this on solaris and hp-ux as well, and there it also works. Since the code supplied didn't compile with g++, I changed it to #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <errno.h> #include <string.h> #include <string> #include <iostream> using namespace std; int main() { struct sockaddr_in addr; int BrdcstSock; int opt=1; string message = "Hello, World!"; if ((BrdcstSock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { cerr << "PIDClient: Error creating socket:"<< strerror(errno) << endl; return -1; } if(setsockopt(BrdcstSock,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(opt))){ cerr << "Error setsockopt():" << strerror(errno) << endl; return -1; } memset(&addr,0x00,sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("192.168.2.255"); addr.sin_port=htons(6767); while (1) { if (sendto(BrdcstSock, message.c_str(), strlen(message.c_str()), 0, (struct sockaddr *) &addr, sizeof(addr)) != message.size()){ cerr << "Error sendto():" << strerror(errno) << endl; return -1; } else { cout << "sent:" << message << endl; } sleep(3); } return 0; } and this works on my (linux) laptop, running as a normal user. Could it be that, agains the rules, OP is running with something different than 192.168.101.0/24? peter
First
|
Prev
|
Pages: 1 2 3 Prev: UDP broadcast Next: Create staple diagrams from a text file (containing numbers)from command line? |