public class ReceiveTest1 {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10086);
while(true) {
byte []by = new byte[1024];
DatagramPacket dp = new DatagramPacket(by, by.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
//byte[] by1 = dp.getData();
String s = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+ "---" +s);
}
}
}
发送端
public class SendTest1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket ds = new DatagramSocket();
String Line = null;
while((Line = br.readLine()) != null) {
if("886".equals(Line)) {
break;
}
byte[] by = Line.getBytes();
DatagramPacket dp = new DatagramPacket(by, by.length,
InetAddress.getByName("192.168.0.98"), 10086);
ds.send(dp);