I'm very new to MQTTnet, and I part of my program needs a MQTT broker to interact with a third-party MQTT client that may or may not listen on a local port. In case of a local port, all I need to do is to create a local MQTT broker based on examples I found online:
using MQTTnet;
using MQTTnet.Server;
using System.Net;
MqttServerOptionsBuilder options = new MqttServerOptionsBuilder()
.WithDefaultEndpoint()
.WithDefaultEndpointPort(1883); // I know this is the default, but if port number isn't 1883 I would specify it here
MqttServer mqttServer = new MqttFactory().CreateMqttServer(options.Build());
mqttServer.StartAsync().GetAwaiter();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
And that works, no problem.
Now what I want to is that, instead of the server to connect locally, I want it to connect to a virtual machine with a fictitious IP address of 10.100.10.100.
I haven't found a documentation or tutorial that explains clear enough how exactly should I do this, so I made the following hypothesis:
using MQTTnet;
using MQTTnet.Server;
using System.Net;
using System.Net.Security;
IPAddress ipAddress = new IPAddress(new byte[4] { 10, 100, 10, 100 });
RemoteCertificateValidationCallback callback = (message, cert, chain, sslPolicyErrors) =>
{
return true;
};
MqttServerOptionsBuilder options = new MqttServerOptionsBuilder()
.WithDefaultEndpointBoundIPAddress(new IPAddress(new byte[4] { 10, 100, 10, 100 }))
.WithRemoteCertificateValidationCallback(callback)
.WithDefaultEndpointPort(1883);
MqttServer mqttServer = new MqttFactory().CreateMqttServer(options.Build());
mqttServer.StartAsync().GetAwaiter();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
This didn't work. Not if I run the program locally from 100.10.100.10, nor if I run it from another machine. When I go through the list of all connections with command line netstat -ab, there's nothing connected on the ports I specified.
So I tried to instead keep the line .WithDefaultEndpoint():
//...
MqttServerOptionsBuilder options = new MqttServerOptionsBuilder()
.WithDefaultEndpointBoundIPAddress(new IPAddress(new byte[4] { 10, 100, 10, 100 }))
.WithRemoteCertificateValidationCallback(callback)
.WithDefaultEndpoint()
.WithDefaultEndpointPort(1883);
//...
Now at least it gets working when running locally from 100.10.100.10 (of course it would...), but still it doesn't help with my initial problem trying to connect to the port from an outside machine.
Any ideas? Thanks in advance.