LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Why i am not able to send the broadcast packets in my home network (https://www.linuxquestions.org/questions/linux-networking-3/why-i-am-not-able-to-send-the-broadcast-packets-in-my-home-network-4175464354/)

ManikantaVelaga 06-01-2013 09:30 AM

Why i am not able to send the broadcast packets in my home network
 
Hi,
I have 2 linux machines connected in a LAN.
Both machines are getting IP addresses from Router.

Configuration is below:-
1st Machine IP: 192.168.1.2
2nd Machine IP: 192.168.1.3
Router IP: 192.168.1.1

In my running below program in 1st machine, but sendto() call is failing and returning -1 with error message: Permission Denied.

Why i am getting this error message eventhough i am executing this program as a root user?

Please help me.

PROGRAM IS:
int main()
{
int fd;
struct sockaddr_in TargetAddress, ItsAddress;
char SendBuff[10] = "Hello";
int sentbytes;

fd = socket(AF_INET, SOCK_DGRAM, 0);
perror("socket():");

ItsAddress.sin_family = AF_INET;
ItsAddress.sin_port = htons(8888);
ItsAddress.sin_addr.s_addr = inet_addr("192.168.1.2");
bind(fd, (struct sockaddr*)&ItsAddress, sizeof(ItsAddress));
perror("bind()");


TargetAddress.sin_family = AF_INET;
TargetAddress.sin_port = htons(9998);
TargetAddress.sin_addr.s_addr = inet_addr("255.255.255.255");

sentbytes = sendto(fd, SendBuff, 100, 0, (struct sockaddr*)&TargetAddress, sizeof(struct sockaddr_in));
perror("sendto");
printf(" sent bytes are: %d \n", sentbytes);
}


Thanks for your time
Manikanta Velaga

ManikantaVelaga 06-01-2013 09:56 AM

Yes, i got the solution to this problem, i have to set the socket option : SO_BROADCAST like below. Now i am able to send the broadcast packets.

int OptVal = 1;
if(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &OptVal, sizeof(OptVal)) == -1)
{
perror("setsockopt() is failed: \n");
}


Thanks to all readers
Manikanta Velaga


All times are GMT -5. The time now is 10:05 PM.