From c620d1a5737b61d74bb9d96c781dfbe0af2534e6 Mon Sep 17 00:00:00 2001 From: Gong Lei Date: Sat, 9 Nov 2013 13:28:37 +0800 Subject: [PATCH 1/3] Make PomeloClient class support parsing DNS. --- pomelo-dotnetClient/src/client/PomeloClient.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pomelo-dotnetClient/src/client/PomeloClient.cs b/pomelo-dotnetClient/src/client/PomeloClient.cs index 50f2826..58a6b9c 100644 --- a/pomelo-dotnetClient/src/client/PomeloClient.cs +++ b/pomelo-dotnetClient/src/client/PomeloClient.cs @@ -27,8 +27,17 @@ public PomeloClient(string host, int port) { } private void initClient(string host, int port) { + IPAddress ipAddress; + if (!IPAddress.TryParse (host, out ipAddress)){ + ipAddress = Dns.GetHostEntry (host).AddressList[0]; + } + else{ + Console.WriteLine(String.Format("ERROR: cannot parse host!->{0}", host)); + throw new Exception("Parse host error!"); + } + this.socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); - IPEndPoint ie=new IPEndPoint(IPAddress.Parse(host), port); + IPEndPoint ie=new IPEndPoint(ipAddress, port); try { this.socket.Connect(ie); } From 51ad0915f4a63d8ad2de955556ea0be638a5802a Mon Sep 17 00:00:00 2001 From: Gong Lei Date: Mon, 11 Nov 2013 18:07:30 +0800 Subject: [PATCH 2/3] Fix IP parse error. --- pomelo-dotnetClient/src/client/PomeloClient.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pomelo-dotnetClient/src/client/PomeloClient.cs b/pomelo-dotnetClient/src/client/PomeloClient.cs index 58a6b9c..339c761 100644 --- a/pomelo-dotnetClient/src/client/PomeloClient.cs +++ b/pomelo-dotnetClient/src/client/PomeloClient.cs @@ -31,10 +31,6 @@ private void initClient(string host, int port) { if (!IPAddress.TryParse (host, out ipAddress)){ ipAddress = Dns.GetHostEntry (host).AddressList[0]; } - else{ - Console.WriteLine(String.Format("ERROR: cannot parse host!->{0}", host)); - throw new Exception("Parse host error!"); - } this.socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPEndPoint ie=new IPEndPoint(ipAddress, port); From 62a13e080f0a125a370dc1f1e0340abfa195635a Mon Sep 17 00:00:00 2001 From: Gong Lei Date: Wed, 19 Sep 2018 15:54:40 +0800 Subject: [PATCH 3/3] =?UTF-8?q?1.=20=E5=AE=9E=E7=8E=B0IPV6=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E7=9A=84=E6=94=AF=E6=8C=81=E3=80=82=202.=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=96=AD=E5=BC=80=E8=BF=9E=E6=8E=A5=E7=9A=84=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E8=A7=A6=E5=8F=91=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/client/PomeloClient.cs | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/pomelo-dotnetClient/src/client/PomeloClient.cs b/pomelo-dotnetClient/src/client/PomeloClient.cs index 3b10495..23dabf5 100644 --- a/pomelo-dotnetClient/src/client/PomeloClient.cs +++ b/pomelo-dotnetClient/src/client/PomeloClient.cs @@ -67,18 +67,28 @@ public void initClient(string host, int port, Action callback = null) NetWorkChanged(NetWorkState.CONNECTING); IPAddress ipAddress = null; - + IPAddress ipAddressV6 = null; try { IPAddress[] addresses = Dns.GetHostEntry(host).AddressList; foreach (var item in addresses) { - if (item.AddressFamily == AddressFamily.InterNetwork) + if (item.AddressFamily == AddressFamily.InterNetworkV6) { - ipAddress = item; + ipAddressV6 = item; break; } } + if(ipAddressV6 == null){ + foreach (var item in addresses) + { + if (item.AddressFamily == AddressFamily.InterNetwork) + { + ipAddress = item; + break; + } + } + } } catch (Exception e) { @@ -86,13 +96,20 @@ public void initClient(string host, int port, Action callback = null) return; } - if (ipAddress == null) + if (ipAddressV6 == null && ipAddress == null) { throw new Exception("can not parse host : " + host); } - this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - IPEndPoint ie = new IPEndPoint(ipAddress, port); + IPEndPoint ie = null; + if(ipAddressV6 != null){ + this.socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); + ie = new IPEndPoint(ipAddressV6, port); + } + else{ + this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + ie = new IPEndPoint(ipAddress, port); + } socket.BeginConnect(ie, new AsyncCallback((result) => { @@ -216,8 +233,8 @@ internal void processMessage(Message msg) public void disconnect() { - Dispose(); NetWorkChanged(NetWorkState.DISCONNECTED); + Dispose(); } public void Dispose()