问题
I had an application working fine until about 2 days ago where a HoloLens 1 connected via TCP to another PC on a local network.
Recently, every call to TcpClient() default constructor throws the error above. No values are being passed into it (since it is default), therefore I have no idea what value it is erroring on.
I have tried the following solution, linked to me by someone on the HoloLens slack: https://github.com/Azure/azure-remote-rendering/issues/6
That solution entails updating to Unity 2019.3.15f. I've tried multiple different configurations of Visual Studio, Windows SDK, and Unity.
I have found multiple other posts which seem to indicate that the culprit is il2cpp. Here is a copy of the relevant output.
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)
The most minimal occurrence of this error I have so far produced is as follows:
New Unity project (doesn't seem to matter which version)
Add a GameObject to the scene, attach:
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!");
}
}
}
Configure Unity build settings as specified for HoloLens 1, making sure to include InternetClient, InternetClientServer, PrivateNetworkClientServer, in Publishing settings > Capabilities.
Build, open resulting solution in either Visual Studio 2017 or 2019 with appropriate build tools installed. Deploy to HoloLens and the resulting error will be output to the debug console.
Thanks for reading, I would really appreciate any insights anyone has.
回答1:
Looks like this is a bug in Unity. See this: https://forum.unity.com/threads/il2cpp-failing-in-windows-machine.891436/#post-5944052
Workaround involves changing il2cpp source code:
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)
{
回答2:
Regarding the issue as TcpClient socket connect causes the ArgumentException: Value does not fall within the expected range.
error. We have confirmed that this is a tracking Unity bug internally and should has pushed a fix. And there is already a discussion in Unity forum: https://forum.unity.com/threads/il2cpp-failing-in-windows-machine.891436/#post-5944052
You can try to verify the latest Unity 2019.4.x version to see if the fix has been merged or check the workaround in the above community discussion.
来源:https://stackoverflow.com/questions/62314810/on-a-hololens-1-when-creating-tcpclient-object-with-default-constructor-argume