From 0991883f5f9290cdfa731c9e81b65d874d98daa2 Mon Sep 17 00:00:00 2001 From: startewho Date: Tue, 28 May 2024 11:31:45 +0800 Subject: [PATCH 1/2] support directly use ip --- src/TcpSharp/TcpSharpSocketClient.cs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/TcpSharp/TcpSharpSocketClient.cs b/src/TcpSharp/TcpSharpSocketClient.cs index 422635d..2910d59 100644 --- a/src/TcpSharp/TcpSharpSocketClient.cs +++ b/src/TcpSharp/TcpSharpSocketClient.cs @@ -218,11 +218,21 @@ public void Connect() // Get Host IP Address that is used to establish a connection // In this case, we get one IP address of localhost that is IP : 127.0.0.1 - // If a host has multiple addresses, you will get a list of addresses - var serverIPHost = Dns.GetHostEntry(Host); - if (serverIPHost.AddressList.Length == 0) throw new Exception("Unable to solve host address"); - var serverIPAddress = serverIPHost.AddressList[0]; - if (serverIPAddress.ToString() == "::1") serverIPAddress = new IPAddress(16777343); // 127.0.0.1 + // If a host has multiple addresses, you will get a list of addresses + // + + IPAddress serverIPAddress; + if (IPAddress.TryParse(Host, out serverIPAddress)) + { + + } + else + { + var serverIPHost = Dns.GetHostEntry(Host); + if (serverIPHost.AddressList.Length == 0) throw new Exception("Unable to solve host address"); + serverIPAddress = serverIPHost.AddressList[0]; + if (serverIPAddress.ToString() == "::1") serverIPAddress = new IPAddress(16777343); // 127.0.0.1 + } var serverIPEndPoint = new IPEndPoint(serverIPAddress, Port); // Create a TCP/IP socket. From fa51bc5822053c4a054d6bab4beee67a275b8924 Mon Sep 17 00:00:00 2001 From: startewho Date: Tue, 28 May 2024 13:40:30 +0800 Subject: [PATCH 2/2] Update TcpSharpSocketClient.cs --- src/TcpSharp/TcpSharpSocketClient.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/TcpSharp/TcpSharpSocketClient.cs b/src/TcpSharp/TcpSharpSocketClient.cs index 2910d59..97a5186 100644 --- a/src/TcpSharp/TcpSharpSocketClient.cs +++ b/src/TcpSharp/TcpSharpSocketClient.cs @@ -222,11 +222,7 @@ public void Connect() // IPAddress serverIPAddress; - if (IPAddress.TryParse(Host, out serverIPAddress)) - { - - } - else + if (!IPAddress.TryParse(Host, out serverIPAddress)) { var serverIPHost = Dns.GetHostEntry(Host); if (serverIPHost.AddressList.Length == 0) throw new Exception("Unable to solve host address");