JAVA uses multicast sockets to implement group chat room (LAN)
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. using wevent; import java. io. IOException; import java.net. datagramPacket; import java.net. inetAddress; import java.net. multicastSocket; import javax. swing. JButton; import javax. s Wing. JFrame; public class Multicast extends JFrame implements Runnable {private static TextArea mShowMsg; // display information record private TextField mMsgField; // enter the private JButton mSendButton component for sending content; // send the private Dimension screenSize = Toolkit. getdefatooltoolkit (). getScreenSize (); protected static final int PORT = 3838; // PORT private static final String BROADCAST_IP = "230.0.0.1"; // The IP protocol provides some special IP addresses for multicast, at 224.0.0.0 Private static MulticastSocket multicast between 239.0000255; // Multicast socket private static InetAddress group; private static boolean isFirst; // whether the first information in the information record is public multicast () {setTitle ("multicast group room"); setSize (450,400); int width = (int) screenSize. getWidth (); int height = (int) screenSize. getHeight (); setLocation (width/2-getWidth ()/2, height/2-getHeight ()/2); setLayout (null); this. addWindowListener (new Wind OwAdapter () {public void windowClosing (incluwevent we) {try {sendMessage ("I'm offline! ");} Catch (IOException e) {e. printStackTrace ();} System. exit (0) ;}}); mShowMsg = new TextArea (); mShowMsg. setBounds (20, 10,400,280); mShowMsg. setEditable (false); add (mShowMsg); mMsgField = new TextField (); mMsgField. setBounds (20,310,320, 28); add (mMsgField); mSendButton = new JButton ("send"); mSendButton. setBounds (350,310, 70, 28); mSendButton. addActionListener (new ActionListener () {@ Overridepublic void ac Tionreceivmed (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 = InetAddress. 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); // wait for the message recipient to prepare and send "I am going online !" To ensure that you can receive sendMessage ("I am online! ");} Catch (InterruptedException e) {e. printStackTrace () ;}}/*** broadcast this message ** @ throws IOException */private static void sendMessage (String msg) throws IOException {// The sent data package class DatagramPacket packet = new DatagramPacket (msg. getBytes (), msg. getBytes (). length, group, PORT); multicast. send (packet); // send data packet} @ Overridepublic void run () {// receive information byte [] msg = new byte [1024]; while (true) {DatagramPacket packet = new DatagramPacket (msg, msg. length); try {multicast. receive (packet);} catch (IOException e) {e. printStackTrace ();} if (isFirst) {mShowMsg. append ("\ r \ n");} System. out. println (new String (msg, 0, packet. getLength (); isFirst = true; mShowMsg. append (packet. getAddress () + "said:" + new String (msg, 0, packet. getLength ()). trim ());}}}
Running effect: