File tree Expand file tree Collapse file tree 1 file changed +15
-6
lines changed
Source/HiveMQtt/Client/Transport Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change 15
15
*/
16
16
namespace HiveMQtt . Client . Transport ;
17
17
18
+ using System . Data ;
18
19
using System . IO . Pipelines ;
19
20
using System . Net ;
20
21
using System . Net . Security ;
@@ -160,17 +161,25 @@ private async Task<bool> CreateTLSConnectionAsync(Stream stream)
160
161
/// <returns>A boolean representing the success or failure of the operation.</returns>
161
162
public override async Task < bool > ConnectAsync ( CancellationToken cancellationToken = default )
162
163
{
163
- IPEndPoint ipEndPoint ;
164
- var ipAddress = await LookupHostNameAsync ( this . Options . Host , this . Options . PreferIPv6 ) . ConfigureAwait ( false ) ;
164
+ IPEndPoint ? ipEndPoint = null ;
165
165
166
- // Create the IPEndPoint depending on whether it is a host name or IP address.
167
- if ( ipAddress == null )
166
+ if ( IPAddress . TryParse ( this . Options . Host , out var parsedIp ) )
168
167
{
169
- ipEndPoint = new IPEndPoint ( IPAddress . Parse ( this . Options . Host ) , this . Options . Port ) ;
168
+ ipEndPoint = new IPEndPoint ( parsedIp , this . Options . Port ) ;
170
169
}
171
170
else
172
171
{
173
- ipEndPoint = new IPEndPoint ( ipAddress , this . Options . Port ) ;
172
+ var lookupResult = await LookupHostNameAsync ( this . Options . Host , this . Options . PreferIPv6 ) . ConfigureAwait ( false ) ;
173
+
174
+ if ( lookupResult != null )
175
+ {
176
+ ipEndPoint = new IPEndPoint ( lookupResult , this . Options . Port ) ;
177
+ }
178
+ }
179
+
180
+ if ( ipEndPoint == null )
181
+ {
182
+ throw new HiveMQttClientException ( "Failed to create IPEndPoint. Broker is no valid IP address or hostname." ) ;
174
183
}
175
184
176
185
this . Socket = new ( ipEndPoint . AddressFamily , SocketType . Stream , ProtocolType . Tcp ) ;
You can’t perform that action at this time.
0 commit comments