Link to home
Start Free TrialLog in
Avatar of wilch101
wilch101

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of jcaldwel
jcaldwel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wilch101
wilch101

ASKER

setsockopt(sck, SOL_SOCKET, SO_BROADCAST, 1)

perror on this function returns Bad Address

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.

Sorry, but that didn't help.  

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

Thanks
jw