我在客户端使用smack api建立与openfire的连接。能够生成成功的连接并且可以聊天。但是在聊了一段时间之后我得到了以下异常。
AbstractXMPPConnection: Connection closed with error
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at crittercism.android.m.read(Unknown Source)
at java.io.InputStreamReader.read(InputStreamReader.java:233)
at java.io.BufferedReader.read(BufferedReader.java:325)
at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1506)
at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:986)
at org.kxml2.io.KXmlParser.next(KXmlParser.java:346)
at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1151)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:141)
at libcore.io.BlockGuardOs.recvfrom(BlockGua
通过对相关帖子的几个答案,但在我的方案中没有找到此异常的实际原因。我使用以下代码与openfire建立连接:
try { XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword(userName+Constants.openFireIP, password)
.setServiceName("182.**.***.**")
.setHost("182.**.***.**")
.setPort(5222)
.setResource("/Smack")
.build();
connecton = new XMPPTCPConnection(config);
System.out.println("connecton ######## "+connecton);
makeConnectionAvailable();
}
catch(Exception e){
System.out.println("Exception "+e.getMessage());
e.printStackTrace();
}
public void makeConnectionAvailable(){
try{
if(!connecton.isConnected()){
connecton.connect();
connecton.login();
connecton.addConnectionListener(connectionListener);
}else if(!connecton.isAuthenticated()){
connecton.login();
}
}
catch (Exception e){
e.printStackTrace();
}
}
由于此连接已终止并丢失传入的聊天消息,直到我再次建立连接。如果有人解决这类问题,请告诉我。