Uniy iOS兼容ipv6

Uniy iOS兼容ipv6,第1张

概述苹果的规定:2016年6月1日提交到App Store必须支持IPv6-only网iOS 9.0、OS X 10.11 以上的系统 在IPv6的环境下是支持IP地址访问网络的。所以大家测试机如果是 iOS9.0以上的系统,可以直接通过IP访问。这是因为iOS 9.0之后 NSURLSession和CFNetwork能把IPv4的地址 合成IPv6的地址(在DNS64/NAT64网络环境中)。i...

苹果的规定:2016年6月1日提交到App Store必须支持IPv6-only网
iOS 9.0、OS X 10.11 以上的系统 在IPv6的环境下是支持IP地址访问网络的。所以大家测试机如果是 iOS9.0以上的系统,可以直接通过IP访问。这是因为iOS 9.0之后 NSURLSession和CFNetwork能把IPv4的地址 合成IPv6的地址(在DNS64/NAT64网络环境中)。
iOS 9.0以下的系统 就会报错

objective-c接口

#include <sys/socket.h>#include <netdb.h>#include <arpa/inet.h>#include <err.h>#include <string.h>extern "C" const char* AppMiscGetIPv6(const char *mHost,const char *mPrt){	if( nil == mHost )		return NulL;	const char *newChar = "No";	const char *cause = NulL;	struct addrinfo* res0;	struct addrinfo hints;	struct addrinfo* res;	int n, s;		memset(&hints, 0, sizeof(hints));		hints.ai_flags = AI_DEFAulT;	hints.ai_family = PF_Unspec;	hints.ai_socktype = SOCK_STREAM;		if((n=getaddrinfo(mHost, "http", &hints, &res0))!=0)	{		printf("getaddrinfo error: %s\n",gai_strerror(n));		return NulL;	}    		struct sockaddr_in6* addr6;	struct sockaddr_in* addr;	char ipbuf[128];    bool isipv6 = false;	s = -1;	for(res = res0; res; res = res->ai_next)	{		if (res->ai_family == AF_INET6)		{			addr6 =( struct sockaddr_in6*)res->ai_addr;			newChar = inet_ntop(AF_INET6, &addr6->sin6_addr, ipbuf, sizeof(ipbuf));            isipv6 = true;		}		else		{			addr =( struct sockaddr_in*)res->ai_addr;			newChar = inet_ntop(AF_INET, &addr->sin_addr, sizeof(ipbuf));            isipv6 = false;        }		break;	}		freeaddrinfo(res0);        int len = strlen(newChar);    char* sbuff = (char*)malloc(len +3+1);    sbuff[0] = 'v';    sbuff[1] = isipv6 ? '6':'4';    sbuff[2] = ';';    int i = 0;    len++;    for(i = 0; i < len; i++)    {        sbuff[i+3] = newChar[i];    }	return sbuff;}

c#接口

using UnityEngine;using System;using System.Collections;using System.Runtime.InteropServices;using System.Net.Sockets;public static class AppMiscIOS{	public static voID IPv6GetIPType(string serverIp, string serverPorts, out string outServerIp, out AddressFamily outIPType)	{		outIPType = AddressFamily.InterNetwork;		outServerIp = serverIp;		try		{			string newAddr = GetIPv6(serverIp, serverPorts);			if (!string.IsNullOrEmpty(newAddr) && newAddr.Length > 4)			{				if (newAddr[0] == 'v' && newAddr[2] == ';')				{					if (newAddr[1] =='6')					{						outServerIp = newAddr.Substring(3);						outIPType = AddressFamily.InterNetworkV6;					}else if (newAddr[1] == '4'){						outServerIp = newAddr.Substring(3);					}				}			}		}		catch (Exception e)		{			DeBUG.LogError("IPv6_GetIPType error:" + e);		}	}	[Dllimport("__Internal")]	private static extern string AppMiscGetIPv6(string host, string port);	private static string GetIPv6(string host, string port)	{		#if UNITY_IOS && !UNITY_EDITOR			return  AppMiscGetIPv6(host, port);		#else			return "v4;"+host;		#endif	}}

使用例子(注意下面例子只是示范,具体有些变量的声明和方法没有写明,仅供参考)

public bool Connect(string strHost, int port){	try	{		CloseSocket();	#if UNITY_IOS		var newHost = strHost;		var address_family = AddressFamily.InterNetwork;		// 调用接口兼容ipv6		AppMiscIOS.IPv6GetIPType(strHost, port.ToString(), out newHost, out address_family);		m_socket = new Socket(address_family, SocketType.Stream, ProtocolType.Tcp);		m_socket.SendTimeout = 500;		m_iAsyncResult = m_socket.BeginConnect(newHost, port, null, m_socket);		DeBUG.Log("connect: " + newHost + " port: " + port + " handle: " + m_socket.Handle + " ipv"+(address_family == AddressFamily.InterNetworkV6 ? "6" : "4"));	#else		m_socket = new Socket(AddressFamily.InterNetwork, ProtocolType.Tcp);		m_socket.SendTimeout = 500;		m_iAsyncResult = m_socket.BeginConnect(strHost, m_socket);		DeBUG.Log("connect: " + strHost + " port: " + port + " handle: " + m_socket.Handle);	#endif		AddNetState(NetState.ConnectStart, null);	}	catch (Exception exception)	{		CloseSocket();		m_bConnect = false;		DeBUG.LogError("Connect Exception:" + exception.ToString());		AddNetState(NetState.ConnectFail, null);	}	return m_bConnect;}

相关链接
《IOS解决ipv6问题》:https://www.jianshu.com/p/10ef0d568251

总结

以上是内存溢出为你收集整理的Uniy iOS兼容ipv6全部内容,希望文章能够帮你解决Uniy iOS兼容ipv6所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/998853.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存