Simple interface:
Train of thought:
1. First create the interface in the previous illustration
2. Add related event listener Events
3. Use multithreading to design the sender and receiver
4. Create thread initiation threads in the Send button and the receiving end respectively.
5. The last Datagramsocket service (guaranteed uniqueness) to be sent and received separately in the main program
Import java.net.*;
Import java.io.*;
Import java.awt.*;
Import java.awt.event.*; Class Chatdemo {public static void main (string[] args) throws Socketexception,unknownhostexception {Dat
Agramsocket send=new Datagramsocket ();
Datagramsocket receive=new Datagramsocket (10001);
New Framedemo (send,receive);
} class Framedemo {private Button clearbut;
Private Datagramsocket send;
Private Datagramsocket receive;
Private Frame F;
Private TextArea Sendarea;
Private TextArea Receivearea;
Private Button but;
Framedemo (Datagramsocket Send,datagramsocket receive) {this.send=send;
this.receive=receive;
Init ();
public void init () {f=new Frame ("chat panel");
F.setbounds (600,500,500,500);
F.setlayout (New FlowLayout ());
Sendarea=new TextArea ();
Receivearea=new TextArea ();
F.add (Receivearea);
F.add (Sendarea); Clearbut=new BuTton ("clear screen");
But=new button ("send");
F.add (clearbut);
F.add (But);
MyEvent ();
F.setvisible (TRUE); The public void MyEvent () {F.addwindowlistener () {The new Windowadapter () {public void window
Closing (WindowEvent e) {system.exit (0);
}
});
But.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {
New Thread (New Send (Send,sendarea)). Start ();
New Thread (new receive (Receive,receivearea)). Start ();
}
});
Clearbut.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e)
{Receivearea.settext ("");
}
});
}///Receive class receive implements Runnable {private Datagramsocket ds;
Private TextArea Receivearea; Receive (DatagramsOcket Ds,textarea Receivearea) {this.ds=ds;
This.receivearea=receivearea;
public void Run () {//Create packet byte[] Buf=new byte[1024];
Datagrampacket dp=new Datagrampacket (buf,buf.length);
try {ds.receive (DP);
catch (IOException e) {throw new RuntimeException ("Receive-side error");
String ip=dp.getaddress (). gethostaddress ();
String Data=new string (Dp.getdata (), 0,dp.getlength ());
Receivearea.append (ip+ "\ r \ n" +data+ "\ r \ n");
}//Send End Class Send implements Runnable {private TextArea sendarea;
Private Datagramsocket DS;
Send (Datagramsocket ds,textarea sendarea) {this.ds=ds;
This.sendarea=sendarea;
public void Run () {String senddata=sendarea.gettext ();
Sendarea.settext (""); Create a packet byte[]Buf=senddata.getbytes ();
Datagrampacket Dp=null;
try {dp=new datagrampacket (buf,buf.length,inetaddress.getbyname ("127.0.0.1"), 10001);
catch (Unknownhostexception e) {throw new RuntimeException ("Send-side error");
try {if (buf.length==0) return;
Ds.send (DP);
catch (IOException e) {throw new RuntimeException ("Send-side error");
}
}
}