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

2 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