setVisible(true);
}
class send implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
String ip=ipText.getText();
InetAddress address=InetAddress.getByName(ip);
byte[] data=sendText.getText().getBytes();
DatagramPacket dp=new DatagramPacket(data,data.length,address,9527);
String myip=InetAddress.getLocalHost().getHostAddress();
text.append(myip+":\n"+sendText.getText()+"\n");
socket.send(dp);
sendText.setText(null);
}catch(Exception e1){
System.out.println(e1);
}
}
}
class clear implements ActionListener{
public void actionPerformed(ActionEvent e) {
text.setText("");
}
}
class exit implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
private void server() {
try {
socket=new DatagramSocket(9527);
byte[] buf=new byte[1024];
final DatagramPacket dp1=new DatagramPacket(buf,buf.length);
Runnable runnable=new Runnable(){
public void run(){
while(true){
try{
Thread.sleep(100);
socket.receive(dp1);
String message=new String(dp1.getData(),0,dp1.getLength());
String ip=dp1.getAddress().getHostAddress();
if(!InetAddress.getLocalHost().getHostAddress().equals(ip))
text.append(ip+":\n"+message+"\n");
}catch(Exception e){
System.out.println(e);
}
}
}
};
new Thread(runnable).start();
} catch (SocketException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
Frame frame=new Frame();
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
责任编辑:小草