The main idea is simple:
1. Set up your own receive Port
2. Set the other IP and port
3. Sending data and receiving data
Here's the main code:
Import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;p ublic class Qqframe extends JFrame implements runnable{private JTextArea Sendarea = new JTextArea (20,20); Private JTextArea Showarea = new JTextArea (10,20); Private JButton Sendbutton = new JButton ("send"); Private MenuBar MenuBar = new MenuBar (); Private Menu SetMenu = new Menu ("Settings"); Private Menu Helpmenu = new Menu ("Help"); Private MenuItem Aboutitem = new MenuItem ("about"); Private MenuItem SetItem = new MenuItem ("Configuration Parameters"); Private thread thread = new thread (this); Private config config = new config (); Private JDialog Dialog = new JDialog (this, "parameter configuration", true); private int myport; Qqframe (int in_myport) {super ("LAN chat Tool"); Setresizable (FALSE); MyPort = In_myport; Setitem.addactionlistener (New Setdialog (Dialog,this)); Aboutitem.addactionlistener (New Aboutdialog (this)); Initlayout (); Sendbutton.addactionlIstener (New Sendlistener (This,sendarea,showarea,config)); Thread.Start (); Addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) { System.exit (0); } }); } void Initlayout () {int w,h; W = 400; h = 500; SetBounds (100,100,W,H); SetVisible (TRUE); SetLayout (New GridLayout (3,1,10,15)); JPanel JP1,JP2; JP1 = new JPanel (); JP2 = new JPanel (); JLabel jl1; JL1 = new JLabel ("Chat record Box:"); Jp1.setlayout (NULL); Jp1.add (JL1); Jl1.setbounds (0,0,w,20); Jp1.add (Showarea); Showarea.setbounds (0,30,w,h/3-20); JLabel Jl2; Jp2.setlayout (NULL); JL2 = new JLabel ("Send box:"); Jp2.add (JL2); Jl2.setbounds (0,0,w,20); Sendarea.setbounds (0,30,w,h/3-20); Jp2.add (Sendarea); JPanel jp3 = new JPanel (); Add (JP1); Add (JP2); Add (JP3); Jp3.add (Sendbutton); Setmenubar (MenuBar); Menubar.add (SetMenu); Menubar.add (Helpmenu); Helpmenu.add (Aboutitem); Setmenu.add (SetItem); Dialog.setbounds (120,150,200,250); JLabel Iplabel = new JLabel ("Opposing IP"); JLabel Yourportlabel = new JLabel ("receiver Port"); JLabel Namelabel = new JLabel ("native name"); JTextField Iptextfield = new JTextField (15); JTextField Yourporttextfield = new JTextField (8); JTextField Nametextfield = new JTextField (15); Iptextfield.settext ("127.0.0.1"); Yourporttextfield.settext ("879"); Nametextfield.settext ("Hanxi"); Dialog.setlayout (New GridLayout (4,2,10,10)); Dialog.add (Iplabel); Dialog.add (Iptextfield); Dialog.add (Yourportlabel); Dialog.add (Yourporttextfield); Dialog.add (Namelabel); Dialog.add (Nametextfield); Dialog.add (New Panel ()); JButton Surebutton = new JButton ("OK"); Dialog.add (SurEbutton); Surebutton.addactionlistener (New Surelistener (Iptextfield,yourporttextfield,nametextfield,dialog,config)); public void Run () {datagrampacket DP = NULL; Datagramsocket ds = null; byte[] buf = new byte[1024]; try {dp = new Datagrampacket (buf,buf.length); ds = new Datagramsocket (myport); System.out.println ("myport=" +myport); } catch (Exception e) {} while (true) {try {ds.receive ( DP); int length = Dp.getlength (); InetAddress address = dp.getaddress (); int port = Dp.getport (); String message = new String (Dp.getdata (), 0,length); Showarea.append ("Received data length:" +length+ "\ n"); Showarea.append ("Received data from:" +address+ "Port:" +port+ "\ n"); Showarea.append ("Received data is:" +message+ "\ n"); } catch (Exception e) { }}}}//send event class Sendlistener implements actionlistener{private JFrame qqframe; Private JTextArea Sendarea; Private JTextArea Showarea; Private config config; Sendlistener (JFrame in_qqframe, JTextArea In_sendarea, JTextArea In_showarea, Config in_config) {qqframe = In_Q Qframe; Sendarea = In_sendarea; Showarea =in_showarea; config = in_config; System.out.println ("In_port:" +config.getyourport ()); Qqframe.validate (); } public void actionperformed (ActionEvent e) {System.out.println ("You clicked the Send button"); byte[] buf = Sendarea.gettext (). Trim (). GetBytes (); try {inetaddress address = Inetaddress.getbyname (Config.getip ()); Datagrampacket DP = new Datagrampacket (Buf,buf.length,address,integer.parseint (Config.getyourport ())); Datagramsocket ds = new Datagramsocket (); Showarea.append ("Packet Destination Address:" +dp.getaddress () + "\ n"); Showarea.apPend ("Packet Destination port number:" +dp.getport () + "\ n"); Showarea.append ("Packet Length:" +dp.getlength () + "\ n"); Ds.send (DP); } catch (Exception ee) {}}}//configuration settings class Setdialog implements actionlistener{Private JDialog dialog; Private JFrame frame; Setdialog (JDialog in_dialog,jframe Parent) {dialog = In_dialog; frame = parent; } public void actionperformed (ActionEvent e) {dialog.validate (); int x = Frame.getx (); int y = frame.gety (); Dialog.setbounds (x+50,y+50,200,250); Dialog.setvisible (TRUE); }}//About dialog box class Aboutdialog extends JDialog implements actionlistener{private JFrame frame; Private JLabel JL = new JLabel ("Thank you for your use, Han XI"); Aboutdialog (JFrame parent) {frame = parent; Add (JL); } public void actionperformed (ActionEvent e) {validate (); int x = Frame.getx (); int y = frame.gety (); SetBounds (x+50,y+50,200,250); SetVisible (TRUE);}}//dialog box OK button event class Surelistener implements actionlistener{JTextField Iptextfield; JTextField Yourporttextfield; JTextField Nametextfield; Private Dialog Dialog; Private config config; Surelistener (JTextField in_iptextfield,jtextfield in_yourporttextfield,jtextfield in_nameTextField,JDialog in_ Dialog,config in_config) {Iptextfield = In_iptextfield; Yourporttextfield = In_yourporttextfield; Nametextfield = In_nametextfield; dialog = In_dialog; config = in_config; } public void actionperformed (ActionEvent e) {config.set (Iptextfield.gettext (), Yourporttextfield.gettext (), n Ametextfield.gettext ()); System.out.println ("Config:yourport:" +config.getyourport ()); Dialog.setvisible (FALSE); }}//parameter class config{private String ip= "127.0.0.1"; Private String yourport= "666"; Private String name= "Hanxi"; public void Set (String in_ip, String in_yourport,string in_name) {IP = in_ip; Yourport = In_yourport; name = In_name; } public String GetIP () {return IP; } public String Getyourport () {return yourport; } public String GetName () {return name; }}
Here is the Main method code:
public class qq{public static void Main (String args[]) { if (args.length = = 0) { System.out.println ("Please enter the port number to receive the data after QQ (need space)!" "); System.exit (0); } System.out.println (Integer.parseint (args[0)); Qqframe frame = new Qqframe (Integer.parseint (args[0]));} }
Set up a picture of ports and IP
Here is the chat interface
UDP protocol-based chat room (Java implementation)