1、下载 zeromq对应语言的源码包
zeromq
2、上传并解压
$ tar xvzf zeromq-4.1.7.tar.gz
3、安装依赖包
sudo apt-get update && \
sudo apt-get install -y libtool pkg-config build-essential autoconf automake uuid-dev
4、构建makefile
cd zeromq-4.1.7/
./configure
5、安装
sudo make install
6、更新系统动态链接库并验证安装
sudo ldconfig
ldconfig -p | grep zmq
看到:
$ ldconfig -p | grep zmq
libzmq.so.5 (libc6,x86-64) => /usr/local/lib/libzmq.so.5
libzmq.so (libc6,x86-64) => /usr/local/lib/libzmq.so
就算安装成功了
官网测试代码,例如选择C语言(选择其他语言时,详细安装步骤参考INSTALL文件),则:
选择对应语言的测试用例:
服务端代码:
// Hello World sever
#include
#include
#include
#include
#include
int main (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
while (1) {
char buffer [10];
zmq_recv (responder, buffer, 10, 0);
printf ("Received Hello\n");
sleep (1); // Do some 'work'
zmq_send (responder, "World", 5, 0);
}
return 0;
}
客户端代码:
// Hello World client
#include
#include
#include
#include
int main (void)
{
printf ("Connecting to hello world server…\n");
void *context = zmq_ctx_new ();
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++) {
char buffer [10];
printf ("Sending Hello %d…\n", request_nbr);
zmq_send (requester, "Hello", 5, 0);
zmq_recv (requester, buffer, 10, 0);
printf ("Received World %d\n", request_nbr);
}
zmq_close (requester);
zmq_ctx_destroy (context);
return 0;
}
编译:
g++ server.cpp -o server -lzmq
g++ client.cpp -o client -lzmq
运行,服务端输出:
$ ./server
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
……
客户端输出:
$ ./client
Connecting to hello world server…
Sending Hello 0…
Received World 0
Sending Hello 1…
Received World 1
Sending Hello 2…
Received World 2
Sending Hello 3…
……
官网对应github包下载解压后,先用./autogen.sh 生成configure文件,然后执行./configure,然后make & make install 即可
https://gist.github.com/katopz/8b766a5cb0ca96c816658e9407e83d00