locked
SocketException: Too many open files. Please advise! RRS feed

  • Question

  • User192065 posted

    I am working on code that builds an ARP table of every host on a subnet and then finds the hosts where the OUI portion of the MAC address matches a specific manufacturer. I have no issues on Android, works like a dream. On iOS though, I am getting a SocketException: Too many open files. The code looks like this:

    private async Task ResolveIpAsync(string ip)
    {
        var client = new TcpClient();
        client.NoDelay = true;
        client.ReceiveTimeout = 1000;
        client.SendTimeout = 1000;
        try
        {
            var task = client.ConnectAsync(ip, 1100);
            await Task.WhenAny(task, Task.Delay(500));
            client.GetStream().Close();
            client.Close();
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message + "\r\n" + e.StackTrace);
        }
    }
    

    Then, the "discovery" looks like this:

    public async Task StartDiscoveryAsync() { var ipCollection = GetIpsInSubnet();

        var taskGroups = new List<List<Task>>();
        var tasks = new List<Task>();
        var j = 0;
        taskGroups.Add(tasks);
    
        for (var i = 1; i < ipCollection.Count - 1; i++)
        {
            tasks.Add(ResolveIpAsync(ipCollection[i].ToString()));
    
            if (++j < 10) continue;
    
            j = 0;
            tasks = new List<Task>();
            taskGroups.Add(tasks);
        }
    
        Debug.WriteLine(taskGroups.Count);
    
        foreach (var taskGroup in taskGroups)
        {
            await Task.WhenAll(taskGroup);
        }
    
        OnDiscoveryFinished();
    }
    

    Originally I had one single Task.WhenAll in the discovery process, and thought maybe that was causing too many sockets to open at once. So, I tried splitting them into groups of ten as you see here. Same problem. Then I thought, maybe TcpClient.Close doesn't really work, and I saw this https://support.microsoft.com/en-us/kb/821625 so I attempted to do the same and call Close on the NetworkStream. Still no dice. This is for a subnet of 253 hosts (I skipthe network IP and the broadcast IP). Originally I was using SimplePing and had a binding library, but that was causing problems of its own (though it did work, it had performance issues). Is there anything I can do to resolve this issue?

    Thursday, June 30, 2016 1:27 AM

Answers

  • User192065 posted

    I reverted to my ping implementation and worked out the performance issues. Even UdpSocketClient with SendToAsync, wrapped in a using block has the same problem. I think there is an issue with either Xamarin (or Mono's) socket implementation on iOS, or with the garbage collector. Oh well...

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, June 30, 2016 3:06 AM

All replies

  • User192065 posted

    I reverted to my ping implementation and worked out the performance issues. Even UdpSocketClient with SendToAsync, wrapped in a using block has the same problem. I think there is an issue with either Xamarin (or Mono's) socket implementation on iOS, or with the garbage collector. Oh well...

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, June 30, 2016 3:06 AM
  • User31867 posted

    I am also facing this issue. It was working fine for a year, till last month when some users reported the issue. Any suggestion.

    Friday, February 10, 2017 8:54 AM
  • User192065 posted

    @AnilMathpati said: I am also facing this issue. It was working fine for a year, till last month when some users reported the issue. Any suggestion.

    I would suggest you implement it in plain old Objective C and then create a binding library. I was unable to get around the issue in Xamarin/C#. I suspect, but am not 100% sure that it is at its core an issue with Mono, but the project was moving too quickly for me to attack it from that angle.

    I used "https://developer.apple.com/library/content/samplecode/SimplePing/Listings/CommonSimplePingm.html" and a binding library. I did make some changes to make the code more efficient, but I will have to dig around to find the exact changes I made. I will find some time to post my version of SimplePing and the binding library to Github as soon as possible if you would like.

    Saturday, February 11, 2017 1:16 AM
  • User259421 posted

    Hi, any updated on this issues? We are facing the same issue in our apps.

    Tuesday, November 6, 2018 7:48 AM