Wednesday, 28 September 2011

ØMQ (Zero MQ)

ZeroMQ is a socket library that allows developers to create distributed, concurrent, queue based messaging systems with high availability.  One of the many benefits is the abstraction of dealing with sockets at a queue and atomic message level.

It’s really simple to use, here is an example:

Client

namespace Client
{
using System;
using System.Text;

class Program
{
static void Main()
{
using (var ctx = new ZMQ.Context(1))
{
var socket = ctx.Socket(ZMQ.REQ);
socket.Connect("tcp://localhost:5555");

while (true)
{
socket.Send(Encoding.ASCII.GetBytes("Hello"));

byte[] message;
socket.Recv(out message);

Console.WriteLine(Encoding.ASCII.GetString(message));
}
}
}
}
}

Server

namespace Server
{
using System;
using System.Text;

class Program
{
static void Main()
{
using (var ctx = new ZMQ.Context(1))
{
var socket = ctx.Socket(ZMQ.REP);
socket.Bind("tcp://*:5555");

while (true)
{
byte[] message;
socket.Recv(out message);

Console.WriteLine(Encoding.ASCII.GetString(message));

socket.Send(Encoding.ASCII.GetBytes("World"));
}
}
}
}
}

This sample was taken from the internet but you can download a snapshot of the source here:


http://stevenhollidge.com/blog-source-code/ZeroMqDemo.zip

4 comments:

  1. Do you know how to use the subscribe part?

    ReplyDelete
  2. um, congratulations on a content-free blog post? can I, too, make money saying nothing?

    ReplyDelete
  3. The code samples were hosted on pastie.org which has unfortunately dropped out of the internet and taken my code samples with it (hence you can't see the code) but you can still download the example code in the link at the bottom of the blog post.

    I don't make any money out of this site. This post was a resource for me to get a working example up and running without having to trawl the net for the sample.

    All the newer posts have the example code embedded in the blog posts with full project source hosted on github.

    Hope you find some of the code in one or two of my posts useful.

    ReplyDelete
  4. where did you get the .dll for in the project? How come the class names, namespaces, etc. differ from these examples? https://github.com/imatix/zguide/tree/master/examples/C%23

    ReplyDelete