学堂 学堂 学堂公众号手机端

Unity ipv6的兼容适配(iOS) Scoket

lewis 4年前 (2021-11-01) 阅读数 7 #技术


欢迎加入Unity业内qq交流群:956187480




做了一个产品,在ios审核上架的时候biej被拒了如下:

Guideline 2.1 - Performance - App Completeness

We discovered one or more bugs in your app when reviewed on iPhone running iOS 11.4.1 on Wi-Fi connected to an IPv6 network.
Specifically, when we tapped on the log in button, no further action took place.

意思是说应用在ipv6的环境下不能登录。

然后我们就要找解决方案了:

之前的scoket连接建立,AddressFamily.InterNetwork是ipv4的网络

/// <param name="port">端口号</param>
public void CreateConnection(string serverAddress, int port)
{
if (scoket != null && scoket.Connected)
{
return;
}
try
{
this.scoket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.scoket.NoDelay = true;
IPEndPoint end_point = new IPEndPoint(IPAddress.Parse(serverAddress), port);
this.scoket.BeginConnect(end_point, new AsyncCallback(this.Connected), this.scoket);
}
catch (Exception e)
{
Debuger.Log( e.Message);
}

}

我们需要检测当前网络环境类型并做兼容处理如下

/// <param name="port">端口号</param>
public void CreateConnection(string serverAddress, int port)
{
if (scoket != null && scoket.Connected)
{
return;
}
try
{
IPAddress[] address = Dns.GetHostAddresses(serverAddress);
if (address[0].AddressFamily == AddressFamily.InterNetworkV6)
{
Debug.Log("Connect InterNetworkV6");
scoket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
}
else
{
Debug.Log("Connect InterNetwork");
scoket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
this.scoket.NoDelay = true;
IPEndPoint end_point = new IPEndPoint(IPAddress.Parse(serverAddress), port);
this.scoket.BeginConnect(end_point, new AsyncCallback(this.Connected), this.scoket);
}
catch (Exception e)
{
Debuger.Log( e.Message);
}

}

IPAddress[] address = Dns.GetHostAddresses(serverAddress);我们可以知道当前网络环境类型,然后创建对应连接的scoket

以上对ipv6的网络适配有一个前提就是 服务器也必须能支持ipv6的网络传输不然一切都是空谈

欢迎加入Unity业内qq交流群:956187480


版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门