UDP broadcast is divided into three types:
All ones-by setting the broadcast address to all ones (255.255.255.255), all hosts on the network receive the broadcast.
Network-by setting the broadcast address to a specific network number in the network portion of the IP address and setting all ones in the host portion of the broadcast address, all hosts on the specified network receive the broadcast. for example, when a broadcast packet is sent with the broadcast address of 131.108.255.255, all hosts on network number 131.108 receive the broadcast.
Subnet-by setting the broadcast address to a specific network number and a specific subnet number, all hosts on the specified subnet receive the broadcast. for example, when a broadcast packet is set with the broadcast address of 131.108.4.255, all hosts on subnet 4 of network 131.108 receive the broadcast.
For more information, see: http://www.cisco.com/univercd/cc/td/doc/ci...k/ics/cs006.htm
1) All ones will be sent to all hosts on the network. If they are on inetnet, this will be a huge burden; generally, the host and router do not support this type.
2) network-oriented/subnet-specific broadcast refers to the UPD broadcast sent to the same network or subnetwork. Most hosts/routers support this broadcast.
3) I wrote an example specifically to help you:
Even in the Appendix A environment, the test passes.
4) Note that the sender and acceptor must be in the same network.
Linux: 192.168.20.73/24
The BSD end uses two addresses: 192.168.20.78/24 and 192.168.20.78/16,
The broadcast address is 192.168.255.255 (broadcast corresponding to the Linux host)
Test passed.
Appendix:
========================================================== ==============
Sender: FreeBSD:
========================================================== ==============
/*
*
*/
# Include <stdio. h>
# Include <string. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Define sock_port 1128
Int main ()
{
Struct sockaddr_in s_addr;
Struct sockaddr_in c_addr;
Int sock;
Int status;
Int addr_len;
Int Len;
Char buff [128];
Int Yes = 1;
Printf ("create socket./N/R ");
Sock = socket (af_inet, sock_dgram, 0 );
If (-1 = sock)
{
Printf ("socket error./N/R ");
Return 1;
}
Setsockopt (sock, sol_socket, so_broadcast, & yes, sizeof (yes ));
/* Setsockopt (sock, sol_socket, so_dontroute, & yes, sizeof (yes ));*/
Memset (& c_addr, 0, sizeof (struct sockaddr_in ));
C_addr.sin_family = af_inet;
C_addr.sin_port = htons (SOCK_PORT-1 );
C_addr.sin_addr.s_addr = inaddr_any;
Printf ("Bind Address to socket./N/R ");
Status = BIND (sock, (struct sockaddr *) & c_addr, sizeof (c_addr ));
If (-1 = Status)
{
Printf ("BIND error./N/R ");
Return 2;
}
S_addr.sin_family = af_inet;
S_addr.sin_port = htons (sock_port );
S_addr.sin_addr.s_addr = inet_addr ("192.168.255.255");/* <--- change the broadcast address here to test */
Printf ("send data.../n/R ");
Addr_len = sizeof (s_addr );
Strcpy (buff, "Hello I'm 192.168.20.78 .");
Len = sendto (sock, buff, strlen (buff), 0,
(Struct sockaddr *) & s_addr, addr_len );
If (LEN <0)
{
Printf ("/n/rsend error./N/R ");
Return 3;
}
Printf ("Send success./N/R ");
Return 0;
}
========================================================== ==============
Receiver Linux:
========================================================== ==============
/*
*
*/
# Include <stdio. h>
# Include <string. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Define sock_port 1128
Int main ()
{
Struct sockaddr_in s_addr;
Struct sockaddr_in c_addr;
Int sock;
Int status;
Int addr_len;
Int Len;
Char buff [128];
Int Yes = 1;
Printf ("create socket./N/R ");
Sock = socket (af_inet, sock_dgram, 0 );
If (-1 = sock)
{
Printf ("socket error./N/R ");
Return 1;
}
/* Setsockopt (sock, sol_socket, so_broadcast, & yes, sizeof (yes ));*/
Memset (& s_addr, 0, sizeof (struct sockaddr_in ));
S_addr.sin_family = af_inet;
S_addr.sin_port = htons (sock_port );
S_addr.sin_addr.s_addr = inaddr_any;
Printf ("% d, % d/n/R", s_addr.sin_family,
Ntohs (s_addr.sin_port ),
S_addr.sin_addr.s_addr,
Sock );
Printf ("Bind Address to socket./N/R ");
Status = BIND (sock, (struct sockaddr *) & s_addr, sizeof (s_addr ));
If (-1 = Status)
{
Printf ("BIND error./N/R ");
Return 2;
}
Printf ("Waiting Recv data.../n/R ");
Addr_len = sizeof (c_addr );
Len = recvfrom (sock, buff, sizeof (buff)-1, 0,
(Struct sockaddr *) & c_addr, & addr_len );
If (LEN <0)
{
Printf ("/n/rrecv error./N/R ");
Return 3;
}
Buff [Len] = '/0 ';
Printf ("/n/rrecvfrom: % s: % d./N/R ",
Inet_ntoa (c_addr.sin_addr), ntohs (c_addr.sin_port ));
Printf ("Buff: % s/n/R", buff );
Return 0;
}