From: cerr on
[snip]

My code now looks like this:

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;
}
if(setsockopt(BrdcstSock,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(opt))){
cerr << "Error setsockopt():" << strerror(errno) << endl;
return;
}
memset(&addr,0x00,sizeof(addr));

addr.sin_family = AF_INET;

addr.sin_addr.s_addr = inet_addr("192.168.101.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;
}
else
{
cout << "sent:" << message << endl;
}
sleep(3);
}

This compiles and runs nicely but i don't see any packets on port 6767
with Wireshark... :( I still see "sent: Hello, World!" appearing on my
shell but then again, I get the same screen ouput when i disconnect
the physical connection, why does this not work, any suggestions where
i'm missing out?

Thank you!
Ron
From: Andrew Poelstra on
On 2010-02-02, cerr <ron.eggler(a)gmail.com> wrote:
> [snip]
>
> My code now looks like this:
>
> 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;
> }
> if(setsockopt(BrdcstSock,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(opt))){
> cerr << "Error setsockopt():" << strerror(errno) << endl;
> return;
> }
> memset(&addr,0x00,sizeof(addr));
>
> addr.sin_family = AF_INET;
>
> addr.sin_addr.s_addr = inet_addr("192.168.101.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;
> }
> else
> {
> cout << "sent:" << message << endl;
> }
> sleep(3);
> }
>
> This compiles and runs nicely but i don't see any packets on port 6767
> with Wireshark... :( I still see "sent: Hello, World!" appearing on my
> shell but then again, I get the same screen ouput when i disconnect
> the physical connection, why does this not work, any suggestions where
> i'm missing out?
>
> Thank you!
> Ron

This is a silly question, but are you sure you aren't broadcasting
only on one interface? Say, the loopback interface?

Less silly, can you send directed packets?

Does strace give you any useful information?


From: Peter van Hooft on
On 2010-02-03, Andrew Poelstra <apoelstra(a)localhost.localdomain> wrote:
> On 2010-02-02, cerr <ron.eggler(a)gmail.com> wrote:
>> [snip]
>>
>> My code now looks like this:
>>
>> 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;
>> }
>> if(setsockopt(BrdcstSock,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(opt))){
>> cerr << "Error setsockopt():" << strerror(errno) << endl;
>> return;
>> }
>> memset(&addr,0x00,sizeof(addr));
>>
>> addr.sin_family = AF_INET;
>>
>> addr.sin_addr.s_addr = inet_addr("192.168.101.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;
>> }
>> else
>> {
>> cout << "sent:" << message << endl;
>> }
>> sleep(3);
>> }
>>
>> This compiles and runs nicely but i don't see any packets on port 6767
>> with Wireshark... :( I still see "sent: Hello, World!" appearing on my
>> shell but then again, I get the same screen ouput when i disconnect
>> the physical connection, why does this not work, any suggestions where
>> i'm missing out?
>>
>> Thank you!
>> Ron
>
> This is a silly question, but are you sure you aren't broadcasting
> only on one interface? Say, the loopback interface?
>
> Less silly, can you send directed packets?
>
> Does strace give you any useful information?
>
>

Perhaps you need to be root to send to broadcast addresses?


peter

From: Andrew Poelstra on
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

From: Peter van Hooft on
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
>

You're right. I tested this on my (linux) laptop, and it indeed seems to
work as a normal user.
What platform didn't this work on?

peter