Java uses multicast sockets for group chat rooms (LAN)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.