import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class sendDemo {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
DatagramSocket ds=new DatagramSocket();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=br.readLine())!=null){
if(line.equals("886")){
break;
}
byte[] buf="fsdfsdfd".getBytes();
InetAddress is=InetAddress.getByName("192.168.49.255");
DatagramPacket dp=new DatagramPacket(buf,buf.length,is,9009);
ds.send(dp);
}
ds.close();
}
}
package csdn.hbsi.com;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveDemo {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
DatagramSocket ds=new DatagramSocket(9009);
while(true){
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(),0,dp.getLength());
int port=dp.getPort();
System.out.println(ip+":"+port+"\n\t"+data);
}
}
}