I. Introduction of ZEROMQ
ZEROMQ's official website: http://zeromq.org/, here's a brief introduction:
ZeroMQ (also known asØMQ, 0MQ, or ZMQ) looks a embeddable networking library but acts like a concurrency framework. It gives you sockets this carry atomic messages across various transports likeinch-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 is 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 the most operating systems.ZeroMQ is fromImatix and isLGPLv3 Open source.
Second, installation
Download zeromq-4.0.7.tar.gz from official website,
Tar zxvf zeromq-4.0. 7 . TAR.GZCD ZEROMQ-4.0. 7 . /configure--prefix=/usr/local/zeromqmakemake Install
[Email protected]:zeromq-4.0.7# ls/usr/local/zeromq/
Bin include Lib Share
Third, the use
The following is a simple echo server implemented with ZEROMQ:
ZMQSVR.C:
1#include"zmq.h"2#include <string.h>3#include <unistd.h>4 5 intMainvoid)6 {7 void* Zmq_ctx =zmq_ctx_new ();8 void* Zmq_sock =Zmq_socket (Zmq_ctx, zmq_rep);9Zmq_bind (Zmq_sock,"tcp://*:5555");Ten Oneprintf"start listen for socket...\n"); A intSeq =0; - while(1) - { theprintf"\nloop seq:%d\n", seq++); - intMsg_size =0; - Charbuf[Ten] = {0}; - zmq_msg_t request; +Zmq_msg_init (&request); -Zmq_msg_recv (&request, Zmq_sock,0); +Msg_size = Zmq_msg_size (&request); Amemcpy (buf, Zmq_msg_data (&request), msg_size); atprintf"recv Request:%s\n", buf); -Zmq_msg_close (&request); - -Sleep1); - - zmq_msg_t reply; inZmq_msg_init_size (&reply, msg_size); -memcpy (Zmq_msg_data (&reply), buf, msg_size); toprintf"Send reply:%s\n", buf); +Zmq_msg_send (&reply, Zmq_sock,0); -Zmq_msg_close (&reply); the } * $Sleep1);Panax Notoginseng Zmq_close (zmq_sock); - Zmq_ctx_destroy (ZMQ_CTX); the + return 0; A}
ZMQCLI.C:
1#include <zmq.h>2#include <stdio.h>3#include <unistd.h>4#include <string.h>5 6 intMainvoid)7 {8 void* Zmq_ctx =zmq_ctx_new ();9 void* Zmq_sock =Zmq_socket (Zmq_ctx, zmq_req);TenZmq_connect (Zmq_sock,"tcp://localhost:5555"); One A intindex =0; - for(index =0; Index <Ten; index++) - { theprintf"\nloop seq:%d\n", index); - zmq_msg_t request; -Zmq_msg_init_size (&request,6); - Charbuf[Ten] = {0}; +sprintf (BUF,"hello%d", index); -memcpy (Zmq_msg_data (&request), buf,6); +printf"Send request:%s\n", buf); AZmq_msg_send (&request, Zmq_sock,0); atZmq_msg_close (&request); - -memset (BUF,0,sizeof(BUF)); - zmq_msg_t reply; -Zmq_msg_init (&reply); -Zmq_msg_recv (&reply, Zmq_sock,0); inmemcpy (buf, Zmq_msg_data (&reply), Zmq_msg_size (&reply)); -printf"recv reply:%s\n", buf); toZmq_msg_close (&reply); + } - theSleep1); * Zmq_close (zmq_sock); $ Zmq_ctx_destroy (ZMQ_CTX);Panax Notoginseng - return 0; the}
Zmqsvr and ZMQCLI implement the sending of Helloi from the client side, and echo back to the client after the server receives it.
Makefile as follows:
1TARGET =zmqsvr zmqcli2 3 All : $ (TARGET)4 5CFLAGS =-g-i/usr/local/zeromq/include/-Wall6Ldflags =-l/usr/local/zeromq/lib/7 8 zmqsvr:zmqsvr.c9GCC $ (CFLAGS) $<-o [email protected] $ (ldflags)-LZMQTen One ZMQCLI:ZMQCLI.C AGCC $ (CFLAGS) $<-o [email protected] $ (ldflags)-LZMQ - - Clean : theRM-RF $ (TARGET)
[Email protected]: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
[email protected]:zeromq#./zmqcliloop seq:0send request:hello0recv reply:hello0loop seq: 1 send request:hello1recv reply:hello1...loop seq: 9 Send Request:hello9recv reply:hello9
In another terminal:
[email protected]:zeromq$./for09
(If you run with an error such as cannot found libzmq.so.4, you can use Strace./zmqsvr to view the path to the libzmq.so.4 of the call and copy libzmq.so.4 to that path.) )
ZEROMQ Installation and use