C# Binding (.NET & Mono)

clrzmq4

ZeroMQ namespace in C# (.NET 4 and mono 3) - currently using libzmq v4.1.0.

This is built AnyCPU, to run on Windows (VC2010) and on GNU/Linux (C 4.8.2), i386 and amd64.
You can also use your own libzmq distribution, by replacing the files in i386 and amd64 folders.

Binaries

Available on GitHub and on nuget.

Source Code

http://github.com/zeromq/clrzmq4

Sample code

public static void HWClient(string[] args)
{
    // Create
    using (var context = new ZContext())
    using (var requester = new ZSocket(context, ZSocketType.REQ))
    {
        // Connect
        requester.Connect("tcp://127.0.0.1:5555");
 
        for (int n = 0; n < 10; ++n)
        {
            string requestText = "Hello";
            Console.Write("Sending {0}...", requestText);
 
            // Send
            requester.Send(new ZFrame(requestText));
 
            // Receive
            using (ZFrame reply = requester.ReceiveFrame()) 
            {
                Console.WriteLine(" Received: {0} {1}!", requestText, reply.ReadString());
            }
        }
    }
}

NetMQ

Native C# port of ZeroMQ. Stable and actively maintained.

Binaries

Available on Nuget.

Source Code

http://github.com/zeromq/netmq

Sample code

using (NetMQContext ctx = NetMQContext.Create())
{
    using (var server = ctx.CreateResponseSocket())
    {
        server.Bind("tcp://127.0.0.1:5556");
        using (var client = ctx.CreateRequestSocket())
        {
            client.Connect("tcp://127.0.0.1:5556");
            client.Send("Hello"); 
 
            string m1 = server.ReceiveString();
            Console.WriteLine("From Client: {0}", m1);
            server.Send("Hi Back");
 
            string m2 = client.ReceiveString();
            Console.WriteLine("From Server: {0}", m2);
            Console.ReadLine();
        }
    }
}

Castle.Zmq

Modern .Net binding targeting ØMQ v4.0.5. Released under the LGPL license.

Common ZMQ patterns also available such as proxy/device, shared queue, forwarders, workerpool using idiomatic C#.

Binaries

Will soon be available on nuget

Source code

https://github.com/castleproject/castlezmq

Sample code

const string MsgReq = "Hello";
const string MsgReply = "World";
 
using (var ctx = new Context())
using (var reqSocket = ctx.CreateSocket(SocketType.Req))
using (var repSocket = ctx.CreateSocket(SocketType.Rep))
{
    repSocket.Bind("tcp://0.0.0.0:90002");
 
    reqSocket.Connect("tcp://127.0.0.1:90002");
 
    reqSocket.Send(MsgReq);
 
    var msg = repSocket.Recv();
    var msgStr = Encoding.UTF8.GetString(msg);
 
    repSocket.Send(MsgReply);
 
    msg = reqSocket.Recv();
    msgStr = Encoding.UTF8.GetString(msg);
}