import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
pubblic class Send{
public static void main(String[] args) throws Exception{
//发送端
DataprogramSocket ds = new DataprogramSocket();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while((str=br.readLine())!=null){
byte[] bytes=str.getBytes();
DataprogramPacket dp = new DataprogramPacket(bytes,bytes,length,InetAddress.getByname("127.0.0.1"),7777);
ds.send(dp);
}
ds.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Receive{
public static void main(String[] args) throws IOException{
//接收端
DataprogramSoket ds = new DataprogramSocket(7777);
while(true){
byte[] bytes = new byte[1024];
DataprogramPacket dp = new DataprogramPacket(bytes,bytes,length);
ds.receive(dp);
//解析数据包
byte[] buf = dp.getData();
String str = new String(buf,0,buf.length);
System.out.println("from:("+dp.getAddress().getHostName()+")"+str);
}
}
}