Implement LAN chat tools in Java

Source: Internet
Author: User
Tags configuration settings gety

Use UDP protocol to send a datagram to the other party. You must set the port number for receiving data each time you start it. The IP address and port sent can be modified after it is started.

PackedCode:

Http://115.com/file/e7jb7noz #
Qq.zip

The main idea is simple:

1. Set your own receiving port

2. Set the peer IP address and port

3. send and receive data

 

The following is the main code:

 Import Java.net .* ;  Import Java. AWT .* ;  Import Java. AWT. event .* ;  Import Javax. Swing .* ;  Import Java. Io .* ;  Public   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 = New  Menubar ();  Private Menu setmenu = New Menu ("set" );  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 = New  Config ();  Private Jdialog dialog = New Jdialog (This , "Parameter configuration ", True  );  Private   Int  Myport; qqframe (  Int  In_myport ){  Super ("LAN chat tools" ); 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 (invalid wevent 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 ("sending 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 ("peer IP Address" ); Jlabel yourportlabel = New Jlabel ("recipient's receiving port" ); Jlabel namelabel = New Jlabel ("local 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 (, ); 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:" + 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_qqframe; sendarea = In_sendarea; showarea = In_showarea; config = In_config; system. Out. println ( "In_port:" +Config. getyourport (); qqframe. Validate ();}  Public   Void  Actionreceivmed (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 ( "Data Packet Destination Address:" + dp. getaddress () + "\ n" ); Showarea. append ( "Destination Port Number of the data packet:" + 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  Actionreceivmed (actionevent e) {dialog. Validate ();  Int X = Frame. getx ();  Int Y = Frame. Gety (); dialog. setbounds (x + 50, Y + 50,200,250 ); Dialog. setvisible (  True  );}}  //  Dialog Box  Class Aboutdialog Extends Jdialog Implements  Actionlistener {  Private Jframe;  Private Jlabel JL = New Jlabel ("Thank you for your use, Author: Han Xi" ); Aboutdialog (jframe parent) {Frame = Parent; add (jl );}  Public   Void  Actionreceivmed (actionevent e) {validate ();  Int X = Frame. getx ();  Int Y = Frame. Gety (); setbounds (x + 50, Y + 50,200,250 ); Setvisible (  True  );}}  //  Confirmation button event in the dialog box  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 Actionreceivmed (actionevent e) {config. Set (iptextfield. gettext (), yourporttextfield. gettext (), nametextfield. gettext (); system. Out. println ( "Config: yourport:" + Config. getyourport (); dialog. setvisible (  False  );}}  //  Parameters  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 address ;}  Public String getyourport (){  Return  Yourport ;}  Public  String getname (){  Return  Name ;}} 

The main method code is as follows:

Public ClassQq {Public Static VoidMain (string ARGs []) {If(ARGs. Length = 0) {System. Out. println ("Enter the port number for receiving data after QQ (space required )! "); System. Exit (0);} System. Out. println (integer. parseint (ARGs [0]); Qqframe=NewQqframe (integer. parseint (ARGs [0]) ;}}

Set the port and IP address

 

Below is the chat interface

Related Article

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.