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

实现IPV6连接的支持 #39

Open
wants to merge 4 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
31 changes: 24 additions & 7 deletions pomelo-dotnetClient/src/client/PomeloClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,49 @@ 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)
{
NetWorkChanged(NetWorkState.ERROR);
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) =>
{
Expand Down Expand Up @@ -216,8 +233,8 @@ internal void processMessage(Message msg)

public void disconnect()
{
Dispose();
NetWorkChanged(NetWorkState.DISCONNECTED);
Dispose();
}

public void Dispose()
Expand Down