Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 47dfc075a0
Fetching contributors…

Cannot retrieve contributors at this time

file 49 lines (44 sloc) 1.854 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
//
// Reading from multiple sockets
// This version uses zmq_poll()
//

// Author: Michael Compton
// Email: michael.compton@littleedge.co.uk

using System;
using System.Text;
using ZMQ;

namespace ZMQGuide {
    class Program {
        static void Main(string[] args) {
            // Prepare our context and socket
            using (Context context = new Context(1)) {
                // Connect to task ventilator and weather server
                using (Socket receiver = context.Socket(SocketType.PULL),
                    subscriber = context.Socket(SocketType.SUB)) {
                    receiver.Connect("tcp://localhost:5557");
                    subscriber.Connect("tcp://localhost:5556");
                    subscriber.Subscribe("10001 ", Encoding.Unicode);

                    PollItem[] items = new PollItem[2];
                    items[0] = receiver.CreatePollItem(IOMultiPlex.POLLIN);
                    items[0].PollInHandler += new PollHandler(ReceiverPollInHandler);
                    items[1] = subscriber.CreatePollItem(IOMultiPlex.POLLIN);
                    items[1].PollInHandler += new PollHandler(SubscriberPollInHandler);
                    // Process messages from both sockets
                    while (true) {
                        context.Poll(items, -1);
                    }
                }
            }
        }
        // Task Processing event
        public static void ReceiverPollInHandler(Socket socket, IOMultiPlex revents) {
            socket.Recv();
            Console.WriteLine("Process Task");
        }

        // Weather server event
        public static void SubscriberPollInHandler(Socket socket, IOMultiPlex revents) {
            socket.Recv();
            Console.WriteLine("Process Weather");
        }
    }
}
Something went wrong with that request. Please try again.