#if UNITY_IPHONE && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern string getIPv6(string mHost, string mPort);
#endif
//"192.168.1.1&&ipv4"
public static string GetIPv6(string mHost, string mPort)
{
#if UNITY_IPHONE && !UNITY_EDITOR
string mIPv6 = getIPv6(mHost, mPort);
return mIPv6;
#else
return mHost + "&&ipv4";
#endif
}
void getIPType(String serverIp, String serverPorts, out String newServerIp, out AddressFamily mIPType)
{
mIPType = AddressFamily.InterNetwork;
newServerIp = serverIp;
try
{
string mIPv6 = GetIPv6(serverIp, serverPorts);
if (!string.IsNullOrEmpty(mIPv6))
{
string[] m_StrTemp = System.Text.RegularExpressions.Regex.Split(mIPv6, "&&");
if (m_StrTemp != null && m_StrTemp.Length >= 2)
{
string IPType = m_StrTemp[1];
if (IPType == "ipv6")
{
newServerIp = m_StrTemp[0];
mIPType = AddressFamily.InterNetworkV6;
}
}
}
}
catch (Exception e)
{
Dg.LogError("GetIPv6 error:" + e.ToString());
}
}
public TcpClient CreateTcpClient(String serverIp, String serverPorts,out String newServerIP)
{
String newServerIp = "";
AddressFamily newAddressFamily = AddressFamily.InterNetwork;
#if UNITY_IPHONE && !UNITY_EDITOR
getIPType(serverIp, serverPorts, out newServerIp, out newAddressFamily);
if (!string.IsNullOrEmpty(newServerIp)) { serverIp = newServerIp; }
#endif
TcpClient client = new TcpClient(newAddressFamily);
Dg.Log("网络类型 AddressFamily :" + newAddressFamily.ToString() + " ServerIp:" + serverIp);
newServerIP = serverIp;
return client;
}
private TcpClient client;
//ip :ipv4地址,
public void Connect(string ip, int port,int connectTimeout)
{
string newIp=ip;
client = ProviderService.Instance.CreateTcpClient(ip, port.ToString(),out newIp);
client.BeginConnect(newIp, port, ConnectCallBack, client);
}
private void ConnectCallBack(IAsyncResult ar)
{
Dg.Log("ConnectCallBack");
try
{
if (client == null) return;
client.EndConnect(ar);
Dg.Log("ConnectCallBack1111");
//连接成功了
}
catch (SocketException e)
{
Dg.LogError("SOCKET::ERROR:", e.ToString());
}
}