A端
public class A {
public static void main(String[] args) throws IOException {
//子线程用来读取消息
new Thread() {
@Override
public void run() {
try {
DatagramSocket ds = new DatagramSocket(8888);
while (true) {
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
System.out.println("A服务器已经开启,等待接收数据.....");
ds.receive(dp);
//取出数据
byte[] data = dp.getData();
int length = dp.getLength(); //取出数据的实际长度
//获取客户端IP
String ip = dp.getAddress().getHostAddress();
String s = new String(data, 0, length);
System.out.println(ip + "-给你发来消息:" + s);
if ("886".equals(s)) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
//主线程用来发消息
sendMsg();
}
private static void sendMsg() throws IOException {
DatagramSocket ds = new DatagramSocket();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("请输入要发送给B的消息");
String msg = reader.readLine();
byte[] bytes = msg.getBytes();
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.15.90"), 8888);
ds.send(dp);
//自定义结束标记
if ("886".equals(msg)) {
break;
}
}
ds.close();
}
}
B端
public class B {
public static void main(String[] args) throws IOException {
//子线程用来读取消息
new Thread() {
@Override
public void run() {
try {
DatagramSocket ds = new DatagramSocket(8888);
while (true) {
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
System.out.println("B服务器已经开启,等待接收数据.....");
ds.receive(dp);
//取出数据
byte[] data = dp.getData();
int length = dp.getLength(); //取出数据的实际长度
//获取客户端IP
String ip = dp.getAddress().getHostAddress();
String s = new String(data, 0, length);
System.out.println(ip + "-给你发来消息:" + s);
if ("886".equals(s)) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
//主线程用来发消息
sendMsg();
}
private static void sendMsg() throws IOException {
ArrayList<String> iPs = IPUtils.getIPs();
for (String ip : iPs) {
System.out.println(ip);
}
DatagramSocket ds = new DatagramSocket();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Scanner sc= new Scanner(System.in);
System.out.println("请输入对方的ip");
String ip = sc.nextLine();
while (true) {
System.out.println("请输入要发送给A的消息");
String msg = reader.readLine();
byte[] bytes = msg.getBytes();
System.out.println(ip);
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(ip), 8888);
ds.send(dp);
//自定义结束标记
if ("886".equals(msg)) {
break;
}
}
ds.close();
}
}
获取IP
public class IPUtils {
public static ArrayList<String> getIPs() throws IOException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("arp /a");
InputStream in = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"GBK"));
ArrayList<String> ips = new ArrayList<>();
while (true){
String s = reader.readLine();
if(s==null){
break;
}
if(s.contains("192.168.15")){
String[] str = s.trim().split("\\s+");
//System.out.println(str[0]);
ips.add(str[0]);
}
}
return ips;
}
}