JumboWu 主页

版权声明:本文为Jumbo原创文章,采用[知识共享 署名-非商业性使用-禁止演绎 4.0 国际 许可协议],转载前请保证理解此协议 原文出处:http://www.jianshu.com/p/c1344765805c

Unity4-7-2-f1-支持iOS-IPv6-(低成本,未出现审核问题)

用Mac搭建NAT64模拟真实环境就不多介绍了,切记文件分享,VNC等服务关掉,重启,网上有很多教程,文章末尾可参考

!!!注意!!! 1、如果你用以太网共享网络数据、iOS终端上不了的话,请换用iphone 移动数据usb共享给Mac电脑测试,之前在这被坑了一把,切记!!! 2、Unity版本4.7.2f1以上 3、所有地方使用域名、使用域名、使用域名,不要用点分十进制IPv4地址或者IPv6十六进制地址,全部使用域名 4、服务器不用做处理,继续他的,上面的域名需要解析到对应的IP

附上代码(放在Assets\Plugins\iOS目录下): iOSHelper.h

@interface iOSHelper : NSObject

+(NSString *)_GetIPAddress : (const char *)host :(const char *)port;

@end

iOSHelper.mm

#import "iOSHelper.h"
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>

char* cStringCopy(const char* str)
{
    if (str == NULL)
        return NULL;
    char *res = (char*)malloc(strlen(str)+1);
    strcpy(res, str);
    NSLog(@"PlatformHelper. GetIPAdderess : res = %s", res);
    return res;
}

const char* _GetIPAddress(const char *host,const char *port)
{
	if( NULL == host || NULL == port)
		return NULL;

	char ipbuf[256];
	memset(ipbuf, 0, sizeof(ipbuf));
	strcpy(ipbuf, host);
	
	struct addrinfo hints;
	memset(&hints, 0, sizeof(hints));
	struct addrinfo* nextAddr = NULL;
	struct addrinfo* resultAddr = NULL;
	
	
	hints.ai_flags = AI_DEFAULT;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	
    struct sockaddr_in6* addr6;
	struct sockaddr_in* addr;
	

	bool haveIpv4Addr = false;
	
    int err = getaddrinfo(host, port, &hints, &resultAddr);
	if(err != 0)
	{
		printf("getaddrinfo error: %s\n",gai_strerror(err));
		return cStringCopy(ipbuf);
	}
	
    //Test Connect timeout, local network
    struct timeval tv;
    int optval;
    int flags = 0;
    fd_set rfds;
    fd_set wfds;
    socklen_t optlen;
    int timeout = 3;
    
    int result;
    const char *cause = NULL;
    int sock = -1;
	for(nextAddr = resultAddr; nextAddr != NULL; nextAddr = nextAddr->ai_next)
	{
        sock = socket(nextAddr->ai_family, nextAddr->ai_socktype, nextAddr->ai_protocol);
        if(sock < 0)
        {
            cause = "socket";
            continue;
        }
        
        flags = fcntl(sock, F_GETFL, 0);
        fcntl(sock, F_SETFL, flags | O_NONBLOCK);
        
        result = connect(sock, nextAddr->ai_addr, nextAddr->ai_addrlen);
        
        if (result == 0)
        {
            result = 1;
            printf("connect success");
        }
        else if (result < 0 && errno != EINPROGRESS)
        {
            result = -1;
            cause = "connect";
            close(sock);
            sock = -1;
            continue;
        }
        else
        {
            do{
                tv.tv_sec = timeout;
                tv.tv_usec = 0;
                
                FD_ZERO(&wfds);
                FD_SET(sock, &wfds);
                rfds = wfds;
                
                result = select(sock + 1, &rfds, &wfds, NULL, &tv);
                
                if (result == 0)
                {
                    result = -1;
                    break;
                }
                
                if (result < 0 && errno != EINTR)
                {
                    result = -1;
                    break;
                }
                else if (result > 0)
                {
                    optlen = sizeof(int);
                    if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)(&optval), &optlen) < 0)
                    {
                        result = -1;
                        break;
                    }
                    
                    if (optval != 0)
                    {
                        result = -1;
                        break;
                    }
                    
                    result = 1;
                    break;
                    
                }
                else{
                    result = -1;
                    break;
                }
            }
            while (1);
            
            if (0 >= result)
             {
                 result = -1;
                 cause = "connect";
                 close(sock);
                 sock = -1;
                 continue;
             }
             else{
                 printf(" real connect success");
             }
        }
        
        /*
        if(connect(sock, nextAddr->ai_addr, nextAddr->ai_addrlen) < 0)
        {
            cause = "connect";
            close(sock);
            sock = -1;
            continue;
        }
        */

        if (result > 0)
        {
            //网络可达
            cause = "connected";
            close(sock);
            sock = -1;
        }
        
        //inet_ntop(nextAddr->ai_family, nextAddr->ai_addr, ipbuf, sizeof(ipbuf));
        switch(nextAddr->ai_family)
        {
            case AF_UNSPEC:
                break;
            case AF_INET:
            {
                addr =( struct sockaddr_in*)nextAddr->ai_addr;
                inet_ntop(AF_INET, &addr->sin_addr, ipbuf, sizeof(ipbuf));
            }
                break;
            case AF_INET6:
            {
                addr6 =( struct sockaddr_in6*)nextAddr->ai_addr;
                inet_ntop(AF_INET6, &addr6->sin6_addr, ipbuf, sizeof(ipbuf));
            }
                break;
            default:
                break;
        }
        
        break;//get addr
        
        /*
	    switch(nextAddr->ai_family)
	    {
	       case AF_UNSPEC:
	            break;
	       case AF_INET:
	            {
	                 addr =( struct sockaddr_in*)nextAddr->ai_addr;
			         inet_ntop(AF_INET, &addr->sin_addr, ipbuf, sizeof(ipbuf));
			         haveIpv4Addr = true;
	            }
	            break;
	       case AF_INET6:
	            {
	                 addr6 =( struct sockaddr_in6*)nextAddr->ai_addr;
			         inet_ntop(AF_INET6, &addr6->sin6_addr, ipbuf, sizeof(ipbuf));
	            }
	            break;
	       default:
	            break;
	    }
	    
	    if (haveIpv4Addr)
	        break;
         */
        
		
	}
	
	
	freeaddrinfo(resultAddr);
	
	printf("getaddrinfo OK");
	
	return cStringCopy(ipbuf);
}

