UDP broadcast is actually very simple. When I understand the IP address resolution scheme, I understand what broadcast is. Many articles call it multicast. I think it is better to call it broadcast, because multicasting refers to broadcasting and multicast, the processing in Java programs is naturally different. This article and the next article only emphasize the concept.
As mentioned above, the broadcast address includes the targeted broadcast address and the limited broadcast address. The TCP/IP protocol stipulates that the two addresses can only appear in the local network, and the router implements this rule, instead of forwarding packets whose target address is a broadcast address to other networks, multicast is different. The next article introduces multicast programming.
This broadcast program can simply become a broadcast program by modifying the target address of the message in the unicast program into a broadcast address.
Package recmail. multiservice;
Import java.net. DatagramSocket;
Import java.net .*;
Import java. io .*;
Import java. util. ArrayList;
Import java. util. Iterator;
Public class extends ramrecvsender_ B extends DataSwapListenerAdapter
Implements Runnable {
Public static final int DatagramPort = 10698;
Private java.net. DatagramSocket road;
Private InetAddress ia;
DatagramPacket dp;
Public static final int Datagram_Length = 8192;
Private DataPacket cdp;
Private boolean ishandling = true;
DataSwapEvent dsevent;
/**
* This constructor is used to receive data packets.
*/
Public DatagramRecvSender_ B (){
Try {
System. out. println ("recv ...");
Dsevent = new DataSwapEvent (this );
SocketAddress sa = new InetSocketAddress (
DatagramRecvSender_ B .DatagramPort );
// Specify at least the datagram receiving port, which is the only requirement on the receiving end. Of course, if the host is a multi-access host, road = new DatagramSocket (sa) is required );
Road = new DatagramSocket (datagramrecvsender_ B .20.ramport );