Keyword: Linux broadcast 255.255.255.255
Sendto error: network is unreachable
Full-network broadcast
Scenario:
Debug the broadcast of Linux network programming today. When a broadcast packet is sent to a port of 255.255.255.255, sendto returns-1. The error is caused:
Sendto error: network is unreachable.
Solution:
1. Programming solution:
Struct sockaddr_in localaddr;
Execute sockfd = socket (af_inet, sock_dgram, 0); then add the following command
Setsockopt (sockfd, sol_socket, so_broadcast, & sock_flag, sizeof (sock_flag ));
Bzero (& localaddr, sizeof (localaddr ));
Localaddr. sin_family = af_inet;
Inet_ton (af_inet, "192.168.1.125", & localaddr. sin_addr); // 192.168.1.125 is the address of a local Nic.
Localaddr. sin_port = htons (12345 );
BIND (sockfd, (Sa *) & localaddr, sizeof (localaddr ));
2. Instructions:
Oute add-net plugin 255.255.255.255 netmask 255.255.255.255 Dev eth0 metric 1
Or
Route add-host route 255.255.255 Dev eth0
End