diff --git a/LiteNetLib/NetClient.cs b/LiteNetLib/NetClient.cs index f9b2668b..6f3ffc7d 100644 --- a/LiteNetLib/NetClient.cs +++ b/LiteNetLib/NetClient.cs @@ -105,7 +105,7 @@ protected override void ProcessError(string errorMessage) public void Connect(string address, int port) { //Create target endpoint - NetEndPoint ep = new NetEndPoint(address, port); + NetEndPoint ep = new NetEndPoint(address, port, AddressType); Connect(ep); } diff --git a/LiteNetLib/NetEndPoint.cs b/LiteNetLib/NetEndPoint.cs index 1f39afd3..dba5e7bf 100644 --- a/LiteNetLib/NetEndPoint.cs +++ b/LiteNetLib/NetEndPoint.cs @@ -55,15 +55,10 @@ public NetEndPoint(string hostStr, int port) IPAddress ipAddress; if (!IPAddress.TryParse(hostStr, out ipAddress)) { - IPHostEntry host = Dns.GetHostEntry(hostStr); - foreach (IPAddress ip in host.AddressList) + ipAddress = ResolveAddress(hostStr, AddressFamily.InterNetworkV6); + if (ipAddress == null) { - if (ip.AddressFamily == AddressFamily.InterNetwork || - ip.AddressFamily == AddressFamily.InterNetworkV6) - { - ipAddress = ip; - break; - } + ipAddress = ResolveAddress(hostStr, AddressFamily.InterNetwork); } } if (ipAddress == null) @@ -73,6 +68,36 @@ public NetEndPoint(string hostStr, int port) EndPoint = new IPEndPoint(ipAddress, port); } + private IPAddress ResolveAddress(string hostStr, AddressFamily addressFamily) + { + IPHostEntry host = Dns.GetHostEntry(hostStr); + foreach (IPAddress ip in host.AddressList) + { + if (ip.AddressFamily == addressFamily) + { + return ip; + } + } + return null; + } + + public NetEndPoint(string hostStr, int port, ConnectionAddressType addressType) + { + IPAddress ipAddress; + if (!IPAddress.TryParse(hostStr, out ipAddress)) + { + ipAddress = ResolveAddress(hostStr, + addressType == ConnectionAddressType.IPv4 + ? AddressFamily.InterNetwork + : AddressFamily.InterNetworkV6); + } + if (ipAddress == null) + { + throw new Exception("Invalid address: " + hostStr); + } + EndPoint = new IPEndPoint(ipAddress, port); + } + internal long GetId() { byte[] addr = EndPoint.Address.GetAddressBytes(); diff --git a/LiteNetLib/NtpSyncModule.cs b/LiteNetLib/NtpSyncModule.cs index 9207b2ff..124f1cef 100644 --- a/LiteNetLib/NtpSyncModule.cs +++ b/LiteNetLib/NtpSyncModule.cs @@ -10,11 +10,11 @@ public class NtpSyncModule public NtpSyncModule(string ntpServer) { - _socket = new NetSocket(ConnectionAddressType.IPv4); - NetEndPoint ourEndPoint = new NetEndPoint(ConnectionAddressType.IPv4, 0); + _ntpEndPoint = new NetEndPoint(ntpServer, 123); + _socket = new NetSocket(_ntpEndPoint.AddressType); + NetEndPoint ourEndPoint = new NetEndPoint(_ntpEndPoint.AddressType, 0); _socket.Bind(ref ourEndPoint); _socket.ReceiveTimeout = 3000; - _ntpEndPoint = new NetEndPoint(ntpServer, 123); SyncedTime = null; } diff --git a/LiteNetLibWin81/NetEndPoint.cs b/LiteNetLibWin81/NetEndPoint.cs index b8405763..302ffdd3 100644 --- a/LiteNetLibWin81/NetEndPoint.cs +++ b/LiteNetLibWin81/NetEndPoint.cs @@ -102,6 +102,11 @@ public NetEndPoint(string hostName, int port) PortStr = port.ToString(); } + public NetEndPoint(string hostName, int port, ConnectionAddressType addressType) : this(hostName, port) + { + + } + internal NetEndPoint(HostName hostName, string port) { HostName = hostName;