一、zeromq简介
zeromq的官方网站:http://zeromq.org/ , 简介如下:
ZeroMQ (also known as ØMQ, 0MQ, or zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry atomic messages across various transports like in-process, inter-process, TCP, and multicast. You can connect sockets N-to-N with patterns like fan-out, pub-sub, task distribution, and request-reply. It's fast enough to be the fabric for clustered products. Its asynchronous I/O model gives you scalable multicore applications, built as asynchronous message-processing tasks. It has a score of language APIs and runs on most operating systems. ZeroMQ is from iMatix and is LGPLv3 open source.
二、安装
从官网下载zeromq-4.0.7.tar.gz,
tar zxvf zeromq-4.0.7.tar.gz cd zeromq-4.0.7 ./configure --prefix=/usr/local/zeromq make make install
root@ubuntu:zeromq-4.0.7# ls /usr/local/zeromq/
bin include lib share
三、使用
以下是用zeromq实现的简单的echo服务器:
zmqsvr.c:
1 #include "zmq.h" 2 #include <string.h> 3 #include <unistd.h> 4 5 int main(void) 6 { 7 void* zmq_ctx = zmq_ctx_new(); 8 void* zmq_sock = zmq_socket(zmq_ctx, ZMQ_REP); 9 zmq_bind(zmq_sock, "tcp://*:5555"); 10 11 printf("start listen for socket... "); 12 int seq = 0; 13 while (1) 14 { 15 printf(" loop seq: %d ", seq++); 16 int msg_size = 0; 17 char buf[10] = {0}; 18 zmq_msg_t request; 19 zmq_msg_init(&request); 20 zmq_msg_recv(&request, zmq_sock, 0); 21 msg_size = zmq_msg_size(&request); 22 memcpy(buf, zmq_msg_data(&request), msg_size); 23 printf("recv request: %s ", buf); 24 zmq_msg_close(&request); 25 26 sleep(1); 27 28 zmq_msg_t reply; 29 zmq_msg_init_size(&reply, msg_size); 30 memcpy(zmq_msg_data(&reply), buf, msg_size); 31 printf("send reply: %s ", buf); 32 zmq_msg_send(&reply, zmq_sock, 0); 33 zmq_msg_close(&reply); 34 } 35 36 sleep(1); 37 zmq_close(zmq_sock); 38 zmq_ctx_destroy(zmq_ctx); 39 40 return 0; 41 }
zmqcli.c:
1 #include <zmq.h> 2 #include <stdio.h> 3 #include <unistd.h> 4 #include <string.h> 5 6 int main(void) 7 { 8 void* zmq_ctx = zmq_ctx_new(); 9 void* zmq_sock = zmq_socket(zmq_ctx, ZMQ_REQ); 10 zmq_connect(zmq_sock, "tcp://localhost:5555"); 11 12 int index = 0; 13 for (index = 0; index < 10; index++) 14 { 15 printf(" loop seq:%d ", index); 16 zmq_msg_t request; 17 zmq_msg_init_size(&request, 6); 18 char buf[10] = {0}; 19 sprintf(buf, "hello%d", index); 20 memcpy(zmq_msg_data(&request), buf, 6); 21 printf("send request: %s ", buf); 22 zmq_msg_send(&request, zmq_sock, 0); 23 zmq_msg_close(&request); 24 25 memset(buf, 0, sizeof(buf)); 26 zmq_msg_t reply; 27 zmq_msg_init(&reply); 28 zmq_msg_recv(&reply, zmq_sock, 0); 29 memcpy(buf, zmq_msg_data(&reply), zmq_msg_size(&reply)); 30 printf("recv reply: %s ", buf); 31 zmq_msg_close(&reply); 32 } 33 34 sleep(1); 35 zmq_close(zmq_sock); 36 zmq_ctx_destroy(zmq_ctx); 37 38 return 0; 39 }
zmqsvr和zmqcli实现了从client端发送helloi, server端收到后并echo回给client。
makefile如下:
1 TARGET = zmqsvr zmqcli 2 3 all: $(TARGET) 4 5 CFLAGS = -g -I/usr/local/zeromq/include/ -Wall 6 LDFLAGS = -L/usr/local/zeromq/lib/ 7 8 zmqsvr:zmqsvr.c 9 gcc $(CFLAGS) $< -o $@ $(LDFLAGS) -lzmq 10 11 zmqcli:zmqcli.c 12 gcc $(CFLAGS) $< -o $@ $(LDFLAGS) -lzmq 13 14 clean: 15 rm -rf $(TARGET)
root@ubuntu:zeromq# make
gcc -g -I/usr/local/zeromq/include/ -Wall zmqsvr.c -o zmqsvr -L/usr/local/zeromq/lib/ -lzmq
gcc -g -I/usr/local/zeromq/include/ -Wall zmqcli.c -o zmqcli -L/usr/local/zeromq/lib/ -lzmq
root@ubuntu:zeromq# ./zmqcli loop seq:0 send request: hello0 recv reply: hello0 loop seq:1 send request: hello1 recv reply: hello1 ... loop seq:9 send request: hello9 recv reply: hello9
另一个终端里:
mamo@ubuntu:zeromq$ ./zmqsvr start listen for socket... loop seq: 0 recv request: hello0 send reply: hello0 ... loop seq: 9 recv request: hello9 send reply: hello9 loop seq: 10
(如果运行时出现cannot found libzmq.so.4之类的错误,可以使用 strace ./zmqsvr查看调用的libzmq.so.4的路径,比如/lib/i386-linux-gnu/i686/,将libzmq.so.4复制到该路径即可。)