1.
posix版(linux下载这个):
win版:
2.安装
tar -zxvf zeromq-2.1.7.tar.gz
cd zeromq-2.1.7
ls
./configure
ps:configure会报个错,貌似是缺少什么库,按提示安装上即可。
make
make install
3.测试程序:
测试客户端代码://zeromqclient.c
- #include <zmq.h>
- #include <string.h>
- #include <stdio.h>
- #include <unistd.h>
- int main ()
- {
- void *context = zmq_init (1); // 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, NULL, NULL);
- printf ("Sending request %d...\n", request_nbr);
- zmq_send (requester, &request, 0);
- zmq_msg_close (&request);
- zmq_msg_t reply;
- zmq_msg_init (&reply);
- zmq_recv (requester, &reply, 0);
- printf ("Received reply %d: [%s]\n", request_nbr, (char *) zmq_msg_data (&reply));
- zmq_msg_close (&reply);
- }
- zmq_close (requester);
- zmq_term (context);
- return 0;
- }
客户端编译:
# gcc zeromqclient.c -o client -lzmq -L/data/zeromq/lib -I/data/zeromq/include
报错:
error while loading shared libraries: libzmq.so.0: cannot open shared object file: No such file or directory
解决方法:
执行:sudo /sbin/ldconfig,因为ldconfig为动态链接库管理命令,一般在系统启动时运行,当安装了新的动态连接库后,需要手动执行此命令。
测试服务器代码://zeromqserver.c
- #include <zmq.hpp>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- int main ()
- {
- // Prepare our context and socket
- zmq::context_t context (1);
- zmq::socket_t socket (context, ZMQ_REP);
- socket.bind ("tcp://*:5555");
- while (true)
- {
- zmq::message_t request; // Wait for next request from client
- socket.recv (&request);
- printf ("Received request: [%s]\n", (char *) request.data ()); // Do some 'work'
- sleep (1); // Send reply back to client
- zmq::message_t reply (6);
- memcpy ((void *) reply.data (), "World", 6);
- socket.send (reply);
- }
- return 0;
- }
服务器编译:
#g++ zeromqserver.c -o server -lzmq -L/data/zeromq/lib -I/data/zeromq/include
ps:如果用gcc就会报错一大堆,但是g++却不会。本人也不知道原因。
运行:
服务器先运行:#./server
客户端运行:#./client "i am xiaocui"
--------------------------------------------------------------------------------------
python:
1. 安装python开发环境。
sudo apt-get install python-dev
2. 安装easy_install工具,该工具主要是方便python的一些第三方库或包的安装.
sudo apt-get install python_setuptools
3. 安装zeromq的python绑定。
sudo easy_install pyzmq
4. 至此,可以开始使用python编写基于zeromq的网络程序了。enjoy it.
阅读(2977) | 评论(0) | 转发(0) |