苹果过审ipv6问题

  苹果过审有一项是必须通过ipv6网络测试游戏,一开始的想法是实现两套socket链接,服务器也实现ipv6,后来百度了其实很简单,直接Dns.GetHostAddresses(string hostNameOrAddress)获取链接地址,hostNameOrAddress传域名地址,域名绑定服务器ip就好了,下面是完整代码: 

 1     /// <summary>
 2     /// 连接服务器
 3     /// </summary>
 4     void ConnectServer(string host, int port) {
 5         TcpClient client = null;
 6 
 7         //获取远端服务器ip地址
 8         IPAddress[] address = Dns.GetHostAddresses(host);
 9         if (address.Length == 0)
10         {
11             Debug.LogError("address.Length == 0");
12             return;
13         }
14 
15         //判断当前网络是否为ipv6建立不同链接
16         if (address[0].AddressFamily == AddressFamily.InterNetworkV6)
17         {
18             client = new TcpClient(AddressFamily.InterNetworkV6);
19         }
20         else
21         {
22             client = new TcpClient(AddressFamily.InterNetwork);
23         }
24 
25         //建立链接
26         client.NoDelay = true;
27         try {
28             client.BeginConnect(address[0], port, new AsyncCallback(OnConnect), null);
29         } catch (Exception e) {
30             Close(); Debug.LogError(e.Message);
31         }
32     }
View Code

 

  

 

posted @ 2017-10-29 17:14  青树  阅读(264)  评论(0编辑  收藏  举报