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”