using MulticastSocket to achieve multipoint broadcasting
Datagramsocket only allows datagrams to be sent to the specified destination address, while MulticastSocket can broadcast datagrams to multiple clients of varying amounts.
Let the class implement the Runnable interface, where an instance of the class can be used as the target of the thread public class Multicastsockettest implements Runnable { Use constants as multicast IP addresses for this program private static final String broadcast_ip = "230.0.0.1"; Use constants as ports for the multi-point broadcast purpose of this program public static final int broadcast_port = 30000; Define a maximum size of 4K per datagram private static final int data_len = 4096; Define MulticastSocket instances of this program Private MulticastSocket socket = null; Private inetaddress broadcastaddress = null; Private Scanner scan = null; Defines a byte array to receive network data byte[] Inbuff = new Byte[data_len]; Creates a Datagrampacket object that prepares to accept data in a specified byte array Private Datagrampacket Inpacket = New Datagrampacket (Inbuff, inbuff.length); Defines a Datagrampacket object for sending Private Datagrampacket outpacket = null; public void Init () throws IOException { Try { Create a MulticastSocket object for sending, receiving data Because the MulticastSocket object needs to be received, there is a specified port Socket = new MulticastSocket (broadcast_port); BroadcastAddress = Inetaddress.getbyname (BROADCAST_IP); Add the socket to the specified multicast address Socket.joingroup (broadcastaddress); Set the datagram sent by this multicastsocket to be echoed back to itself Socket.setloopbackmode (FALSE); Initializes the sending Datagramsocket, which contains a byte array of length 0 Outpacket = new Datagrampacket (new Byte[0], 0, BroadcastAddress, Broadcast_port); Start the thread with the run () method of this instance as the thread body New Thread (This). Start (); Creating a keyboard input stream Scan = new Scanner (system.in); Constant reading of keyboard input while (Scan.hasnextline ()) { Converts a string of characters entered by the keyboard into a byte array byte[] buff = Scan.nextline (). GetBytes (); Set the byte data in the Sent Datagrampacket Outpacket.setdata (Buff); Send Datagrams Socket.send (Outpacket); } } Finally { Socket.close (); } } public void Run () { Try { while (true) { Reads the data in the socket, and the data is read in the byte array encapsulated by the Inpacket. Socket.receive (Inpacket); PrintOut of content read from socket System.out.println ("Chat info:" + new String (Inbuff, 0, Inpacket.getlength ())); } } Catching exceptions catch (IOException ex) { Ex.printstacktrace (); Try { if (socket! = NULL) { Let the socket leave the multipoint IP broadcast address. Socket.leavegroup (broadcastaddress); Close the Socket object Socket.close (); } System.exit (1); } catch (IOException E) { E.printstacktrace (); } } } public static void Main (string[] args) Throws IOException { New Multicastsockettest (). Init (); } } |
The first three lines of the Init () method in the program above create a MulticastSocket object that uses a fixed port for the socket object because it needs to receive datagrams. The second line of bold code adds the socket object to the specified multicast IP address, and the third line of bold code sets the datagram sent by the socket to be echoed back to itself (that is, the socket can accept the datagram it sends). As for the code that uses MulticastSocket to send and receive datagrams in a program is no different from using Datagramsocket,