c# - 在 HoloLens 1 上,在使用默认构造函数 "ArgumentException: Value does not fall within the expected range"创建 TcpClient 对象时抛出

标签 c# sockets unity3d hololens il2cpp

直到大约 2 天前,我有一个应用程序运行良好,其中 HoloLens 1 通过 TCP 连接到本地网络上的另一台 PC。

最近,每次调用 TcpClient() 默认构造函数都会抛出上述错误。没有值被传递给它(因为它是默认值),因此我不知道它错误的值是什么。

我尝试了以下解决方案,有人在 HoloLens slack 上链接给我:https://github.com/Azure/azure-remote-rendering/issues/6

该解决方案需要更新到 Unity 2019.3.15f。我尝试了 Visual Studio、Windows SDK 和 Unity 的多种不同配置。

我发现其他多个帖子似乎表明罪魁祸首是 il2cpp。这是相关输出的副本。

UnloadTime: 7.610200 ms
Exception thrown at 0x771E3AE2 in TCPTest.exe: Microsoft C++ exception: Il2CppExceptionWrapper at memory location 0x025CE7EC.
Exception thrown at 0x771E3AE2 in TCPTest.exe: Microsoft C++ exception: Il2CppExceptionWrapper at memory location 0x025CE90C.
ArgumentException: Value does not fall within the expected range.
  at System.Net.Sockets.Socket.SetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, System.Int32 optionValue) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Net.Sockets.Socket.set_DontFragment (System.Boolean value) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Net.Sockets.Socket.SocketDefaults () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Net.Sockets.Socket..ctor (System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Net.Sockets.TcpClient.initialize () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Net.Sockets.TcpClient..ctor (System.Net.Sockets.AddressFamily family) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Net.Sockets.TcpClient..ctor () [0x00000] in <00000000000000000000000000000000>:0 
  at TcpTestClient.Start () [0x00000] in <00000000000000000000000000000000>:0 

(Filename: currently not available on il2cpp Line: -1)

到目前为止,我遇到的这种错误发生次数最少的情况如下:

新的Unity项目(哪个版本似乎无关紧要)

在场景中添加一个GameObject,附上:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.Sockets;

public class TcpTestClient : MonoBehaviour
{
    private TcpClient tcpClient;
    // Start is called before the first frame update
    void Start()
    {
        tcpClient = new TcpClient();
        if (tcpClient != null)
        {
            Debug.Log("Constructor succeeded!");
        }
    }
}

按照为 HoloLens 1 指定的方式配置 Unity build设置,确保在发布设置 > 功能中包括 InternetClient、InternetClientServer、PrivateNetworkClientServer。

在安装了适当构建工具的 Visual Studio 2017 或 2019 中构建、打开生成的解决方案。部署到 HoloLens,结果错误将输出到调试控制台。

感谢阅读,我非常感谢任何人的见解。

最佳答案

看起来这是 Unity 中的一个错误。看到这个:https://forum.unity.com/threads/il2cpp-failing-in-windows-machine.891436/#post-5944052

解决方法涉及更改 il2cpp 源代码:

diff --git a/libil2cpp/os/Win32/SocketImpl.cpp b/libil2cpp/os/Win32/SocketImpl.cpp
index f63abecd5..5821d2577 100644
--- a/libil2cpp/os/Win32/SocketImpl.cpp
+++ b/libil2cpp/os/Win32/SocketImpl.cpp
@@ -1700,11 +1700,7 @@ namespace os
                 break;

             case kSocketOptionLevelIP:
-#ifdef SOL_IP
-                *system_level = SOL_IP;
-#else
                 *system_level = IPPROTO_IP;
-#endif

                 switch (name)
                 {
@@ -1777,11 +1773,7 @@ namespace os
                 break;
 #if IL2CPP_SUPPORT_IPV6
             case kSocketOptionLevelIPv6:
-        #ifdef SOL_IPV6
-                *system_level = SOL_IPV6;
-        #else
                 *system_level = IPPROTO_IPV6;
-        #endif

                 switch (name)
                 {

关于c# - 在 HoloLens 1 上,在使用默认构造函数 "ArgumentException: Value does not fall within the expected range"创建 TcpClient 对象时抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62314810/

相关文章:

python - 发送的scapy包收不到

c# - 如何知道tcp服务器是否处于监听状态

c# - 使用 Web 服务和本地数据库 MVC View 的一对多关系

c# - 哪种代码模式最适合处理周期性和可变的 Action 流

c# - 套接字问题,客户端声称连接失败,但服务器说连接成功

c# - 如果场景中的多个游戏对象使用该脚本,脚本中声明的公共(public)委托(delegate)是否一定是单个实例?

audio - Unity3D从粒子播放声音

unity3d - 适用于 Unity3D 5.3.5 的 Google VR SDK 中的标准 "Image Effects"问题

c# - 将 Object.GetHashCode() 转换为 Guid

c# - 如何在反序列化的 MongoDB 文档中获取对父对象的引用?