Java UDP Multi-user Multicast Chat program

Source: Internet
Author: User
Tags gettext sendmsg thread class

OneExperimental Environment

Programming language:Java1.8(run in JVM (Java Virsual machine))

Development tools:ECLIPCE

Test environment: LAN

Ii. purpose of the experiment

Society has entered the information Age , and network technology is developing rapidly . A large number of applications rely on the ability to send the same information from one host to multiple hosts or from multiple hosts to multiple hosts, which can spread over the Internet by up to hundreds of thousands of , requiring higher bandwidth . and greatly exceeds the ability to unicast . An important technology that maximizes the use of existing bandwidth is IP multicast .

Third, the implementation of the multicast chat program:


Package cn.gdut.edu.udp;/** * This program realizes the simple multicast chat function; * Step: * 1, use the MulticastSocket in the java.net package to generate the object s; * 2, set the multicast address group=inetaddress.getbyname ("228.9.6.8"); * 3, join the multicast S.joingroup (group); * 4, all the information that will be sent is encapsulated into an instance of a datagram data=datagrampacket; * 5, the header of the datagram into the alias name of the chat person; * 6, send the processed packets: s.send (data); * 7. Create a queue that accepts datagrams Recv:datagrampacket (buf, buf.length) with a queue size of 100 bytes; * 8. Create a listener thread that performs the accept function of the packet in the thread: s.receive (recv); * 9, chat records can be manually saved to the "Note.txt" file; * Chen */import java.net.*;import java.awt.event.*;import java.awt.*;import java.io.*;import javax.swing.*;p ublic Class Mutilcast {public static void main (string[] args) {multicastsocket s = null;inetaddress group = NULL; JPanel Northpanel = new JPanel (); JPanel Southpanel = new JPanel (); JPanel Namepanel = new JPanel (new FlowLayout ()); JLabel Sendlabel = new JLabel ("Send content:"); JLabel Namechangelabel = new JLabel ("Give Yourself a Name:"), final JTextField nameSpace = new JTextField; final JTextArea messagear ea = new JTextArea ("", ","), final JTextField Sendfield = new JTextField (30); JScrollPane MessaGE = new JScrollPane (Messagearea); JButton Sendbutton = new JButton ("send"); JButton Savebutton = new JButton ("Save Record"); JButton Clearupbutton = new JButton ("empty panel"); Box Centerbox = Box.createverticalbox (); Namepanel.add (Namechangelabel); Namepanel.add (nameSpace); Centerbox.add ( Namepanel); centerbox.add (message);//Implement Multicast Data transfer configuration information try {group = Inetaddress.getbyname ("228.9.6.8");} catch ( Unknownhostexception E1) {System.out.println ("Multicast address binding Failed");} try {s = new MulticastSocket (6789),} catch (IOException E1) {System.out.println ("Multicast address creation failed");} try {s.joingroup (group);} catch (IOException E1) {System.out.println ("Multicast address join failed");} End implementation of Multicast data transfer configuration information//"Send" button implementation information function of the sending part of class Sendmsg implements ActionListener {String msg = null; MulticastSocket s = null;inetaddress Group = Null;public sendmsg (multicastsocket s, inetaddress group) {THIS.S = s;this.gr OUP = Group;} public void actionperformed (ActionEvent e) {try {//If the first name is empty give the prompt if (Namespace.gettext (). IsEmpty ()) Joptionpane.showmessagedialog (NULL, "Be sure to take an alias!") "), else {//if the nameIf the word is not empty, add the name to the data header msg = (Namespace.gettext () + "say:" + sendfield.gettext ());D atagrampacket data = new Datagrampacket (msg.get Bytes (), msg.getbytes (). length, group,6789); s.send (data);}} catch (IOException E1) {System.out.println ("Send Failed");}}} Implementation of the datagram's accepted thread class Recevmsg extends thread {multicastsocket s = null;public recevmsg (multicastsocket s) {THIS.S = s;}; public void Run () {byte[] buf = new byte[100];D atagrampacket recv = new Datagrampacket (buf, buf.length); try {while (true) {s.receive (recv); String str = new String (Recv.getdata ()); String line = System.getproperty ("Line.separator"), Messagearea.append (str); Messagearea.append (line);}} catch (IOException E1) {System.out.println ("Accept failed");}}} The save of the chat record is saved in the "Note.txt" file in the current location, class Savemsg implements ActionListener {String msg = null; String line = System.getproperty ("Line.separator");p ublic void actionperformed (ActionEvent e) {try {msg = Messagearea.gettext (); FileOutputStream Note = new FileOutputStream ("Note.txt"); Messagearea.append ("The record has been saved in NOTE.TXT "); Note.write (Msg.getbytes ()); Messagearea.append (line); Note.close ();} catch (IOException E1) {System.out.println ("Send Failed");}}} Clear the chat record on the panel class Clearmsg implements ActionListener {public void actionperformed (ActionEvent e) {try { Messagearea.settext ("");} catch (Exception E1) {System.out.println ("empty Failed");}}} JFrame mutilcastframe = new JFrame ("Multicast chat tool"); Northpanel.add (Sendlabel); Northpanel.add (Sendfield); Northpanel.add ( Sendbutton); Southpanel.add (Savebutton); Southpanel.add (Clearupbutton); Mutilcastframe.getcontentpane (). Add ( Northpanel, "North"); Mutilcastframe.getcontentpane (). Add (Southpanel, "South"); Mutilcastframe.getcontentpane (). Add (Centerbox, "Center"); Mutilcastframe.setdefaultcloseoperation (Mutilcastframe.exit_on_close); Sendbutton.addactionlistener (New sendmsg (S, group)); Savebutton.addactionlistener (new Savemsg ()); Clearupbutton.addactionlistener (New Clearmsg ()); Mutilcastframe.setsize, Mutilcastframe.setlocation (200, Mutilcastframe.show (); recevmsg r = new Recevmsg (s); R.starT ();}} 

Java UDP Multi-user Multicast Chat program

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.