package itat.s11_8;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Beijing {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Beijing_Frame beijing_win=new Beijing_Frame();
beijing_win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
beijing_win.validate();
}
}
class Beijing_Frame extends Frame implements Runnable,ActionListener{
TextField out_message=new TextField("发送数据到上海:");
TextArea in_message=new TextArea();
Button b=new Button("发送数据包到上海");
Beijing_Frame(){
super("我是北京");
setSize(200,200);
setVisible(true);
b.addActionListener(this);
add(out_message,"South");
add(in_message,"Center");
add(b,"North");
Thread thread=new Thread(this);
thread.start();
}
public void run() {
// TODO Auto-generated method stub
DatagramPacket pack=null;
DatagramSocket mail_data=null;
byte data[]=new byte[8192];
pack=new DatagramPacket(data,data.length);
try {
mail_data=new DatagramSocket(888);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
if(mail_data==null)break;
else
try {
mail_data.receive(pack);
int length=pack.getLength();
InetAddress adress=pack.getAddress();
int port=pack.getPort();
String message=new String(pack.getData(),0,length);
in_message.append("收到数据长度:"+length+"/n");
in_message.append("收到数据来自:"+adress+"端口:"+port+"/n");
in_message.append("收到数据是:"+message+"/n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
byte buffer[]=out_message.getText().trim().getBytes();
try {
InetAddress address=InetAddress.getByName("127.0.0.1");
DatagramPacket data_pack=new DatagramPacket(buffer,buffer.length,address,666);
DatagramSocket mail_data=new DatagramSocket();
in_message.append("数据包目标主机地址:"+data_pack.getAddress()+"/n");
in_message.append("数据包目标端口是:"+data_pack.getPort()+"/n");
in_message.append("数据包长度:"+data_pack.getLength()+"/n");
mail_data.send(data_pack);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SocketException eh) {
// TODO Auto-generated catch block
eh.printStackTrace();
} catch (IOException ae) {
// TODO Auto-generated catch block
ae.printStackTrace();
}
}
}
package itat.s11_8;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Shanghai {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Shanghai_Frame shanghai_win=new Shanghai_Frame();
shanghai_win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
shanghai_win.validate();
}
}
class Shanghai_Frame extends Frame implements Runnable,ActionListener{
TextField out_message=new TextField("发送数据到北京:");
TextArea in_message=new TextArea();
Button b=new Button("发送数据包到北京");
Shanghai_Frame(){
super("我是上海");
setSize(200,200);
setVisible(true);
b.addActionListener(this);
add(out_message,"South");
add(in_message,"Center");
add(b,"North");
Thread thread=new Thread(this);
thread.start();
}
public void run() {
// TODO Auto-generated method stub
DatagramPacket pack=null;
DatagramSocket mail_data=null;
byte data[]=new byte[8192];
pack=new DatagramPacket(data,data.length);
try {
mail_data=new DatagramSocket(666);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
if(mail_data==null)break;
else
try {
mail_data.receive(pack);
int length=pack.getLength();
InetAddress adress=pack.getAddress();
int port=pack.getPort();
String message=new String(pack.getData(),0,length);
in_message.append("收到数据长度:"+length+"/n");
in_message.append("收到数据来自:"+adress+"端口:"+port+"/n");
in_message.append("收到数据是:"+message+"/n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
byte buffer[]=out_message.getText().trim().getBytes();
try {
InetAddress address=InetAddress.getByName("127.0.0.1");
DatagramPacket data_pack=new DatagramPacket(buffer,buffer.length,address,888);
DatagramSocket mail_data=new DatagramSocket();
in_message.append("数据包目标主机地址:"+data_pack.getAddress()+"/n");
in_message.append("数据包目标端口是:"+data_pack.getPort()+"/n");
in_message.append("数据包长度:"+data_pack.getLength()+"/n");
mail_data.send(data_pack);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SocketException eh) {
// TODO Auto-generated catch block
eh.printStackTrace();
} catch (IOException ae) {
// TODO Auto-generated catch block
ae.printStackTrace();
}
}
}