void TryConnect(string szIP, int nPort, bool bDns) { try { if (!bDns && m_pGetHostAddressInterface == null) { bDns = true; } IPAddress[] ipa = bDns ? Dns.GetHostAddresses(szIP) : m_pGetHostAddressInterface(szIP); if (ipa != null) { if (!m_bInitIPV6) { m_bInitIPV6 = true; m_bIsIPV6 = CTcp.IsIPV6Net(); } IPAddress pIPAddr = null; for (int i = 0; i < ipa.Length; ++i) { pIPAddr = ipa[i]; if (pIPAddr.AddressFamily == AddressFamily.InterNetworkV6) { if (!m_bIsIPV6) { continue; } } else { if (m_bIsIPV6) { continue; } } IPEndPoint ipDns = new IPEndPoint(pIPAddr, nPort); m_hSocket = new Socket(pIPAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); m_hSocket.BeginConnect(ipDns, new AsyncCallback(ConnectCallback), m_hSocket); m_szConnectAddr = pIPAddr.ToString(); break; } if (m_hSocket == null && ipa != null) { int nLen = ipa.Length; int nIndex = CTcp.RandInt(ref m_nRandSeed, 0, nLen - 1); // 随机一个吧 m_hSocket = new Socket(ipa[nIndex].AddressFamily, SocketType.Stream, ProtocolType.Tcp); m_hSocket.BeginConnect(ipa, nPort, new AsyncCallback(ConnectCallback), m_hSocket); } } } catch (Exception) { m_hSocket = null; if (bDns) { ++m_DnsExceptionCount; } } }
void ThreadRun() { if (m_tcp != null) { m_tcp.Close(); } else { m_tcp = new CTcp(); } m_tcp.ConnectToServer(m_szConnectAddr, m_nConnectPort, m_nRandSeed); if (m_tcp.IsConnect()) { OnSucConnect(); } else { OnFailedConect(); } // 开始接收消息吧 BeginRecive(); }
private AssetDownloadCentral m_backgroundCentral; //后台下载 // 启动时测试一下我本地的IPV6环境, 请将这个域名修改你项目的CDN地址 public void StartTestIPV6(string szCDNUrl) { CTcp.StartTestIPV6(szCDNUrl); }