4

I have a project on Android, which needs to use asmack library to enable real time chat.

The key feature is reconnecting mechanism, that means my application can reconnect automatically whenever the internet connection loses.

Here is my code snippet:

ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT);
connConfig.setSASLAuthenticationEnabled(true);
connConfig.setReconnectionAllowed(true);
XMPPConnection connection = new XMPPConnection(connConfig);

But I got this error whenever I turned off then turned on Wifi:

10-01 21:43:26.942: W/System.err(13695): javax.net.ssl.SSLException: Write error: ssl=0x5a4ad348: I/O error during system call, Broken pipe
10-01 21:43:27.002: W/System.err(13695):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_write(Native Method)
10-01 21:43:27.012: W/System.err(13695):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:693)
10-01 21:43:27.012: W/System.err(13695):    at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)
10-01 21:43:27.012: W/System.err(13695):    at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158)
10-01 21:43:27.012: W/System.err(13695):    at java.io.BufferedWriter.flush(BufferedWriter.java:124)
10-01 21:43:27.012: W/System.err(13695):    at org.jivesoftware.smack.PacketWriter.writePackets(PacketWriter.java:210)
10-01 21:43:27.012: W/System.err(13695):    at org.jivesoftware.smack.PacketWriter.access$000(PacketWriter.java:42)
10-01 21:43:27.012: W/System.err(13695):    at org.jivesoftware.smack.PacketWriter$1.run(PacketWriter.java:78)
10-01 21:43:27.092: W/System.err(13695): javax.net.ssl.SSLException: Read error: ssl=0x5a4ad348: I/O error during system call, Connection timed out
10-01 21:43:27.092: W/System.err(13695):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(Native Method)
10-01 21:43:27.092: W/System.err(13695):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:651)
10-01 21:43:27.092: W/System.err(13695):    at java.io.InputStreamReader.read(InputStreamReader.java:244)
10-01 21:43:27.092: W/System.err(13695):    at java.io.BufferedReader.read(BufferedReader.java:310)
10-01 21:43:27.092: W/System.err(13695):    at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1496)
10-01 21:43:27.092: W/System.err(13695):    at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:979)
10-01 21:43:27.092: W/System.err(13695):    at org.kxml2.io.KXmlParser.next(KXmlParser.java:346)
10-01 21:43:27.092: W/System.err(13695):    at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
10-01 21:43:27.092: W/System.err(13695):    at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:326)
10-01 21:43:27.092: W/System.err(13695):    at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)
10-01 21:43:27.092: W/System.err(13695):    at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:71)

Anyone, who can solve this error, please help me!

duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
  • 1
    Please share what version of the aSmack library you are using. I'm using asmack-android-18-0.8.9beta4.jar, using your code snippet above, and I don't see the error you are getting when I turn off and on wifi on the Nexus3 phone I'm using . – Gene Myers Jan 19 '14 at 18:57
  • Actually I don't use asmack library any more :). Anyway, thank you so much! – duong_dajgja Jan 21 '14 at 07:48
  • I'm curious, what are you using now? – Gene Myers Jan 21 '14 at 20:58
  • I want my server to be able to send some message to clients (Android) actively so I considered to use either GCM or XMPP. I tried using both of them then compared their delays. I found that in most of cases the delay of XMPP is smaller but the delay of GCM is acceptable (at least in my application). So that I decided to use GCM instead of XMPP :) – duong_dajgja Jan 23 '14 at 08:23

4 Answers4

10

Use the latest smack api - I am using 4.1.0

Then set up re-connection like below:

ReconnectionManager manager = ReconnectionManager.getInstanceFor(connection);
manager.enableAutomaticReconnection();

It will automatically re-connect as described Here

J. Musyoka
  • 111
  • 1
  • 4
  • do you know if you have to do it when you are already connected? In case you have no internet connection when you try to connect, do you have to try to connect without the reconnectionmanager or it will do it itself? – Kasas Aug 17 '15 at 14:12
0

Asmack is supporting Re-connection properly, only problem is that You are using SASL Connection so this is giving problem for because you are trying same connection again on turn Wi-Fi off.Try with set SASL false

connConfig.setSASLAuthenticationEnabled(false);

required other wise disconnect manually

connection.disconnect();
Mandeep
  • 140
  • 3
  • 8
0

When setting up everything at the start, call

ReconnectionManager.setEnabledPerDefault(true);

Then connections created from then on will automatically reconnect.

MrBigglesworth
  • 350
  • 1
  • 8
-1

Don't use reconnection mechanism which is implemented in ASMACK.

Use:

    connConfig.setReconnectionAllowed(false);

Instead, you should create BroadcastReceiver which listens to ConnectivityManager.CONNECTIVITY_ACTION. When connection to WIFI or other network is occurred, then you will receive the callback onReceive in your BroadcastReceiver.

Eugene
  • 17
  • 4
  • This way is not good. You can check with this situation: your smartphone connects to an AP, if this AP looses interconnection (let's assume you unplug the wired cable from the AP), in this case your smartphone will not realize the change of internet connection. – duong_dajgja Nov 22 '13 at 13:52
  • @duong_dajgja You are right, but there was a bug in the reconnection manager in the Asmack. Maybe this bug is fixed in the latest releases. Moreover, if the reconnection manager is fixed, then you should combine between two techniques in order not to try reconnect when there is no active network. Does it happen when you call: connection.connect(); – Eugene Nov 24 '13 at 17:01