0

I'm developing a really simple MQTT Android client, so I can connect to it with an MQTT Server I created in C# using the MQTTnet library.

I first tested the C# broker with a C# client and Node-RED and it worked just fine. I also tested the Android Client with CloudMQTT following this tutorial, and it also worked perfectly fine.

But when I tried to connect my Android Client to the C# Server, the Android Client gave me the following error:

Mqtt: Failed to connect to: tcp://localhost:1883 It's not possible to connect to server (32103) - java.net.ConnectException: Connection refused

I'm using BlueStacks as an emulator (I will try to test it in a real Android device asap). I tried to restart the emulator (as said in Paho Mqtt Android connecting to broker fails with (32103)) but it didn't work either.

The code for the Android Client is exactly the same as in the tutorial I mentioned earlier (using the Paho-MQTT library), but changing the serverUri and subscription topic:

final String serverUri = "tcp://localhost:1883";
final String subscriptionTopic = "step/time";

The app only has a TextView where I set the received messages.

As for the C# server, I'm sending a timestamp every 10 seconds. This is my code:

namespace MQTTServerExample
{
    class Program
    {
        static void Main(string[] args)
        {
            serverAsync();
        }

        private static async Task serverAsync()
        {
            // Starting the MQTT Server
            var mqttServer = new MqttFactory().CreateMqttServer();
            var options = new MqttServerOptions();

            //Saving retained application messages
            options.Storage = new RetainedMessageHandler();

            // Publishing messages
            await mqttServer.StartAsync(options);
            Console.WriteLine("### SERVER CONNECTED ###");
            Console.WriteLine("Press ENTER to exit.");
            MqttApplicationMessage message;

#pragma warning disable CS4014
            Task.Run(async () =>
             {
                 while (true)
                 {
                     message = new MqttApplicationMessageBuilder()
                             .WithTopic("step/time")
                             .WithPayload(DateTime.Now.ToString())
                             .WithExactlyOnceQoS()
                             .WithRetainFlag(true)
                             .Build();
                     await mqttServer.PublishAsync(message);
                     await Task.Delay(10000); // Every 10 seconds
                 }
             });
#pragma warning restore CS4014

            //await mqttServer.PublishAsync(message);

            Console.ReadKey();
            await mqttServer.StopAsync();
        }
    }

I'm new to connection protocols and I still don't understand them clearly, so I was hoping you could help me understand this problem.

James Z
  • 12,209
  • 10
  • 24
  • 44
Biel
  • 193
  • 1
  • 3
  • 15
  • I have found connecting an emulator to a local server to be tricky. It's likely once you get the right address it will work on a real device. – nasch Feb 16 '19 at 16:57

2 Answers2

2

A java.net.ConnectException: Connection refused exception means that there is no service listening on the host and port you are trying to connect to, for example because the host address or port is wrong, or the service is not started.

Using "tcp://localhost:1883" as server address only works if the server runs on the same machine as the client (i.e. in your case the Android device). You should use the server's name or IP address.

bwt
  • 17,292
  • 1
  • 42
  • 60
  • Hi and thank you!! I actually tried it with my machine's IP and it gave me the same error, so I'm not sure why this was happening. – Biel Feb 15 '19 at 13:46
  • So it works from a client in C#, but not from an Android client ? Is the C# client on the same machine as the server ? If so it could be a firewall problem – bwt Feb 15 '19 at 15:06
  • Exactly! I guess it must be a firewall problem then. I'll try this on Monday and get back to you. Thank you! – Biel Feb 16 '19 at 07:10
2

I'm going to guess that your C# broker is only listening on localhost not the external IP address of the machine hosting it.

This would work when ran the C# client on the same machine, but not when you use the external IP address from the Android client.

The other possible option is that you are running a firewall on the broker machine which is stopping the Android client connecting.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • In case it was the first option, how could I make the broker to listen to external IPs too? I'll look into these two options on Monday and get back to you, thank you so much! – Biel Feb 16 '19 at 07:11