运行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;
-
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
分享到:
相关推荐
Android 链接openfire的smack库
smack开发文档借鉴.pdf
Notify to Jabber Sometimes a new giant smartphone is too big for me at parties. It's nice to be able to redirect all your messages to a...uses smack lib for this. Alternative: use your gotify server an
Smack is an open source, highly modular, easy to use, XMPP client library written in Java for Java SE compatible JVMs and Android. A pure Java library, it can be embedded into your applications to ...
Smack-3.4.1.tar.gz
Android是一种基于Linux内核(不包含GNU组件)的自由及开放源代码的移动操作系统,主要应用于移动设备,如智能手机和平板电脑。该系统最初由安迪·鲁宾开发,后被Google公司收购并注资,随后与多家硬件制造商、软件...
org.jivesoftware.smack-3.2.2.jar
基于openfire服务器利用Xmpp协议在Android上实现即时通讯 内含:smack.jar, smackx.jar, smackx-debug.jar, smackx-jingle.jar
java smack源码 Android 开发中的日常积累 Android 性能优化 Android 加固与反编译 Android Studio专题 Android 开发中值得看的优秀内容和工具 Android 开源软件 Android 开发辅助工具 Android 推送(含IM) Android...
网上的很多资料都是 Android平台下的开发。开始以为值适合Android平台,后台自己配置java平台的包
openfire客户端smack4.1.x开发所包含的完整依赖包(smack本身+外置libs),实测4.1.1完美运行。
基于XMPP 协议的Java聊天示例程序,已在本人机子上测试成功!
Smack API, 是一个 Java 的XMPP Client Library,也是由Jive Software开发。 优点:。 缺点:API并非为大量并发用户设计,每个客户要1个线程,占用资源大,1台机器只能模拟有限(数千个)客户. smack是一个用 java 写的...
openfire+spark+smack开发流程.pdf
Smack_API.chm Help Documents
java smack源码 概述 Jeejio JM SDK 致力于让第三方开发者开发基于 Jeejio 智能设备的应用时,快速集成与智能设备及其所属用户通信的能力。除了接收用户发送的文字、语音外,还可以处理规定格式的命令。 集成指南 ...
Java铃铛基于 LibJitsi、Ice4J 和 Smack 的 Java Jingle 实现Jitsi 实现了许多希望添加到 XMPP 库中的特性,包括 Jingle 和 ICE。 不幸的是,开发人员不容易访问这些功能,也没有很好的文档记录。 该项目旨在通过...
Smack帮助文档中文版.........................
smack4中文文档 基于samck官方最新文档翻译而成,适用于最新的Smack4.x 简介 6月毕业后来到帝都上班,找了一份Android开发的工作,公司开发的APP需要使用XMPP和Smack进行即时聊天,服务端和IOS平台都已经投入使用,...