SocketException - 在Smack 4.1.7中丢弃网络连接

时间:2016-07-15 11:10:29

标签: xmpp ejabberd smack asmack

我在Android中创建了一个聊天应用程序,并使用Smack API(4.1.7)与Chat Server进行通信。

在Android设备中,如果我切换到飞行模式或连接丢弃,获取 SocketException

请指导我防止此异常

W/AbstractXMPPConnection: Connection closed with error
    java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
    at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:592)
    at libcore.io.IoBridge.recvfrom(IoBridge.java:556)
    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
    at java.io.InputStreamReader.read(InputStreamReader.java:231)
    at java.io.BufferedReader.read(BufferedReader.java:325)
    at org.jivesoftware.smack.util.ObservableReader.read(ObservableReader.java:41)
    at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1515)
    at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:992)
    at org.kxml2.io.KXmlParser.next(KXmlParser.java:349)
    at org.kxml2.io.KXmlParser.next(KXmlParser.java:313)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1173)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$300(XMPPTCPConnection.java:952)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:967)
    at java.lang.Thread.run(Thread.java:818)
    Caused by: android.system.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
    at libcore.io.Posix.recvfromBytes(Native Method)
    at libcore.io.Posix.recvfrom(Posix.java:185)
    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
    at libcore.io.IoBridge.recvfrom(IoBridge.java:553)

这是我的连接代码

XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setServiceName(UserController.getHost());
configBuilder.setHost(UserController.getHost());
configBuilder.setPort(UserController.getPort());
configBuilder.setSendPresence(true);

configBuilder.setDebuggerEnabled(true);
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
connection = new XMPPTCPConnection(configBuilder.build());

// XMPP Connection Listener monitor
connection.addConnectionListener(new ConnectionListener() {
@Override
public void connected(XMPPConnection connection) {
    service.sendConnectionUpdate(WhatTimeGlobal.SERVER_CONNECTED);
}

@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
    service.sendConnectionUpdate(WhatTimeGlobal.SERVER_CONNECTED);
}

@Override
public void connectionClosed() {

}

@Override
public void connectionClosedOnError(Exception e) {
    if (CommonUtils.isInternetAvailable() && connection != null) {
        XMPPService.getInstance().loginIntoServer();
    }
}

@Override
public void reconnectionSuccessful() {
    Log.i("", "Successfully reconnected to the XMPP server.");
}

@Override
public void reconnectingIn(int seconds) {
    Log.i("", "Reconnecting in " + seconds + " seconds.");
}

@Override
public void reconnectionFailed(Exception e) {
    XMPPService.getInstance().loginIntoServer();
}
});

// Configure the Auth Mechanism - Current moment set it as PLAIN
configureAuthMethod();
connection.setUseStreamManagement(true);
XMPPTCPConnection.setUseStreamManagementResumptionDefault(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);
connection.setPacketReplyTimeout(100000);

Roster roster = Roster.getInstanceFor(connection);
roster.setRosterLoadedAtLogin(false);
connection.connect();

1 个答案:

答案 0 :(得分:1)

您无法阻止此例外。你应该处理它(例如安排重新连接)。