运行Smack Jingle demo时
(http://fisheye.igniterealtime.org/browse/~raw,r=11613/smack/trunk/jingle/extension/source/org/jivesoftware/smackx/jingle/mediaimpl/demo/Demo.java)总会遇到下面的异常:
java.net.BindException: Cannot assign requested address: Cannot bind
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.PlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at de.javawi.jstun.test.demo.ice.Candidate.<init>(Candidate.java:44)
at de.javawi.jstun.test.demo.ice.ICENegociator.gatherCandidateAddresses(ICENegociator.java:87)
at org.jivesoftware.smackx.jingle.nat.ICEResolver.initialize(ICEResolver.java:81)
at org.jivesoftware.smackx.jingle.nat.TransportResolver.initializeAndWait(TransportResolver.java:387)
at org.jivesoftware.smackx.jingle.nat.ICETransportManager.<init>(ICETransportManager.java:36)
at com.haojie.smack.demo.Demo.initialize(Demo.java:84)
at com.haojie.smack.demo.Demo.<init>(Demo.java:76)
at com.haojie.smack.demo.Demo.main(Demo.java:170)
可以发现是所使用的JSTUN库出了问题. JSTUN位于
http://jstun.javawi.de/. 于是下载了jstun-0.7.3.src.tar.gz, 运行其中的de.javawi.jstun.test.demo.ice.ICENegociator, 还是有同样的问题:
java.net.BindException: Cannot assign requested address: Cannot bind
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.PlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at de.javawi.jstun.test.demo.ice.Candidate.<init>(Candidate.java:44)
at de.javawi.jstun.test.demo.ice.ICENegociator.gatherCandidateAddresses(ICENegociator.java:89)
at de.javawi.jstun.test.demo.ice.ICENegociator.main(ICENegociator.java:176)
经过调试,发现了原因.
ICENegociator类的gatherCandidateAddresses()方法是用来收集candidates地址的, 它首先获取local地址,作为host candidate,然后从这个地址向指定的STUN服务器发送STUN请求,获取server reflexive candidate address. 对于这些地址都会创建一个Candidate对象. 在Candidate类的构造函数里问题出现了, 先看Candidate构造函数:
public Candidate(Address address, short componentId) throws SocketException, UnknownHostException, UtilityException {
this.socket = new DatagramSocket(0, address.getInetAddress());
this.type = CandidateType.Local;
this.componentId = componentId;
this.priority = 0;
this.base = this;
this.isInUse = false;
}
public Candidate(Address address, CandidateType type, short componentId, Candidate base) throws SocketException, UnknownHostException, UtilityException {
this.socket = new DatagramSocket(0, address.getInetAddress());
this.type = type;
setComponentId(componentId);
this.priority = 0;
this.base = base;
this.isInUse = false;
}
在Candidate类的构造函数里,会创建一个DatagramSocket, DatagramSocket构造函数的第二参数是socket要绑定的local address. 对于host candidate, DatagramSocket的创建没有问题,因为host candidate就是local address; 但对于server reflexive candidate address, 问题出现了, 因为这个地址是NAT转换后的地址, socket无法绑定上去的.
所以改动如下, 判断CandidateType, 如果是ServerReflexive, 则不创建socket.主要的代码为:
public class DiscoveryInfo {
.......
private int publicPort; // 用于保存NAT转换后的端口
public int getPublicPort() {
return publicPort;
}
public void setPublicPort(int publicPort) {
this.publicPort = publicPort;
}
......
}
public class ICENegociator {
.......
public void gatherCandidateAddresses() {
......
DiscoveryTest test = new DiscoveryTest(iaddress, stunServer, stunPort);
DiscoveryInfo di = test.test();
if (di.getPublicIP() != null) {
Candidate cand = new Candidate(new Address(di.getPublicIP().getAddress()), CandidateType.ServerReflexive, componentId, local);
cand.setPort(di.getPublicPort());
......
}
}
......
}
public class DiscoveryTest {
private boolean test1() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageHeaderParsingException {
......
di.setPublicIP(ma.getAddress().getInetAddress());
di.setPublicPort(ma.getPort());
......
}
}
public class Candidate implements Comparable {
......
private int port;
public Candidate(Address address, CandidateType type, short componentId, Candidate base) throws SocketException, UnknownHostException, UtilityException {
if (type == CandidateType.Local) {
this.socket = new DatagramSocket(0, address.getInetAddress());
this.address = null;
} else {
this.address = address;
this.socket = null;
}
this.type = type;
setComponentId(componentId);
this.priority = 0;
this.base = base;
this.isInUse = false;
}
public Address getAddress() throws UtilityException {
if (type == CandidateType.Local) {
return new Address(socket.getLocalAddress().getAddress());
} else {
return this.address;
}
}
public int getPort() {
if (type == CandidateType.Local) {
return socket.getLocalPort();
} else {
return this.port;
}
}
public void setPort(int port) {
this.port = port;
}
}