Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When the mqttv3 client connects() to a server where the message broker is not operating, it does not throw an exception. #1020

Open
1 of 3 tasks
angryEK opened this issue Nov 20, 2023 · 0 comments

Comments

@angryEK
Copy link

angryEK commented Nov 20, 2023

Please fill out the form below before submitting, thank you!

  • Bug exists Release Version 1.2.5 ( Master Branch)
  • Bug exists in MQTTv3 Client on Snapshot Version 1.2.6-SNAPSHOT (Develop Branch)
  • Bug exists in MQTTv5 Client on Snapshot Version 1.2.6-SNAPSHOT (Develop Branch)
public class Test {
    public static void main(String[] args) throws IOException, MqttException {

        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setUserName("test");
        mqttConnectOptions.setPassword("test".toCharArray());
        mqttConnectOptions.setCleanSession(true);
        mqttConnectOptions.setKeepAliveInterval(30);
        mqttConnectOptions.setConnectionTimeout(2);

        MqttAsyncClient mq = new MqttAsyncClient("tcp://127.0.0.1:61611","test", new MemoryPersistence());

        try {
            IMqttToken token = mq.connect(mqttConnectOptions);
            Thread.sleep(5000);
            System.out.println(token.isComplete());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The above example is a code that makes a connect request to any server without a message broker.

If you follow the code inside the library(package org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule(line:67 start())

	public void start() throws IOException, MqttException {
		final String methodName = "start";
		try {
			// @TRACE 252=connect to host {0} port {1} timeout {2}
			log.fine(CLASS_NAME,methodName, "252", new Object[] {host, Integer.valueOf(port), Long.valueOf(conTimeout*1000)});
			SocketAddress sockaddr = new InetSocketAddress(host, port);
			socket = factory.createSocket();
			socket.connect(sockaddr, conTimeout*1000);
			socket.setSoTimeout(1000);
		}
		catch (ConnectException ex) {
			//@TRACE 250=Failed to create TCP socket
			log.fine(CLASS_NAME,methodName,"250",null,ex);
			throw new MqttException(MqttException.REASON_CODE_SERVER_CONNECT_ERROR, ex);
		}
	}

you will call Socket connect.
Socket connect() for non-operating servers will cause ConnectException.
On the code, the generated ConnectException is thrown as MqttException.
But I can't get any reception, the project I made doesn't work normally.
Am I misunderstanding and using it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant