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

Unable to connect to MQTT broker: MqttCommunicationException #21

Open
MattHoneycutt opened this issue Nov 23, 2016 · 10 comments
Open

Unable to connect to MQTT broker: MqttCommunicationException #21

MattHoneycutt opened this issue Nov 23, 2016 · 10 comments

Comments

@MattHoneycutt
Copy link

I'm getting a generic "Exception of type 'uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException' was thrown." while trying to connect to an MQTT broker. I can connect to the broker just fine using MQTT.fx, and I can connect to another MQTT broker just fine using the M2MQTT libraries, so I'm thinking that the client isn't compatible with this particular broker for some reason.

Is there some way to get additional logging details or a deeper view into what's going wrong?

@xingzhougmu
Copy link

xingzhougmu commented Sep 5, 2017

@MattHoneycutt I have similar issue. my broker is talking over ssl. On server side (broker side) I used self-signed cert signed by self-signed CA. On client side, I install the CA cert in the trusted authority. However, I always get the generic uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException.

{uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException: Exception of type 'uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException' was thrown.
at uPLibrary.Networking.M2Mqtt.MqttClient.SendReceive(Byte[] msgBytes, Int32 timeout)
at uPLibrary.Networking.M2Mqtt.MqttClient.Connect(String clientId, String username, String password, Boolean willRetain, Byte willQosLevel, Boolean willFlag, String willTopic, String willMessage, Boolean cleanSession, UInt16 keepAlivePeriod)
at uPLibrary.Networking.M2Mqtt.MqttClient.Connect(String clientId, String username, String password)

The code to create the client is as below:

X509Certificate2 caCert = new X509Certificate2(Directory.GetCurrentDirectory() + "/Certs/Development/RootCA.crt");
                MqttClient client = new MqttClient(brokerHostName, 443, true, caCert, null, MqttSslProtocols.TLSv1_2);
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId, userName, password);

By checking the network trace, it seems the SSL channel has been established. However, after some time (few seconds) the connection is failed with communication error.

@amguilmet
Copy link

amguilmet commented May 28, 2018

I'm having this exact same issue, as well. I'm using Mosquitto 0.15, M2Mqtt 4.3.0 and MQTT.fx 1.5. When connecing with MQTT.fx, I can connect to mosquitto but only with v3.1. If I use v3.1.1 instead, it immediately looses the connection. When connecting to mosquitto via m2mqtt, I get the same error message in the logs as when I try MQTT.fx with v3.1.1.

`1527514650: New connection from 127.0.0.1.

1527514650: Invalid protocol "MQTT" in CONNECT from 127.0.0.1.
1527514650: Socket read error on client (null), disconnecting.`

Before the m2mqtt client throws the MqttCommunicationException exception, it pegs the CPU out at 100%

What I did to 'fix' this issue was force m2mqtt to use v3.1 with

client.ProtocolVersion = MqttProtocolVersion.Version_3_1

That seemed to make it work.

@ghost
Copy link

ghost commented Sep 11, 2018

Hello, I'm also having this issue and the fix proposed by amguilmet didn't work. Does anyone have additional suggestions?

@benjamincoven
Copy link

Hello everyone, I am working on Unity for Hololens, and I am facing the same problem whatever the server is active or not. The problem only happens on Hololens and not in my desktop Unity Editor. Thank you.

@dillipdala
Copy link

Hello everyone
i am working with uPLibrary.Networking.M2Mqtt library to connect aws iot device but facing problem when try to connect IotMqttClient.Connect("teleConnect") .

var CaCert = X509Certificate.CreateFromCertFile(@"D:\Aws\root-CA.crt");
X509Certificate2 clientCert = new X509Certificate2(@"D:\Aws\04d500d157.pfx","password");

        MqttClient IotMqttClient = new MqttClient(AwsEndPoint, 8883, true, CaCert, clientCert, MqttSslProtocols.TLSv1_2);
           IotMqttClient.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
           IotMqttClient.MqttMsgSubscribed += Client_MqttMsgSubscribed;
        IotMqttClient.Connect("teleConnect");

Error:
An unhandled exception of type 'uPLibrary.Networking.M2Mqtt.Exceptions.MqttConnectionException' occurred in M2Mqtt.Net.dll

please help me
thank you

@rusty-boffin
Copy link

I'm also getting this exception - I'm trying to connect to www.mosquitto.org (without SSL) and get the exception 35 secs after the call to .Connect(). As well as this exception, when I try to close my WPF app, the window disappears by the process remains, and I have to kill it. The debugger shows me that there's a worker thread stuck in Socket.Receive().
The interesting thing is that this doesn't always happen - sometimes the client connects OK and everything works fine. I suspect that the problem comes when I terminate my app without disconnecting the client (typically during a debugging session). Is it possible that the broker has a problem with dropped connections?

@inforaudio
Copy link

Hello
somebody have some solution??
We test with mqttClient.ProtocolVersion = MqttProtocolVersion.Version_3_1; and nothing

The server detect a new conection but the client cant connect
VisualStudio show
uPLibrary.Networking.M2Mqtt.Exceptions.MqttConnectionException: Exception connecting to the broker

@rauleteee
Copy link

@inforaudio did you find the solution? I am having the same issue

@tejswiniK
Copy link

Hello Everyone,
Facing same exception. Did anyone found solution?

@rakesh5283
Copy link

seems no one have the solution yet ?

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

10 participants