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_D GRAM,0);
dest_addr.sin_family=AF_IN ET;
dest_addr.sin_port=htons(5 051);
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_add r,sizeof(s truct 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
int sckt;
struct sockaddr_in dest_addr;
sckt=socket(AF_INET,SOCK_D
dest_addr.sin_family=AF_IN
dest_addr.sin_port=htons(5
inet_aton("192.168.1.255",
memset (&(dest_addr.sin_zero),'\0
if (sendto (sckt,"test",4,0,&dest_add
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
if( bind (sck, (void *)&dest_addr, sizeof(dest_addr)) == -1)
perror( "bind" );
Then do the setsockopt line.
ASKER
Sorry, but that didn't help.
Still get the same error Bad Address.
Still get the same error Bad Address.
ASKER
Actually it works by setting an int variable to 1 and passing its pointer in the function instead of 1.
Thanks
jw
Thanks
jw
ASKER
perror on this function returns Bad Address