Skip to content

Commit c182d4c

Browse files
committed
Merge branch 'master' of https://github.com/2881099/FreeRedis
2 parents a828299 + 0ab93a6 commit c182d4c

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/FreeRedis/Internal/DefaultRedisSocket.cs

+19-4
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ public void Connect()
182182
{
183183
ResetHost(Host);
184184

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);
189189

190190
var asyncResult = localSocket.BeginConnect(endpoint, null, null);
191191
if (!asyncResult.AsyncWaitHandle.WaitOne(ConnectTimeout, true))
@@ -268,5 +268,20 @@ public static KeyValuePair<string, int> SplitHost(string host)
268268

269269
return new KeyValuePair<string, int>(host, 6379);
270270
}
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+
}
271286
}
272287
}

0 commit comments

Comments
 (0)