paho.mqtt.java icon indicating copy to clipboard operation
paho.mqtt.java copied to clipboard

MqttException: Unable to connect to server

Open prisikarm opened this issue 4 years ago • 3 comments

  • [X] Bug exists Release Version 1.2.2 ( Master Branch)
  • [ ] Bug exists in MQTTv3 Client on Snapshot Version 1.2.3-SNAPSHOT (Develop Branch)
  • [ ] Bug exists in MQTTv5 Client on Snapshot Version 1.2.3-SNAPSHOT (Develop Branch)

Hello, I am trying to use Paho with TomCat server. While I am running the application locally, everything is fine, but once I deploy the app on the server (apache tomcat 8.0.48, jdk1.8.0_162) its not working.

Here are the logs: 11:47:52.406 [WEBSITE-startStop-7] INFO com.APP.mqtt.MQTTConfig - Try to connect to tcp://m21.cloudmqtt.com:16952 11:47:52.529 [WEBSITE-startStop-7] INFO com.APP.mqtt.MQTTConfig - CONNECTING TO MQTT 11:47:52.551 [WEBSITE-startStop-7] INFO com.APP.mqtt.MQTTConfig - CONNECTED 11:47:52.551 [WEBSITE-startStop-7] INFO com.APP.mqtt.MQTTConfig - wait for completion reached 11:47:54.617 [WEBSITE-startStop-7] ERROR com.APP.mqtt.MQTTConfig - ERROR(Connection) org.eclipse.paho.client.mqttv3.MqttException: Unable to connect to server at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:80) at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:722) at java.base/java.lang.Thread.run(Thread.java:844) Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400) at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243) at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225) at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402) at java.base/java.net.Socket.connect(Socket.java:591) at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:74) ... 2 common frames omitted

Here is the code:

void config(){ String brokerUrl = "tcp://" + this.broker + ":" + port; logger.info("Try to connect to " + brokerUrl); MqttConnectOptions connectionOptions = new MqttConnectOptions(); try { this.mqttClient = new MqttAsyncClient(brokerUrl, clientId, new MemoryPersistence()); connectionOptions.setCleanSession(true); connectionOptions.setAutomaticReconnect(true); if (password != null) { connectionOptions.setPassword(this.password.toCharArray()); } if (userName != null) { connectionOptions.setUserName(this.userName); } logger.info("CONNECTING TO MQTT"); IMqttToken token = this.mqttClient.connect(connectionOptions); logger.info("CONNECTED"); logger.info("wait for completion reached"); token.waitForCompletion(); logger.info("CONNECTION DONE"); this.mqttClient.setCallback(this); } catch (Exception me) { logger.error("ERROR(Connection)", me); } }

Please help :(

prisikarm avatar Apr 02 '20 17:04 prisikarm

You are getting "Connection refused" error. This could be MQTT broker configuration issue (may be it is not allowing connection from a remote system) or the connection is getting blocked by firewall.

rdasgupt avatar Apr 02 '20 20:04 rdasgupt

@rdasgupt thank you for the fast reply. We already tried with https://test.mosquitto.org/ broker and with mosquito running on the raspberry pi, same error. We even tried with the different tomcat hosting, but still the same error...

prisikarm avatar Apr 02 '20 20:04 prisikarm

I sometimes (not always) get the same error using mosquitto version 2.0.11 on a Raspberry PI 5 with the latest openHABian version. The client is a self written Java (openjdk 11.0.18) program running on Windows 11.

hermann-59 avatar Apr 18 '24 00:04 hermann-59