@@ -182,10 +182,10 @@ public void Connect()
182
182
{
183
183
ResetHost ( Host ) ;
184
184
185
- IPEndPoint endpoint = IPAddress . TryParse ( _ip , out var tryip ) ?
186
- new IPEndPoint ( tryip , _port ) :
187
- new IPEndPoint ( Dns . GetHostAddresses ( _ip ) . FirstOrDefault ( ) ?? IPAddress . Parse ( _ip ) , _port ) ;
188
- var localSocket = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
185
+ EndPoint endpoint = ParseEndPoint ( _ip , _port ) ;
186
+ var localSocket = endpoint . AddressFamily == AddressFamily . InterNetworkV6 ?
187
+ new Socket ( AddressFamily . InterNetworkV6 , SocketType . Stream , ProtocolType . Tcp ) :
188
+ new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
189
189
190
190
var asyncResult = localSocket . BeginConnect ( endpoint , null , null ) ;
191
191
if ( ! asyncResult . AsyncWaitHandle . WaitOne ( ConnectTimeout , true ) )
@@ -268,5 +268,20 @@ public static KeyValuePair<string, int> SplitHost(string host)
268
268
269
269
return new KeyValuePair < string , int > ( host , 6379 ) ;
270
270
}
271
+
272
+ private static EndPoint ParseEndPoint ( string ip , int port )
273
+ {
274
+ if ( IPAddress . TryParse ( ip , out var tryip ) )
275
+ {
276
+ return new IPEndPoint ( tryip , port ) ;
277
+ }
278
+
279
+ if ( Dns . GetHostAddresses ( ip ) . Length == 0 )
280
+ {
281
+ throw new Exception ( $ "无法解析“{ ip } ”") ;
282
+ }
283
+
284
+ return new DnsEndPoint ( ip , port ) ;
285
+ }
271
286
}
272
287
}
0 commit comments