zeromq安装使用
一、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復制到該路徑即可。)
總結
以上是生活随笔為你收集整理的zeromq安装使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 3.x 爬虫基础---ht
- 下一篇: MySQL教程(三)—— MySQL的安