}
public void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
cancelBut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
f.setVisible(false);
}
});
sendBut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String text = ta2.getText();
try {
send(text);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//receive();
receive();
}
});
}
public void send(final String str) throws Exception
{
new Thread(new Runnable(){
public void run()
{
try
{
DatagramSocket ds = new DatagramSocket();
byte[] buf =str.getBytes();
DatagramPacket dp =
new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.23"),10000);
ds.send(dp);
}
catch(Exception e)
{
System.out.println("发送失败");
}
}
}).start();
}
public void receive()
{
new Thread(new Runnable(){
public void run()
{
try
{
DatagramSocket ds = new DatagramSocket(10000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData());
int port = dp.getPort();
ta1.setText(ip+":"+data);