I am working on a chat application using asmack. I am able to establish xmpp connection and can send and receive messages properly. However, I wish to reconnect the XMPP connection if I change the internet connectivity from WIFI to #G and vice-versa. To listen to connectivity change, I have a broadcastreceiver which successfully notifies about connection changed. If I get connectivity as false, I check the xmpp connection for null or not null. If it's not null, I set it as null. And when internet is connected, I try to reconnect the xmpp connection. The main thing is that I start my app for first time, the xmpp connection is built successfully and the TLS handshake is done properly. I have specified the truststore logic as follows:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
connectionConfig.setTruststoreType("AndroidCAStore");
connectionConfig.setTruststorePassword(null);
connectionConfig.setTruststorePath(null);
}
else
{
connectionConfig.setTruststoreType("BKS");
String path = System.getProperty("javax.net.ssl.trustStore");
if(path == null)
{
path = System.getProperty("java.home") + File.separator + "etc" + File.separator + "security" + File.separator + "cacerts.bks";
}
connectionConfig.setTruststorePath(path);
}
This works fine for the first time and the TLS handshake gives me below result in logcat:
But when I try to reconnect as explained above, I do not get the success tag as received previously. Instead the errors received are as follows:
02-19 17:59:39.098: W/System.err(21977): java.net.SocketException: Socket closed
02-19 17:59:39.133: W/System.err(21977): at libcore.io.Posix.sendtoBytes(Native Method)
02-19 17:59:39.133: W/System.err(21977): at libcore.io.Posix.sendto(Posix.java:146)
02-19 17:59:39.137: W/System.err(21977): XMPPError establishing connection with server.: remote-server-error(502) XMPPError establishing connection with server.
02-19 17:59:39.141: W/System.err(21977): -- caused by: javax.net.ssl.SSLException: Unable to create application data
02-19 17:59:39.160: W/System.err(21977): at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
02-19 17:59:39.160: W/System.err(21977): at libcore.io.IoBridge.sendto(IoBridge.java:473)
02-19 17:59:39.160: W/System.err(21977): at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
02-19 17:59:39.160: W/System.err(21977): at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
02-19 17:59:39.160: W/System.err(21977): at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
02-19 17:59:39.160: W/System.err(21977): at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)
02-19 17:59:39.160: W/System.err(21977): at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158)
02-19 17:59:39.160: W/System.err(21977): at java.io.BufferedWriter.flush(BufferedWriter.java:124)
02-19 17:59:39.160: W/System.err(21977): at org.jivesoftware.smack.util.ObservableWriter.flush(ObservableWriter.java:48)
02-19 17:59:39.160: W/System.err(21977): at org.jivesoftware.smack.PacketWriter.writePackets(PacketWriter.java:196)
02-19 17:59:39.160: W/System.err(21977): at org.jivesoftware.smack.PacketWriter.access$000(PacketWriter.java:40)
02-19 17:59:39.160: W/System.err(21977): at org.jivesoftware.smack.PacketWriter$1.run(PacketWriter.java:76)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.XMPPConnection.initReaderAndWriter(XMPPConnection.java:699)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.XMPPConnection.proceedTLSReceived(XMPPConnection.java:835)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:262)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:69)
02-19 17:59:39.164: W/System.err(21977): Nested Exception:
02-19 17:59:39.164: W/System.err(21977): javax.net.ssl.SSLException: Unable to create application data
02-19 17:59:39.164: W/System.err(21977): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
02-19 17:59:39.164: W/System.err(21977): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
02-19 17:59:39.164: W/System.err(21977): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:647)
02-19 17:59:39.164: W/System.err(21977): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:618)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.XMPPConnection.initReaderAndWriter(XMPPConnection.java:666)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.XMPPConnection.proceedTLSReceived(XMPPConnection.java:835)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:262)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
02-19 17:59:39.164: W/System.err(21977): at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:69)
Please help me to reconnect a failed xmpp connection and also let me know if there is any way to retrieve a session id so that I may check with the server whether any xmpp connection is alive with respect to the requested session id.