Java UDP穿透NAT試驗 – JAVA編程語言程序開發技術文章

早就聽說用UDP穿透NAT可以解決P2P軟件中的兩個通過NAT上網的客戶端直接通信的問題。當然,需要一個中介來幫助找到對方。終於用Java做瞭這個試驗。

 

代碼貼出來吧。

 

UDPAgent.java:

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.regex.Pattern;

 

/**
*
* @author Leo Luo
*
*/
public class UDPAgent implements Runnable {

 

public static void main(String[] args) throws Exception {
new UDPAgent(-1).start();
}

 

DatagramSocket ds;

 

byte[] recbuf = new byte[1024];

 

DatagramPacket rec = new DatagramPacket(recbuf, recbuf.length);

 

static String ipPattern = "([0-9]{1,3}.){3}[0-9]{1,3}";

 

static String portPattern = "[0-9]{1,5}";

 

static Pattern sendPattern = Pattern.compile("send " + ipPattern + " "
+ portPattern + " .*");

 

int port;

 

public UDPAgent(int port) {
this.port = port;

 

}

 

public void init() throws Exception {
if (port < 1024 || port > 655535) {
ds = new DatagramSocket();
} else {
ds = new DatagramSocket(port);
}
}

 

public void start() throws Exception {
println("start");
println("LocalPort:" + port);
init();
new Thread(this).start();// recive thread
receive();
}

 

public void receive() {
for (;;) {
try {
ds.receive(rec);
String msg = new String(rec.getData(), rec.getOffset(), rec
.getLength());
String line = rec.getSocketAddress() + ":" + msg;
println(line);
onReceive(rec);
} catch (Exception e) {
e.printStackTrace();
}
}
}

 

public void onReceive(DatagramPacket rec) {

 

}

 

public void doCommand(String cmd) throws Exception {
// command:
// 1. send xxx.xxx.xxx.xxx xxx *******************
if (sendPattern.matcher(cmd).matches()) {
doSend(cmd);
}
}

 

public void doSend(String cmd) throws Exception {
println("CMD: " + cmd);
String[] s = cmd.split(" ", 4);
int port = Integer.parseInt(s[2]);
InetSocketAddress target = new InetSocketAddress(s[1], port);
byte[] bs = s[3].getBytes();
doSend(target, bs);
}

 

public void doSend(SocketAddress addr, byte[] data) throws Exception {
DatagramPacket pack = new DatagramPacket(data, data.length, addr);
ds.send(pack);
}

 

public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
try {
String line = reader.readLine();
while (!"exit".equals(line)) {
doCommand(line);
line = reader.readLine();
}
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}

 

public void println(String s) {
System.out.println(System.currentTimeMillis() + ":" + s);
}
}

 

UDPClient.java

 

____________________________________________________

 

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

 

public class UDPClient extends UDPAgent {

 

/**
* @param args
*/
public static void main(String[] args) throws Exception {
new UDPClient("www.javadoc.cn", 2008, -1).start();
}

 

String serverName;

 

int serverPort;

 

SocketAddress server;

 

public UDPClient(String host, int port, int localPort) {
super(localPort);
this.server = new InetSocketAddress(host, port);

 

}

 

public void start() throws Exception {
println("start");
init();
register();
new Thread(this).start();// recive thread
receive();
}

 

public void onReceive(DatagramPacket rec) {
try {
report(rec);
if (rec.getSocketAddress().equals(server)) {
doCommand(new String(rec.getData(), rec.getOffset(), rec
.getLength()));
}
} catch (Exception e) {
e.printStackTrace();
}

 

}

 

public void report(DatagramPacket rec) throws Exception {
String s = rec.getSocketAddress()
+ new String(rec.getData(), rec.getOffset(), rec.getLength());
byte[] buf = s.getBytes();
ds.send(new DatagramPacket(buf, buf.length, server));
}

 

public void register() throws Exception {
String msg = "register " + getLocalAddress() + " " + ds.getLocalPort();
doSend(server, msg.getBytes());
}

 

public String getLocalAddress() throws Exception {
InetAddress addr = InetAddress.getLocalHost();
return addr.getHostAddress();
}
}

 

UDPServer.java

 

_______________________________________________________________

 


public class UDPServer extends UDPAgent {

 

public static void main(String[] args) throws Exception {
new UDPServer(2008).start();
}

 

public UDPServer(int port) {
super(port);
}
}

 

1。啟動一個Server.

 

2。啟動兩個Client.

 

然後從Server端的Console裡邊可以看到兩個Client的NAT後的地址和端口。

 

在Server段輸入命令 send a.a.a.a A send b.b.b.b B hello

 

a.a.a.a是第一個Client的NAT後的ip,A端口號。

 

b是第二個。。。

 

輸入這個命令後,A就會直接發給B一個 hello。 發送成功。 如果是同一個NAT後邊,可能要讓A發送到B的內網地址才能成功。

 本文出自“yypiao”

發佈留言