Getting error on android mobile device - An address incompatible with the requested protocol was used

Hi, How can I force web socket to use IPv4 instead of IPv6 in Unity Client as in some cases on mobile network, while connecting socket I’m getting this error- (on WIFI it works fine)

An address incompatible with the requested protocol was used, StackTrace =   at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x00000] in <00000000000000000000000000000000>:0
08-12 12:40:35.560  3957  3974 E Unity   :   at System.Net.Sockets.TcpClient.EndConnect (System.IAsyncResult asyncResult) [0x00000] in <00000000000000000000000000000000>:0
08-12 12:40:35.560  3957  3974 E Unity   :   at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00000] in <00000000000000000000000000000000>:0
08-12 12:40:35.560  3957  3974 E Unity   :   at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncImpl[TArg1,TArg2] (System.Func`5[T1,T2,T3,T4,TResult] beginMethod, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, TArg1 arg1, TArg2 arg2, System.Object state, System.Threading.Tasks.TaskCreationOptions creationOptions) [0x00000] in <0000000000

I googled about this issue An address incompatible with the requested protocol was used and found out that, this issue is due to IPv6.
Then for testing it out, I disabled the IPv6 on the mobile device and the error was gone and successfully connected to socket while being on mobile network.

So, how can I ensure WebSocket is using the correct protocol.

@hdjay0129 What version of Unity engine do you use? Is the mobile device on T-Mobile or Sprint networks?

Please try and add this line to before you initialize the client object in the SDK and see if it brings an improvement:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var client = new Client(scheme, host, port, serverKey);
// etc.

@novabyte, We are using Unity version 2018.4.3f1.
The mobile devices are on India’s vodafone carrier networks.
Tried on two different devices having vodafone 4G LTE and same issue & when changed the carrier’s APN Protocol to use IPv4 only instead of IPv4/IPv6, websocket successfully connected as seen in Logs on ManagedCloud.

I tried that with the new build containing the code you provided.

Interesting. Can you please reach out to us via email and provide the hostname so we can look into the project network setup for you?

@mofirouz sure.
Just sent a support email.

@hdjay0129 I’ve done some investigation into this error from Unity engine and tracked it down to this Mono runtime issue. Looks like it was patched 15 months ago but until Unity engine adopts the version I don’t think there’s a workaround. Sadly the entire patch is just 3 lines of code.

Is it possible you could update your Unity project to the latest 2019.x release? Before you do make sure to take a complete project backup because Unity has a habit of breaking more things than they fix in each release. This is not an issue with Nakama or the load balancer configuration.

Then let me know what version of Mono runtime is in use in the latest 2019 release you try with this code:

Type type = Type.GetType("Mono.Runtime");
if (type != null)
{
    MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
    if (displayName != null)
    {
        Debug.Log(displayName.Invoke(null, null));
    }
}

@novabyte We are on Unity 2018.4.3f1 LTS version and It won’t be possible to update the project to the latest 2019.x version until LTS release of 2019 version.

I can definitely, make a copy of this project. I’ll test that out in the latest 2019.x Unity version and get back with results.

What’s strange is that we are only facing this on a particular mobile carrier’s data and with other mobile carriers and WiFi there’s no such issue, so I’m assuming that with WiFi and other mobile carriers, it first tries IPv4 and passes while in case of this particular mobile data it first tries IPv6 and fails.

so I’m assuming that with WiFi and other mobile carriers, it first tries IPv4 and passes while in case of this particular mobile data it first tries IPv6 and fails.

Yep I think you’re absolutely right. It really seems to just depend on the order that IPv4 or IPv6 is tried with the carrier network settings. Let me know what info you can give for the 2019.x output on the Mono version.

@novabyte So this it the output we got for Mono version from Unity versions -

  • 2018.4.3f1 - 5.11.0 (Visual Studio built mono)
  • 2019.2.2f1 - 5.11.0 (Visual Studio built mono)

@hdjay0129 That doesn’t look like the typical output from the code I shared above. For example in Unity 2018 release the code above outputs the information like so:

5.11.0 (unity-2018.3-mbe/77a1ed72a23)

What’s important in the exact output above is the commit SHA ("77a1ed72a23") of the checkout for the Mono runtime that Unity used in their release. I don’t see that in what you’ve pasted.

@novabyte As I thought, Unity is giving different output for that on mac and windows. Earlier output was on Windows.

On mac I got this output for Unity Version,

  • 2018.4.3f1 - 5.11.0 (unity-2018.3-mbe/77a1ed72a23)
  • 2019.2.2.f1 - 5.11.0 ((HEAD/ed4090cc21f)

And also, as you know, we have another project with nakama which is on Unity version 2018.2.2f1, and we checked and find out it doesn’t have this problem.

@hdjay0129 Ok I think that with this proof point:

we have another project with nakama which is on Unity version 2018.2.2f1, and we checked and find out it doesn’t have this problem.

We can conclude it’s a bug in Unity engine. Is it possible for you to downgrade your current project to that version of Unity and test whether it’s solved with it?

Hi @novabyte,
So I downgraded my unity project to that version and tested it out and got the same issue.

So, I checked more,
I created a sample scene with below code and tried it in both of our project with Unity version 2018.2.21f1.
What’s strange is that it worked in our previous project and not in the new one.
The only difference in both of these is Nakama.
In our new project we have latest nakama unity client v2.2.2 where’s the older project has custom made nakama unity client based on version 2.1.0 with web build support which I received from @mofirouz for our previous project.

(new project) - sample code - with nakama unity client v2.2.2

using UnityEngine;
using UnityEngine.UI;
using Nakama;

public class NSM : MonoBehaviour
{
    public Text text;
    public string playerId = "some_player_id";
    public string ip = "managed_cloud_host";
    public int port = 7350;
    public string key = "server_key";

    private async void Awake()
    {
        var client = new Client( "https", ip, port, key);
        var session = await client.AuthenticateCustomAsync(playerId);

        Debug.LogFormat("[{0}] Session created", "NSM/ConnectSocket");
        text.text = "Session created";

        var socket = client.NewSocket();

        Debug.LogFormat("[{0}] Socket Created", "NSM/ConnectSocket");
        text.text = "Socket created";

        await socket.ConnectAsync(session, true);

        text.text = "Socket Connected";
        Debug.LogFormat("[{0}] Socket connected", "NSM/ConnectSocket");
    }
}

(old project) - sample code - with nakama unity client v2.1.0
    private async void Awake()
    {
        var client = new Client(key, ip, port, true);
        var session = await client.AuthenticateCustomAsync(playerId);

        Debug.LogFormat("[{0}] Session created", "NSM/ConnectSocket");
        text.text = "Session created";

        var socket = client.CreateWebSocket();

        Debug.LogFormat("[{0}] Socket Created", "NSM/ConnectSocket");
        text.text = "Socket created";

        await socket.ConnectAsync(session);

        text.text = "Socket Connected";
        Debug.LogFormat("[{0}] Socket connected", "NSM/ConnectSocket");
    }

@hdjay0129 This is great. I have an idea on an experiment which could work around the issue in Unity engine. I’ll drop you a note when I’ve got a prerelease build you can try.

@hdjay0129 I’ve put together a minimal prerelease of the Nakama client with a patch I think will work around the issue in the Mono runtime with Unity engine.

Nakama-prerelease.unitypackage.txt (97.2 KB)

The forum does not allow files with “.unitypackage” extension so I’ve added “.txt” to the end which you can strip from the filename.

Please try it with your project and on device with the problematic network if you can and let me know if it resolves it.

1 Like

@novabyte With this fix included, test build worked fine on our devices where it was happening before. Hope it has been fixed once for all as not seeing that issue anymore after many tries and socket connection seems to be working fine.
Many thanks @novabyte :blush:
Will update you here if any of our player encounters this issue.

A final update for this thread. After extensive tests with various studios and developer teams as well as the help from @hdjay0129 we’ve released the 2.3.1 version of the client sdk with a workaround for the issue in Unity engine and their packaged version of Mono.

1 Like