首页 > 解决方案 > 在 HoloLens 1 上,使用默认构造函数创建 TcpClient 对象时会抛出“ArgumentException:值不在预期范围内”

问题描述

我有一个应用程序运行良好,直到大约 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 构建设置,确保在发布设置 > 功能中包括 InternetClient、InternetClientServer、PrivateNetworkClientServer。

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

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

标签: c#socketsunity3dhololensil2cpp

解决方案


看起来这是 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)
                 {

推荐阅读