Smack Jingle java.net.BindException: Cannot assign requested address: Cannot bind

运行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代码 收藏代码
  1. java.net.BindException:Cannotassignrequestedaddress:Cannotbind
  2. atjava.net.PlainDatagramSocketImpl.bind0(NativeMethod)
  3. atjava.net.PlainDatagramSocketImpl.bind(UnknownSource)
  4. atjava.net.DatagramSocket.bind(UnknownSource)
  5. atjava.net.DatagramSocket.<init>(UnknownSource)
  6. atjava.net.DatagramSocket.<init>(UnknownSource)
  7. atde.javawi.jstun.test.demo.ice.Candidate.<init>(Candidate.java:44)
  8. atde.javawi.jstun.test.demo.ice.ICENegociator.gatherCandidateAddresses(ICENegociator.java:87)
  9. atorg.jivesoftware.smackx.jingle.nat.ICEResolver.initialize(ICEResolver.java:81)
  10. atorg.jivesoftware.smackx.jingle.nat.TransportResolver.initializeAndWait(TransportResolver.java:387)
  11. atorg.jivesoftware.smackx.jingle.nat.ICETransportManager.<init>(ICETransportManager.java:36)
  12. atcom.haojie.smack.demo.Demo.initialize(Demo.java:84)
  13. atcom.haojie.smack.demo.Demo.<init>(Demo.java:76)
  14. 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代码 收藏代码
  1. java.net.BindException:Cannotassignrequestedaddress:Cannotbind
  2. atjava.net.PlainDatagramSocketImpl.bind0(NativeMethod)
  3. atjava.net.PlainDatagramSocketImpl.bind(UnknownSource)
  4. atjava.net.DatagramSocket.bind(UnknownSource)
  5. atjava.net.DatagramSocket.<init>(UnknownSource)
  6. atjava.net.DatagramSocket.<init>(UnknownSource)
  7. atde.javawi.jstun.test.demo.ice.Candidate.<init>(Candidate.java:44)
  8. atde.javawi.jstun.test.demo.ice.ICENegociator.gatherCandidateAddresses(ICENegociator.java:89)
  9. 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构造函数:


Java代码 收藏代码
  1. publicCandidate(Addressaddress,shortcomponentId)throwsSocketException,UnknownHostException,UtilityException{
  2. this.socket=newDatagramSocket(0,address.getInetAddress());
  3. this.type=CandidateType.Local;
  4. this.componentId=componentId;
  5. this.priority=0;
  6. this.base=this;
  7. this.isInUse=false;
  8. }
  9. publicCandidate(Addressaddress,CandidateTypetype,shortcomponentId,Candidatebase)throwsSocketException,UnknownHostException,UtilityException{
  10. this.socket=newDatagramSocket(0,address.getInetAddress());
  11. this.type=type;
  12. setComponentId(componentId);
  13. this.priority=0;
  14. this.base=base;
  15. this.isInUse=false;
  16. }

在Candidate类的构造函数里,会创建一个DatagramSocket, DatagramSocket构造函数的第二参数是socket要绑定的local address. 对于host candidate, DatagramSocket的创建没有问题,因为host candidate就是local address; 但对于server reflexive candidate address, 问题出现了, 因为这个地址是NAT转换后的地址, socket无法绑定上去的.

所以改动如下, 判断CandidateType, 如果是ServerReflexive, 则不创建socket.主要的代码为:


Java代码 收藏代码
  1. publicclassDiscoveryInfo{
  2. .......
  3. privateintpublicPort;//用于保存NAT转换后的端口
  4. publicintgetPublicPort(){
  5. returnpublicPort;
  6. }
  7. publicvoidsetPublicPort(intpublicPort){
  8. this.publicPort=publicPort;
  9. }
  10. ......
  11. }
  12. publicclassICENegociator{
  13. .......
  14. publicvoidgatherCandidateAddresses(){
  15. ......
  16. DiscoveryTesttest=newDiscoveryTest(iaddress,stunServer,stunPort);
  17. DiscoveryInfodi=test.test();
  18. if(di.getPublicIP()!=null){
  19. Candidatecand=newCandidate(newAddress(di.getPublicIP().getAddress()),CandidateType.ServerReflexive,componentId,local);
  20. cand.setPort(di.getPublicPort());
  21. ......
  22. }
  23. }
  24. ......
  25. }
  26. publicclassDiscoveryTest{
  27. privatebooleantest1()throwsUtilityException,SocketException,UnknownHostException,IOException,MessageAttributeParsingException,MessageHeaderParsingException{
  28. ......
  29. di.setPublicIP(ma.getAddress().getInetAddress());
  30. di.setPublicPort(ma.getPort());
  31. ......
  32. }
  33. }
  34. publicclassCandidateimplementsComparable{
  35. ......
  36. privateintport;
  37. publicCandidate(Addressaddress,CandidateTypetype,shortcomponentId,Candidatebase)throwsSocketException,UnknownHostException,UtilityException{
  38. if(type==CandidateType.Local){
  39. this.socket=newDatagramSocket(0,address.getInetAddress());
  40. this.address=null;
  41. }else{
  42. this.address=address;
  43. this.socket=null;
  44. }
  45. this.type=type;
  46. setComponentId(componentId);
  47. this.priority=0;
  48. this.base=base;
  49. this.isInUse=false;
  50. }
  51. publicAddressgetAddress()throwsUtilityException{
  52. if(type==CandidateType.Local){
  53. returnnewAddress(socket.getLocalAddress().getAddress());
  54. }else{
  55. returnthis.address;
  56. }
  57. }
  58. publicintgetPort(){
  59. if(type==CandidateType.Local){
  60. returnsocket.getLocalPort();
  61. }else{
  62. returnthis.port;
  63. }
  64. }
  65. publicvoidsetPort(intport){
  66. this.port=port;
  67. }
  68. }


转自:http://mysuperbaby.iteye.com/blog/901370

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值