Keywords: Linux broadcast 255.255.255.255
SendTo Error:network is unreachable
Full network broadcast
Scene:
Today, when you debug a broadcast on Linux network programming, when you send a broadcast packet to a port on 255.255.255.255, SendTo returns-1 for the wrong reason:
SendTo Error:network is unreachable.
Workaround:
1. Programming Solution:
struct sockaddr_in localaddr;
Execute SOCKFD = Socket (af_inet, SOCK_DGRAM, 0); Add the following instruction after
SetSockOpt (SOCKFD, Sol_socket, So_broadcast, &sock_flag, sizeof (Sock_flag));
Bzero (&localaddr, sizeof (LOCALADDR));
localaddr.sin_family = af_inet;
Inet_pton (Af_inet, "192.168.1.125", &localaddr.sin_addr); 192.168.1.125 is the address of a local network card
Localaddr.sin_port = htons (12345);
Bind (SOCKFD, (SA *) &localaddr, sizeof (LOCALADDR));
2. Directive Solution:
Oute add-net 255.255.255.255 netmask 255.255.255.255 dev eth0 metric 1
Or
Route add-host 255.255.255.255 Dev eth0