diff --git a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs index 4663ac62b..9a2b92f5a 100644 --- a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs @@ -376,20 +376,9 @@ public MqttClientOptionsBuilder WithTcpServer(string host, int? port = null, Add _tcpOptions = new MqttClientTcpOptions(); - _port = port; - // The value 0 will be updated when building the options. // This a backward compatibility feature. - - if (IPAddress.TryParse(host, out var ipAddress)) - { - _remoteEndPoint = new IPEndPoint(ipAddress, port ?? 0); - } - else - { - _remoteEndPoint = new DnsEndPoint(host, port ?? 0, addressFamily); - } - + _remoteEndPoint = new DnsEndPoint(host, port ?? 0, addressFamily); _port = port; return this; diff --git a/Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs b/Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs index 59483e896..4b6402cff 100644 --- a/Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs +++ b/Source/MQTTnet/Client/Options/MqttClientTcpOptions.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Net; using System.Net.Sockets; @@ -20,6 +21,12 @@ public sealed class MqttClientTcpOptions : IMqttClientChannelOptions /// public bool? DualMode { get; set; } + [Obsolete("Use RemoteEndpoint or MqttClientOptionsBuilder instead.")] + public int? Port { get; set; } + + [Obsolete("Use RemoteEndpoint or MqttClientOptionsBuilder instead.")] + public string Server { get; set; } + public LingerOption LingerState { get; set; } = new LingerOption(true, 0); /// diff --git a/Source/MQTTnet/Implementations/MqttTcpChannel.cs b/Source/MQTTnet/Implementations/MqttTcpChannel.cs index cbce5f39f..f82f36165 100644 --- a/Source/MQTTnet/Implementations/MqttTcpChannel.cs +++ b/Source/MQTTnet/Implementations/MqttTcpChannel.cs @@ -16,6 +16,7 @@ using MQTTnet.Client; using MQTTnet.Exceptions; using MQTTnet.Internal; +using MQTTnet.Protocol; namespace MQTTnet.Implementations { @@ -98,6 +99,29 @@ public async Task ConnectAsync(CancellationToken cancellationToken) socket.DualMode = _tcpOptions.DualMode.Value; } + // This block is only for backward compatibility. + if (_tcpOptions.RemoteEndpoint == null && !string.IsNullOrEmpty(_tcpOptions.Server)) + { + int port; + if (_tcpOptions.Port.HasValue) + { + port = _tcpOptions.Port.Value; + } + else + { + if (_tcpOptions.TlsOptions?.UseTls == true) + { + port = MqttPorts.Secure; + } + else + { + port = MqttPorts.Default; + } + } + + _tcpOptions.RemoteEndpoint = new DnsEndPoint(_tcpOptions.Server, port, AddressFamily.Unspecified); + } + await socket.ConnectAsync(_tcpOptions.RemoteEndpoint, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested();