SocketException: Too many open files. Please advise!

JordanRestifoJordanRestifo USMember
edited June 2016 in Xamarin.iOS

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?

Tagged:

Best Answer

Answers

  • AnilMathpatiAnilMathpati INMember ✭✭

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

  • @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/Common_SimplePing_m.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.

  • LochanGabdaLochanGabda USMember ✭✭

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

Sign In or Register to comment.