Connection lost (32109) - java.io.EOFException (MqttAndroidClient)
<p>P.S. Please read the whole question before concluding anything.</p>

<p>I am getting the following exception whenever connect method is called:</p>

<blockquote>
  <p>Connection lost (32109) - java.io.EOFException
  at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
  at java.lang.Thread.run(Thread.java:818)
  Caused by: java.io.EOFException
  at java.io.DataInputStream.readByte(DataInputStream.java:77)
  at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
  at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)</p>
</blockquote>

<p>I have tried couple of code snippets but both of them are raising the same exception.</p>

<p><strong>Method 1</strong></p>

<pre><code>private final String serverUri = "tcp://95.177.135.232:9001";

private void mqqtConnectAndroidClient() {
    String clientId = "clientID-" + MqttClient.generateClientId();
    mqttAndroidClient = new MqttAndroidClient(getMainActivity(), serverUri, clientId);
    MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
    mqttConnectOptions.setKeepAliveInterval(60);
    mqttConnectOptions.setCleanSession(true);
    mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
    try {
        mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                subscribeToTopic();
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                AppLog.Error("Failed to connect to: " + serverUri);
                exception.printStackTrace();
            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }
}
</code></pre>

<p><strong>Method 2</strong></p>

<pre><code>private final String serverUri = "tcp://95.177.135.232:9001";

private void methodTwo() {
    try {
        MqttDefaultFilePersistence mdfp = new MqttDefaultFilePersistence(MQTT_DIR);
        mqttClient = new MqttClient(serverUri, "1", mdfp);
        MqttConnectOptions options = new MqttConnectOptions();
        options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
        options.setKeepAliveInterval(60);
        options.setAutomaticReconnect(true);
        options.setCleanSession(true);
        mqttClient.connect(options);
        mqttClient.subscribe("#");
        mqttClient.setCallback(new MqttCallback() {

            @Override
            public void connectionLost(Throwable arg0) {
                AppLog.Error("Connection lost");
                try {
                    mqttClient.connect();
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken arg0) {
                AppLog.Error("deliveryComplete");
            }

            @Override
            public void messageArrived(String arg0, MqttMessage arg1) throws Exception {
                AppLog.Error("messageArrived");
                AppLog.Error(arg1.toString());
            }
        });
    } catch (MqttException e) {
        AppLog.Error("Main Exception");
        e.printStackTrace();
    }
}
</code></pre>

<p><strong>Subscribe Method</strong></p>

<pre><code>public void subscribeToTopic() {
    try {
        String subscriptionTopic = "#";
        mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                AppLog.Error("Subscribed!");
            }
            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                AppLog.Error("Failed to subscribe");
            }
        });
    } catch (MqttException ex) {
        System.err.println("Exception whilst subscribing");
        ex.printStackTrace();
    }
}
</code></pre>

<p>If I try the same server URI at <a href="http://www.hivemq.com/demos/websocket-client/" rel="nofollow noreferrer">HiveMQ</a>, It is working perfectly fine with proper connection and subscription is also giving me results. I am using the following two libraries:</p>

<blockquote>
  <p>compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
  compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'</p>
</blockquote>

<p>I tried some of the SO answers likes <a href="https://stackoverflow.com/questions/24258887/mqtt-on-android-connection-lost">One</a>, <a href="https://stackoverflow.com/questions/36176056/paho-mqtt-android-connecting-to-broker-fails-with-32103?rq=1">Two</a>, <a href="https://stackoverflow.com/questions/38799690/paho-mqtt-callbacks-in-different-classes-android-java?rq=1">Three</a>, <a href="https://stackoverflow.com/questions/24791118/android-paho-mqtt-service-for-publishing?rq=1">Four</a> and <a href="https://stackoverflow.com/questions/28543569/paho-mqtt-android-service-issue?noredirect=1&amp;lq=1">Five</a> but none of them are working for me.</p>

<p>Any help will be appreciated. Thank you in advance!</p>