Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client connect support directly use ipaddress #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/TcpSharp/TcpSharpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,17 @@ 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))
{
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.
Expand Down