[Java]
Import java. awt. BorderLayout;
Import java. awt. Color;
Import java. awt. FlowLayout;
Import java. awt. GridLayout;
Import java. awt. TextArea;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. io. IOException;
Import java.net. DatagramPacket;
Import java.net. DatagramSocket;
Import java.net. InetAddress;
Import java.net. SocketException;
Import java.net. UnknownHostException;
Import javax. swing. JButton;
Import javax. swing. JFrame;
Import javax. swing. JPanel;
Import javax. swing. JScrollBar;
Import javax. swing. JScrollPane;
Public class UDPChat extends JFrame implements ActionListener {
Public TextArea textmessage = null;
Public TextArea sendtext = null;
Public DatagramSocket socket;
Public JScrollBar vsBar;
Public UDPChat ()
{
Super ();
SetTitle ("small chat program ");
SetBounds (200,150,350,280 );
JPanel panel1 = new JPanel ();
JPanel panel2 = new JPanel ();
Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
SetBackground (Color. BLACK );
Textmessage = new TextArea ();
Sendtext = new TextArea ();
Textmessage. setEditable (false );
Textmessage. setColumns (35 );
Textmessage. setRows (10 );
// Textmessage. setLineWrap (true );
Sendtext. setColumns (35 );
Sendtext. setRows (2 );
JButton buttonOK = new JButton ("OK ");
JButton buttonclean = new JButton ("clear ");
JButton buttonquit = new JButton ("exit ");
GridLayout grid = new GridLayout (3, 1 );
Grid. setHgap (10 );
Grid. setVgap (10 );
Panel1.setLayout (grid );
// Panel1.setLayout (new GridLayout (3, 1 ));
Panel1.add (buttonOK );
Panel1.add (buttonclean );
Panel1.add (buttonquit );
Panel2.setLayout (new FlowLayout ());
Panel2.add (textmessage );
Panel2.add (sendtext );
GetContentPane (). add (panel1, BorderLayout. EAST );
GetContentPane (). add (panel2, BorderLayout. CENTER );
SetVisible (true );
ButtonOK. addActionListener (this );
Buttonclean. addActionListener (this );
Buttonquit. addActionListener (this );
Server ();
}
Public void server ()
{
Try {
Socket = new DatagramSocket (9527 );
Byte [] buf = new byte [1, 1024];
Final DatagramPacket dp1 = new DatagramPacket (buf, buf. length );
Runnable run = new Runnable (){
Public void run ()
{
While (true)
{
Try {
Thread. sleep (100 );
Socket. receive (dp1 );
Int len = dp1.getLength ();
String message = new String (dp1.getData (), 0, len );
String ip = dp1.getAddress (). getHostAddress ();
System. out. println (ip );
If (! InetAddress. getLocalHost (). getHostAddress (). equals (ip ))
Textmessage. append (ip + "\ n" + message + '\ n ');
} Catch (IOException e)
{
E. printStackTrace ();
}
Catch (InterruptedException e)
{
E. printStackTrace ();
}
}
}
};
New Thread (run). start ();
} Catch (SocketException e)
{
E. printStackTrace ();
}
}
Public static void main (String [] args ){
UDPChat udp = new UDPChat ();
}
@ Override
Public void actionreceivmed (ActionEvent e ){
// JButton button = (JButton) e. getSource ();
String buttonname = e. getActionCommand ();
If (buttonname. equals ("exit "))
System. exit (0 );
If (buttonname. equals ("clear "))
{
Textmessage. setText ("");
}
If (buttonname. equals ("OK "))
{
String iP = "192.168.8.15 ";
Try {
InetAddress address = InetAddress. getByName (iP );
Byte [] data = sendtext. getText (). getBytes ();
DatagramPacket dp = new DatagramPacket (data, data. length, address, 9527 );
String myip = InetAddress. getLocalHost (). getHostAddress ();
Textmessage. append (myip + "\ n" + sendtext. getText () + "\ n ");
Socket. send (dp );
Sendtext. setText ("");
} Catch (UnknownHostException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
} Catch (IOException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
}
}
}
}