-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDPClientB.java
More file actions
88 lines (79 loc) · 3.54 KB
/
UDPClientB.java
File metadata and controls
88 lines (79 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package org.renlr.test;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
public class UDPClientB {
public static void main(String[] args) {
try {
//向server发起请求
SocketAddress target = new InetSocketAddress("10.1.11.137", 2008);
DatagramSocket client = new DatagramSocket();
String message = "I am UDPClientB 192.168.85.129";
byte[] sendbuf = message.getBytes();
DatagramPacket pack = new DatagramPacket(sendbuf, sendbuf.length, target);
client.send(pack);
//接收server的回复内容
byte[] buf = new byte[1024];
DatagramPacket recpack = new DatagramPacket(buf, buf.length);
client.receive(recpack);
//处理server回复的内容,然后向内容中的地址与端口发起请求(打洞)
String receiveMessage = new String(recpack.getData(), 0, recpack.getLength());
String[] params = receiveMessage.split(",");
String host = params[0].substring(5);
String port = params[1].substring(5);
System.out.println(host + ":" + port);
sendMessage(host, port, client);
} catch (Exception e) {
e.printStackTrace();
}
}
//向UPDClientA发起请求(在NAT上打孔)
private static void sendMessage(String host, String port, DatagramSocket client) {
try {
SocketAddress target = new InetSocketAddress(host, Integer.parseInt(port));
for (;;) {
String message = "I am master 192.168.85.129 count test";
byte[] sendbuf = message.getBytes();
DatagramPacket pack = new DatagramPacket(sendbuf, sendbuf.length, target);
client.send(pack);
//接收UDPClientA回复的内容
receive(client);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//收到UDPClientA的回复内容,穿透已完成
private static void receive(DatagramSocket client) {
try {
for (;;) {
//将接收到的内容打印
byte[] buf = new byte[1024];
DatagramPacket recpack = new DatagramPacket(buf, buf.length);
client.receive(recpack);
String receiveMessage = new String(recpack.getData(), 0, recpack.getLength());
System.out.println(receiveMessage);
//记得重新收地址与端口,然后在以新地址发送内容到UPDClientA,就这样互发就可以了。
int port = recpack.getPort();
InetAddress address = recpack.getAddress();
String reportMessage = "I am master 192.168.85.129 count test";
//发送消息
sendMessage(reportMessage, port, address, client);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void sendMessage(String reportMessage, int port, InetAddress address, DatagramSocket client) {
try {
byte[] sendBuf = reportMessage.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, address, port);
client.send(sendPacket);
System.out.println("send success");
} catch (Exception e) {
e.printStackTrace();
}
}
}