3

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
    Have you tried to use a Socket instead of a TCPClient? It seems there is some problem with the default option for the "DontFragment" option set by default, creating manually the socket will allow you to specify it's options and then you may be able to avoid the problem. – Gusman Jun 10 '20 at 23:51
  • Thank you that's a fantastic idea. I'm going to test this tomorrow and I will report back. – Aaron Koenigsberg Jun 11 '20 at 03:14
  • I got the same error creating a socket from scratch. I was able to get past the error on a different machine, however. Best I can tell it has something to do with a different Windows SDK. 10.0.17xxx and 10.0.18xxx are on the machine that works and those 2 plus 10.0.19xxx on the machine that breaks. I'm thinking it has something to do with that since one of the configuration in unity targets the latest SDK – Aaron Koenigsberg Jun 12 '20 at 16:46

2 Answers2

4

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)
                 {
Sunius
  • 2,789
  • 18
  • 30
  • I looked into this, however my SocketImpl.cpp doesn't have that snippet at 1777. I tried just modifying 1700, however it didn't have an effect. Like my other comment I think it has to do with the Windows SDK version. – Aaron Koenigsberg Jun 12 '20 at 16:46
  • I can confirm that it has been fixed in Unity >= 2019.4.5f1; see: https://forum.unity.com/threads/il2cpp-failing-in-windows-machine.891436/#post-6138168 – Marek Stój Sep 26 '20 at 11:06
0

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.

Franklin Chen - MSFT
  • 4,845
  • 2
  • 17
  • 28