[zeromq-dev] [NetMQ] How to properly free a monitor in order to be able to recreate one?

Leonard Michelet leonard.michelet at openwide.fr
Mon Mar 23 17:48:39 CET 2015


Hello everyone, I'm having problem using Monitor with NetMQ. Please tell me if there is a dedicated place to ask about NetMQ, or possibly report bug.
Here's what I do :
 1) I create and bind a DEALER socket, associate a monitor to it and start monitoring it.
 2) Then I Unbind the socket, stop monitoring, dispose everything but the NetMQContext.
 3) Finally, I want to start again 1) but I receive a NetMQException because the inproc socket created by the DEALER socket to send event to its monitor is still alive.

Next is a simple code to see my problem : 

using System;
using System.Threading;
using NetMQ;
using System.Threading.Tasks;
using NetMQ.Monitoring;
using NetMQ.zmq;

namespace Server
{
    class ServerProg
    {
        static void CreateServerSocket(NetMQContext ctx)
        {
            try
            {
                using (var server = ctx.CreateDealerSocket())
                {
                    var monitor = new NetMQMonitor(ctx, server, "inproc://*:4556.monitor",
                                                   SocketEvent.Connected | SocketEvent.Disconnected);
                    Task monitorTask = Task.Factory.StartNew(monitor.Start);
                    server.Bind("tcp://*:4556");
                    Thread.Sleep(500);
                    server.Unbind("tcp://*:4556");
                    monitor.Stop();
                    monitor.Dispose();
                    server.Close();
                    server.Dispose();
                }
            }
            catch (NetMQException e)
            {
                Console.Out.WriteLine("Exception: " + e);
                throw;
            }
        }

        static void Main(string[] args)
        {
            using (NetMQContext ctx = NetMQContext.Create())
            {
                CreateServerSocket(ctx);
                CreateServerSocket(ctx);
            }
        }
    }
}



More information about the zeromq-dev mailing list