zmq 是一个基于c的消息队列编程框架。zeromq用处很多, 它具有:
//
// mqclient.c
// Hello World client
// Connects REQ socket to tcp://localhost:5555
// Sends "Hello" to server, expects "World" back
//
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main (void)
{
void *context = zmq_ctx_new ();
// Socket to talk to server
printf ("Connecting to hello world server…\n");
void *requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://localhost:5555");
int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
zmq_msg_t request;
zmq_msg_init_data (&request, "Hello", 6, 0, 0);
printf ("Sending Hello %d…\n", request_nbr);
zmq_msg_send (&request, requester, 0);
zmq_msg_close (&request);
printf ("prepare recv message\n");
zmq_msg_t reply;
zmq_msg_init (&reply);
if (-1==zmq_msg_recv (&reply, requester, 0)) {
printf("recv data error.\n");
}
printf ("Received World %d\n", request_nbr);
zmq_msg_close (&reply);
}
zmq_close (requester);
zmq_ctx_destroy (context);
return 0;
}
//
// mqserver.c
// Hello World server
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main (void)
{
int ret;
char buf[5]="world";
buf[5] = 0;
void *context = zmq_ctx_new ();
// Socket to talk to clients
void *responder = zmq_socket (context, ZMQ_REP);
ret = zmq_bind (responder, "tcp://*:5555");
if (ret==0) {
printf("zmq_bind success\n");
} else {
printf("zmq_bind error=%d:%s\n", ret, strerror(errno));
exit(ret);
}
while (1==1) {
// Wait for next request from client
printf("wait for clients...\n");
zmq_msg_t request;
zmq_msg_init (&request);
ret = zmq_msg_recv (&request, responder, 0);
printf ("Received=%d\n", ret);
zmq_msg_close (&request);
// Do some 'work'
sleep (1);
// Send reply back to client
zmq_msg_t reply;
zmq_msg_init_data (&reply, buf, 6, 0, 0);
ret = zmq_msg_send (&reply, responder, 0);
printf("zmq_msg_send ret=%d\n", ret);
zmq_msg_close (&reply);
}
// We never get here but if we did, this would be how we end
zmq_close (responder);
zmq_ctx_destroy (context);
return 0;
}
$ ./mqclnt
参考: http://zguide.zeromq.org/py:all
#############################################################
# mqpub.py
# Weather update server
# Binds PUB socket to tcp://*:5556
# Publishes random weather updates
#############################################################
import zmq
import random
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")
while True:
zipcode = random.randrange(1,100000)
temperature = random.randrange(1,215) - 80
relhumidity = random.randrange(1,50) + 10
socket.send("%d %d %d" % (zipcode, temperature, relhumidity))
#############################################################
# mqsub.py
# Weather update client
# Connects SUB socket to tcp://localhost:5556
# Collects weather updates and finds avg temp in zipcode
#############################################################
import sys
import zmq
# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)
print "Collecting updates from weather server"
socket.connect ("tcp://localhost:5556")
# Subscribe to zipcode, default is NYC, 10001
zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"
socket.setsockopt(zmq.SUBSCRIBE, zip_filter)
# Process 5 updates
total_temp = 0
for update_nbr in range (5):
string = socket.recv()
zipcode, temperature, relhumidity = string.split()
total_temp += int(temperature)
print "Average temperature for zipcode '%s' was %dF" % (zip_filter, total_temp / update_nbr)
$ python ./mqpub.py
$ python ./mqsub.py