//UDP发射端
import java.io.*;
import java.net.*;
/**
* udp发送端
* @author Administrator
*
*/
public class Udpsend {
public static void main(String[] args) {
DatagramSocket out =null;
try {
out = new DatagramSocket(10000);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
try {
while((str=br.readLine())!=null){
if(str.equals("886")){
break;
}
byte[] byt =str.getBytes();
DatagramPacket pac = new DatagramPacket(byt, byt.length, InetAddress.getLocalHost(), 10001);
out.send(pac);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
out.close();
}
}
//UDP接收端
import java.io.IOException;
import java.net.*;
/**
* UDP接收端
* @author Administrator
*
*/
public class Udpserver {
public static void main(String[] args) {
DatagramSocket in = null;
try {
in = new DatagramSocket(10001);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
byte[] byt= new byte[1024*64];
DatagramPacket pac = new DatagramPacket(byt, byt.length);
try {
in.receive(pac);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//String ip = pac.getAddress().getHostAddress();
String data = new String(pac.getData(),0,pac.getLength());
System.out.println(data);
}
}
}