Unity中CS代码: PlatformHelper.cs

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using Assets.Scripts.Common;

public class PlatformHelper : Singleton<PlatformHelper>
{

#if UNITY_IPHONE && !UNITY_EDITOR
        [DllImport("__Internal")]
        private static extern string _GetIPAddress(string host, string port);
#endif


	public  string GetIPAddress(string host, string port)
    {
#if UNITY_IPHONE && !UNITY_EDITOR
		return _GetIPAddress(host, port);
#else
        return host;
#endif
    }
}

顺便Singleton.cs单例模式也贴上:

public class Singleton<T> where T : class, new()
    {
        // 单件子类实例
        private static T _instance;

        protected Singleton()
        {
        }

        /// <summary>
        ///     获得类型的单件实例
        /// </summary>
        /// <returns>类型实例</returns>
        public static T Instance()
        {
            if (null == _instance)
            {
                _instance = new T();
            }

            return _instance;
        }

        /// <summary>
        /// 删除单件实例
        /// </summary>
        public static void DestroyInstance()
        {
            _instance = null;
        }
    }

socket连接的地方参考:

    /// <summary>
    /// 连接服务器,支持IPv4和IPv6
    /// </summary>
    /// <param name="server">服务器域名</param>
    /// <param name="port">端口号</param>
    public void ConnectSocket(string server, int port)
    {
        try
        {
            string newServerIP = server;
            newServerIP = PlatformHelper.Instance().GetIPAddress(server, port.ToString());
            IPAddress IPAddr = null;
            
            if (!IPAddress.TryParse(newServerIP, out IPAddr))
            {
                //地址获取有问题,连接失败
                return;
            }
            
            mSocket = new Socket(IPAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); ;
            mSendHead = mSendTail = 0;
            mRecvHead = mRecvTail = 0;
            mIsSending = mIsRecving = false;
            mState = SocketState.StateConnecting;
            ClearSocketErrorCode();

            mSocket.BeginConnect(IPAddr, port, new AsyncCallback(ConnectComplete), null);

        }
        catch (System.Net.Sockets.SocketException ex)
        {
            SetErrorState(ex.SocketErrorCode);
        }
    }

参考如下: 1.http://www.codezeg.com/2016/06/14/ipv6/#section-3 2.http://www.codeinsect.net/blog/2016/05/26/unity-ipv6-socket-%E6%94%AF%E6%8C%81%EF%BC%8C%E5%B7%B2%E6%B5%8B%E8%AF%95%E9%80%9A%E8%BF%87/ 3.http://blog.csdn.net/zhuweisky/article/details/8209040 4.https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html#//apple_ref/doc/uid/TP40010220-CH213-SW1 5.http://www.cnblogs.com/HemJohn/p/5590483.html

Fork me on GitHub