3

I have been looking into zmq for a while now and have implemented a simplified poc - to mimic my infrastructure design - using it (specifically using the NetMQ wrapper), with great results.

My situation is this:

In the future I am planning to run multiple clients on a single machine, where each client needs to communicate with a "server" (located on a different machine) via multiple sockets.

I've noticed that for each socket that I declare and open explicity many more are opened internally by zmq and are managed by it.

Edit

binding sockets get a new port allocated in the dynamic range and that is fine,

but although my client connects explicitly to only 2 ports, some 15 ports are allocated for him automatically by zmq.

this, I fear, might eventually lead to a shortage of ports, a situation I very much want to avoid.

The questions:

  1. How does zmq allocates ports intenally and what is the ratio between explicitly declared sockets and the ports zmq openes automatically?

  2. Is there a way for me to control the port allocation programatically, via configuration or by any other means?

  3. How does using a poll affects the ports usage?

Tnx,

barakcaf
  • 1,294
  • 2
  • 16
  • 27
  • 2
    This sounds a little like "if it ain't broke, don't fix it" sort of situation. If you test your application with a large number of clients, do you actually run into a problem? If you tune the max. number of open files available on the system and to your application, does the problem go away? – larsks Aug 10 '15 at 14:00
  • i've edited the question - currently i have a poc, before moving on i want to catch possible hurdles. this potentially might be a big one, i would like to avoid in advance if possible – barakcaf Aug 10 '15 at 14:22

1 Answers1

3

When you create a socket on zeromq/netmq on windows a dedicated socket is used to signal between io thread and user thread, this socket take two ports. if you call bind you bind another port with your selected port.

The dedicated socket is using the dynamic port range (netmq), so if you stay away from that range you will not have any problem.

Dynamic port range for windows vista and above is 49152 until 65535

port counting code:

static void Main(string[] args)
{
    var id = Process.GetCurrentProcess().Id;

    using (var context = NetMQContext.Create())
    {
        List<NetMQSocket> sockets = new List<NetMQSocket>();

        NetMQSocket server = context.CreateDealerSocket();
        server.Bind("tcp://localhost:6666");

        int i= 0;
        while (true)
        {
            var client = context.CreateDealerSocket();
            client.Connect("tcp://localhost:6666");

            sockets.Add(client);

            Thread.Sleep(1000);                                       

            ProcessStartInfo startInfo = new ProcessStartInfo("NETSTAT.EXE", "-a -o");
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;

            Console.WriteLine("Calculating taken ports...");

            Process process  = Process.Start(startInfo);                    
            int portCounter = -7; // start with minus 4 for framework and server socket

            while (!process.StandardOutput.EndOfStream)
            {
                if (process.StandardOutput.ReadLine().Contains(id.ToString()))
                {
                    portCounter ++;
                }
            }

            Console.Clear();
            Console.WriteLine("{0} sockets takes {1} ports, avg of {2} ports per socket", sockets.Count, portCounter, portCounter / sockets.Count);
            Console.WriteLine("Press enter to create another socket");

            Console.ReadLine();
        }
    }
}
Jason
  • 13,606
  • 2
  • 29
  • 40
somdoron
  • 4,653
  • 2
  • 16
  • 24
  • i've noticed that when i connect to a socket (not only bind) additional ports are also opened (in the dynamic port range) - if i have many long running clients and each openes many ports isn't there a danger of running out of ports? – barakcaf Aug 10 '15 at 14:43
  • so yes, each socket take 2 ports from dynamic range. You will only have a problem if you open more than 8191 sockets. – somdoron Aug 10 '15 at 14:48
  • i've been reading your blog and learning a lot from it (are you the wuthor of NetMQ?), i've edited my question to be more specific please respond if you can – barakcaf Aug 11 '15 at 05:46
  • 3
    thanks, I'm the founder. I don't think 15 ports are taken. When you allocate a socket you take 2 and the netmq framework take another 4, but only for the first socket. so if you create more sockets each one should 2 additional ports. when you connect you need one more port by the design of tcp (also from dynamic range) and the server also need one more. So bottom line 3 ports for every connecting socket and 1 more at the server side for every socket connected. – somdoron Aug 11 '15 at 15:45
  • I'm not sure I understand, polls on the ports usage? – somdoron Aug 12 '15 at 09:37
  • yes, do they affect the port usage? does using a pool on a socket requires additional port allocation? – barakcaf Aug 12 '15 at 10:13
  • @somdoron Can you please explain what is the purpose of the two ports taken when a socket is allocated? – Raihan Jul 18 '23 at 06:40