• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1782
  • Last Modified:

sendto() broadcast datagram packet

I am trying to broadcast a simple datagram packet over the network using the sendto() function on a linux box.

        int sckt;
     struct sockaddr_in dest_addr;
     sckt=socket(AF_INET,SOCK_DGRAM,0);
     dest_addr.sin_family=AF_INET;
     dest_addr.sin_port=htons(5051);
     inet_aton("192.168.1.255",&dest_addr.sin_addr.s_addr);
     memset (&(dest_addr.sin_zero),'\0',8);
        if (sendto (sckt,"test",4,0,&dest_addr,sizeof(struct sockaddr_in))==-1)
          perror ("sendto");


perror returns Permission Denied

The above code works if I send it to just one address, but not with a broadcast address.

How can I do a broadcast

Thanks
jw
0
wilch101
Asked:
wilch101
  • 3
  • 2
1 Solution
 
jcaldwelCommented:
Try the following:

setsockopt(sck, SOL_SOCKET, SO_BROADCAST, 1)
0
 
wilch101Author Commented:
setsockopt(sck, SOL_SOCKET, SO_BROADCAST, 1)

perror on this function returns Bad Address

0
 
jcaldwelCommented:
Sorry, I believe you have to bind the socket for this to work:

 if( bind (sck, (void *)&dest_addr, sizeof(dest_addr)) == -1)
    perror( "bind" );

Then do the setsockopt line.

0
 
wilch101Author Commented:
Sorry, but that didn't help.  

Still get the same error Bad Address.
0
 
wilch101Author Commented:
Actually it works by setting an int variable to 1 and passing its pointer in the function instead of 1.

Thanks
jw
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now