Import Java.awt.dimension;import java.awt.textarea;import Java.awt.textfield;import Java.awt.toolkit;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.windowadapter;import Java.awt.event.windowevent;import Java.io.ioexception;import Java.net.datagrampacket;import java.net.InetAddress; Import Java.net.multicastsocket;import Javax.swing.jbutton;import Javax.swing.jframe;public class Multicast extends JFrame implements Runnable {private static TextArea mshowmsg;//display information record private TextField Mmsgfield;//input Send content component private JBU Tton Msendbutton; The Send button private Dimension screensize = Toolkit.getdefaulttoolkit (). Getscreensize ();p rotected static final int PORT = 3838; The port private static final String broadcast_ip = "230.0.0.1";//The IP protocol provides some special IP addresses for multicast, between 224.0.0.0 to 239.255.255 private static MulticastSocket multicast; Multicast sockets private static InetAddress group;private static Boolean IsFirst; Is the first message in the Information log Public multicast () {settitle ("multicast group chat room"); SetSize (450, 400int width = (int) screensize.getwidth (); int height = (int) screensize.getheight (); setlocation (WIDTH/2-getwidth ()/2 , HEIGHT/2-GetHeight ()/2); setlayout (null); This.addwindowlistener (new Windowadapter () {public void windowclosing (Win Dowevent We {try {sendMessage ("I'm offline!") ");} catch (IOException e) {e.printstacktrace ();} System.exit (0);}); mshowmsg = new TextArea (); Mshowmsg.setbounds (+, +, 280); mshowmsg.seteditable (false); add (mshowmsg); Mmsgfield = New TextField (), Mmsgfield.setbounds (310, (+), (); Add (Mmsgfield); Msendbutton = new JButton ("send"); Msendbutton.setbounds (310, +), Msendbutton.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent arg0) {//Send data try {sendMessage (Mmsgfield.gettext ());} catch (IOException e) { E.printstacktrace ();}}); Add (Msendbutton); setvisible (true);} public static void Main (string[] args) throws IOException {IsFirst = False;multicast = new MulticastSocket (PORT); group = I Netaddress.getbyname (BROADCAST_IP); Multicast group Multicast.joingroup (group); Join the multicast group to receive the packet new Thread (new Multicast ()). Start (); try {thread.sleep (100);//And other information recipients ready to send "I'm online!" "The information is guaranteed to receive the SendMessage (" I'm online! ");} catch (Interruptedexception e) {e.printstacktrace ();}} /** * Broadcast this message * * @throws ioexception */private static void SendMessage (String msg) throws IOException {//Sent packet class Datagra Mpacket packet = new Datagrampacket (Msg.getbytes (), msg.getbytes (). Length, group, PORT); multicast.send (packet); Send packet} @Overridepublic void Run () {//Receive information byte[] msg = new Byte[1024];while (true) {Datagrampacket packet = new Datagram Packet (msg, msg.length); try {multicast.receive (Packet);} catch (IOException e) {e.printstacktrace ();} if (IsFirst) {mshowmsg.append ("\ r \ n");} SYSTEM.OUT.PRINTLN (msg, 0, Packet.getlength ())), IsFirst = True;mshowmsg.append (packet.getaddress () + "says:" + New String (msg, 0, Packet.getlength ()). Trim ());}}}
Operating effect:
Java uses multicast sockets for group chat rooms (LAN)