Search code examples
mqttpahomqtt-vernemq

"No connection could be made because the target machine actively refused it" connecting to VerneMQ


I installed VerneMQ on an Azure Ubuntu 16.04 VM, and opened port 1883 inbound and outbound. VerneMQ is configured to listen on port 1883, has allow anonymous connections turned on, and vernemq has been started (vernemq start).

allow_anonymous = on

listener.tcp.default = 127.0.0.1:1883

I have created a C# console app on my Windows 10 PC to send messages to the MQTT broker on the VM. I am using NuGet package M2Mqtt version 4.3.0 and have opened 1883 both inbound and outbound in my Windows 10 firewall.

string broker = "<ip address>";
MqttClient client = new MqttClient(broker);
byte code = client.Connect(Guid.NewGuid().ToString());

I am getting connection refused error message when I try to connect.

uPLibrary.Networking.M2Mqtt.Exceptions.MqttConnectionException: 'Exception connecting to the broker'

uPLibrary.Networking.M2Mqtt.Exceptions.MqttConnectionException
HResult=0x80131500 Message=Exception connecting to the broker
Source=M2Mqtt.Net StackTrace: 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) at MQTTSendReceive.MQTT.SendMQTTMessage() in C:\Projects\19 10 12 MQTT\MQTTSendReceive\MQTTSendReceive\MQTT.cs:line 17 at MQTTSendReceive.Program.Main(String[] args) in C:\Projects\19 10 12 MQTT\MQTTSendReceive\MQTTSendReceive\Program.cs:line 13

Inner Exception 1: SocketException: No connection could be made because the target machine actively refused it :1883

I can't see where I am blocked.


Solution

  • You have told VerneMQ to listen on localhost (127.0.0.1) which means you will only be able to connect to it from the Ubuntu machine.

    If you want to be able to access it from elsewhere you need to tell it to listen on all interfaces (0.0.0.0)

    allow_anonymous = on
    
    listener.tcp.default = 0.0.0.0:1883