Release History

  • ZeroMQ Interop v0.8.190.10354 (beta) - includes a rough test suite covering the complete 2.0.10 p/invoke API; includes a fully-featured ZmqSocket class and its subclasses, all of which should be stable.

Project Goals

  • Ability to p/invoke all exports from the ZeroMQ C library
  • Thin, easy-to-use wrapper classes around the p/invoke API
  • Compile with any C# 3.5 compiler, run on any ECMA-334 compatible CLI
  • Compatibility with both the Microsoft CLR and the Mono CLR
  • Support for both 32 and 64 bit versions of the library on any platform that has a CLR and an official ZeroMQ port.

Example 1

// Set up a publisher.

var publisher = new ZmqPublishSocket {
	Identity = Guid.NewGuid().ToByteArray(),
	RecoverySeconds = 10
};

publisher.Bind( address: "tcp://127.0.0.1:9292" );

// Set up a subscriber.

var subscriber = new ZmqSubscribeSocket();

subscriber.Connect( address: "tcp://127.0.0.1:9292" );

subscriber.Subscribe( prefix: "" ); // subscribe to all messages

// Add a handler to the subscriber's OnReceive event

subscriber.OnReceive += () => {

	String message;
	subscriber.Receive( out message, nonblocking: true );

	Console.WriteLine( message );
};

// Publish a message to all subscribers.

publisher.Send( "Hello world!" );

Example 2

var reply_socket = new ZmqReplySocket();
reply_socket.Connect( "tcp://127.0.0.1:12929" );

reply_socket.OnSend += ( msg, mp ) => {

	Console.WriteLine( "Sent {0} byte reply.", msg.Length );
};

reply_socket.OnReceive += () => {

	Byte[] msg;
	reply_socket.Receive( out msg, true );
	var message = Encoding.ASCII.GetString( msg );
	
	if( message.StartsWith( "syn" ) ) {

		Console.WriteLine( "Got {0} byte request.", msg.Length );
		reply_socket.Send( Encoding.ASCII.GetBytes( "ack" ) );
	}
};

var request_socket = new ZmqRequestSocket();
request_socket.Bind( "tcp://127.0.0.1:12929" );

request_socket.OnSend += ( msg, mp ) => {

	Console.WriteLine( "Sent {0} byte request.", msg.Length );
};

request_socket.OnReceive += () => {

	Byte[] msg;
	request_socket.Receive( out msg, true );
	var message = Encoding.ASCII.GetString( msg );
	
	if( message.StartsWith( "ack" ) == false ) {

		throw new Exception();
	}

	Console.WriteLine( "Got {0} byte reply.", msg.Length );
};

request_socket.Send( Encoding.ASCII.GetBytes( "syn" ) );

Last edited May 22, 2011 at 5:42 PM by AlexForster, version 16

Comments

arosca Feb 13, 2013 at 2:48 PM 
minor bug in the examples above: the Connect and Bind methods are swapped in the request and reply sockets.

Code should be:

var reply_socket = new ZmqReplySocket();
reply_socket.Bind( "tcp://127.0.0.1:12929" );

var request_socket = new ZmqRequestSocket();
request_socket.Connect( "tcp://127.0.0.1:12929" );