Preface
In network programming, one packet can be sent by the sender through broadcast and multicast, where multiple receivers receive data.
Broadcast
Because TCP is connected, it cannot be used to send broadcast messages. UDP is required for sending broadcast messages. UDP can send messages without establishing a connection. The destination IP address of a broadcast message is a special IP address called a broadcast address. A broadcast address consists of a network prefix and a full host suffix. For example, 192.168.1.255 is the broadcast address of the network 192.169.1.0, and 130.168.255.255 is the broadcast address of the network 130.168.0.0. If a message is sent to an IP address (255.255.255.255.255), all the Internet-connected computers in the world can receive the message theoretically. However, this is not the case. Generally, the packet is discarded on the router and only broadcast in the gossip network. Therefore, the effect is the same as that of sending messages to the broadcast address of the primary network.
The process of sending a broadcast message in C # is as follows. Be sure to call the setsockoption function. Otherwise, an exception will be thrown:
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);//255.255.255.255
IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 9050);
string hostname = Dns.GetHostName();
byte[] data = Encoding.ASCII.GetBytes(hostname);
sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
sock.SendTo(data, iep1);
sock.SendTo(data, iep2);
sock.Close();
The process of receiving broadcast messages in C # is as follows, nothing special:
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
sock.Bind(iep);
EndPoint ep = (EndPoint)iep;
Console.WriteLine("Ready to receive…");
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
data = new byte[1024];
recv = sock.ReceiveFrom(data, ref ep);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
sock.Close();
Attentions:
1. The customer program can notify the service programs in the subnet of their own locations.
2 The specified port also works when sending a broadcast message. The UDP socket of the receiving end can receive the message if BIND is connected to this interface. (If not bind, it can receive messages from all ports ??)
3. The remote IP address displayed in the packet received by the receiver is the address of the sender. That is to say, the broadcast address is not displayed to the source IP address location of a package (the loopback address is not displayed to the destination IP address location of the package)
4. You can use a thread to continuously send and receive broadcast messages, confirm the positions at both ends, and prove your existence.