运行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:Cannotassignrequestedaddress:Cannotbind
- atjava.net.PlainDatagramSocketImpl.bind0(NativeMethod)
- atjava.net.PlainDatagramSocketImpl.bind(UnknownSource)
- atjava.net.DatagramSocket.bind(UnknownSource)
- atjava.net.DatagramSocket.<init>(UnknownSource)
- atjava.net.DatagramSocket.<init>(UnknownSource)
- atde.javawi.jstun.test.demo.ice.Candidate.<init>(Candidate.java:44)
- atde.javawi.jstun.test.demo.ice.ICENegociator.gatherCandidateAddresses(ICENegociator.java:87)
- atorg.jivesoftware.smackx.jingle.nat.ICEResolver.initialize(ICEResolver.java:81)
- atorg.jivesoftware.smackx.jingle.nat.TransportResolver.initializeAndWait(TransportResolver.java:387)
- atorg.jivesoftware.smackx.jingle.nat.ICETransportManager.<init>(ICETransportManager.java:36)
- atcom.haojie.smack.demo.Demo.initialize(Demo.java:84)
- atcom.haojie.smack.demo.Demo.<init>(Demo.java:76)
- atcom.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:Cannotassignrequestedaddress:Cannotbind
- atjava.net.PlainDatagramSocketImpl.bind0(NativeMethod)
- atjava.net.PlainDatagramSocketImpl.bind(UnknownSource)
- atjava.net.DatagramSocket.bind(UnknownSource)
- atjava.net.DatagramSocket.<init>(UnknownSource)
- atjava.net.DatagramSocket.<init>(UnknownSource)
- atde.javawi.jstun.test.demo.ice.Candidate.<init>(Candidate.java:44)
- atde.javawi.jstun.test.demo.ice.ICENegociator.gatherCandidateAddresses(ICENegociator.java:89)
- atde.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构造函数:
- publicCandidate(Addressaddress,shortcomponentId)throwsSocketException,UnknownHostException,UtilityException{
- this.socket=newDatagramSocket(0,address.getInetAddress());
- this.type=CandidateType.Local;
- this.componentId=componentId;
- this.priority=0;
- this.base=this;
- this.isInUse=false;
- }
- publicCandidate(Addressaddress,CandidateTypetype,shortcomponentId,Candidatebase)throwsSocketException,UnknownHostException,UtilityException{
- this.socket=newDatagramSocket(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.主要的代码为:
- publicclassDiscoveryInfo{
- .......
- privateintpublicPort;//用于保存NAT转换后的端口
- publicintgetPublicPort(){
- returnpublicPort;
- }
- publicvoidsetPublicPort(intpublicPort){
- this.publicPort=publicPort;
- }
- ......
- }
- publicclassICENegociator{
- .......
- publicvoidgatherCandidateAddresses(){
- ......
- DiscoveryTesttest=newDiscoveryTest(iaddress,stunServer,stunPort);
- DiscoveryInfodi=test.test();
- if(di.getPublicIP()!=null){
- Candidatecand=newCandidate(newAddress(di.getPublicIP().getAddress()),CandidateType.ServerReflexive,componentId,local);
- cand.setPort(di.getPublicPort());
- ......
- }
- }
- ......
- }
- publicclassDiscoveryTest{
- privatebooleantest1()throwsUtilityException,SocketException,UnknownHostException,IOException,MessageAttributeParsingException,MessageHeaderParsingException{
- ......
- di.setPublicIP(ma.getAddress().getInetAddress());
- di.setPublicPort(ma.getPort());
- ......
- }
- }
- publicclassCandidateimplementsComparable{
- ......
- privateintport;
- publicCandidate(Addressaddress,CandidateTypetype,shortcomponentId,Candidatebase)throwsSocketException,UnknownHostException,UtilityException{
- if(type==CandidateType.Local){
- this.socket=newDatagramSocket(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;
- }
- publicAddressgetAddress()throwsUtilityException{
- if(type==CandidateType.Local){
- returnnewAddress(socket.getLocalAddress().getAddress());
- }else{
- returnthis.address;
- }
- }
- publicintgetPort(){
- if(type==CandidateType.Local){
- returnsocket.getLocalPort();
- }else{
- returnthis.port;
- }
- }
- publicvoidsetPort(intport){
- this.port=port;
- }
- }
转自:http://mysuperbaby.iteye.com/blog/901370