centos安装编译时需要用的程序
yum install gcc
yum install gcc-c++
yum install make
yum install uuid-devel
yum install libuuid-devel
yum install python-devel # 安装python-zmq时需要
1.下载
posix版: http://download.zeromq.org/zeromq-2.2.0.tar.gz
win版: http://download.zeromq.org/zeromq-2.2.0.zip
2.安装
tar -zxvf zeromq-2.2.0.tar.gz
cd zeromq-2.2.0
./configure # 选择安装路径可以(./configure --prefix=/data/zeromq)
make
make install
ldconfig
# 安装完毕后, 需要手动做一下链接库的链接, 不然无法用pyzmq:
ln -s /usr/local/lib/libzmq.so.1 /usr/lib/libzmq.so.1 # 经尝试,可以省去这一步骤
# 然后安装pyzmq(可参考http://www.zeromq.org/bindings:python)
1) 手动下载后安装(https://github.com/zeromq/pyzmq/downloads/pyzmq-2.2.0.tar.gz)
2) 在线安装
apt-get install python-setuptools #安装easy_install
easy_install "pyzmq==2.2.0"
3.测试
服务端测试代码,服务端服务器上的zmq是按照默认目录安装,因此和下面的client.c的编译不一样:
/*
gcc server.c -o server -lzmq
./server
接收客户消息,并回复随机数字
*/
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
void *context = zmq_init (1);
// Socket to talk to clients
void *responder = zmq_socket (context, ZMQ_REP);
//监听5555端口
zmq_bind (responder, "tcp://*:5555");
printf("binding on port 5555.\nwaiting client send message...\n");
while (1) {
// Wait for next request from client
zmq_msg_t request;
zmq_msg_init (&request);
//接收客户端消息
zmq_recv (responder, &request, 0);
int size = zmq_msg_size (&request);
//打印客户端消息
char *string = malloc (size + 1);
memset(string,0,size+1);
memcpy (string, zmq_msg_data (&request), size);
printf ("Received Hello string=[%s]\n",string);
free(string);
zmq_msg_close (&request);
// Do some 'work'
sleep (1);
// Send reply back to client
zmq_msg_t reply;
char res[128]={0};
//回复客户端
snprintf(res,127,"reply:%d",random());
zmq_msg_init_size (&reply, strlen(res));
memcpy (zmq_msg_data (&reply), res, strlen(res));
//发送
zmq_send (responder, &reply, 0);
zmq_msg_close (&reply);
}
// We never get here but if we did, this would be how we end
zmq_close (responder);
zmq_term (context);
return 0;
}
客户端代码,若是server和客户端端不是同一台机器,需要把localhost改为server的ip:
/*
gcc client.c -o client -lzmq -L/data/zeromq/lib -I/data/zeromq/include
./client "I am jack"
向服务器发送消息,并打印回复消息
*/
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main (int argc,char**argv)
{
//参数1为要发送的消息
if(argc < 2){
printf("need send msg\n");
return 1;
}
void *context = zmq_init (1);
// Socket to talk to server
printf ("Connecting to hello world server...\n");
//连接server
void *requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://localhost:5555");
int request_nbr=0;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
//构建发送消息
zmq_msg_t request={0};
//申请消息的空间
zmq_msg_init_size (&request, strlen(argv[1]));
//填充消息
memcpy (zmq_msg_data (&request), argv[1], strlen(argv[1]));
printf ("Sending Hello %d\n", request_nbr);
//发送消息
zmq_send (requester, &request, 0);
zmq_msg_close (&request);
//准备接收回复
zmq_msg_t reply={0};
zmq_msg_init (&reply);
//接收回复
zmq_recv (requester, &reply, 0);
//读取回复消息
int size = zmq_msg_size (&reply);
char *string = malloc (size + 1);
memset(string,0,size+1);
memcpy (string, zmq_msg_data (&reply), size);
printf ("recv from server string=[%s]\n",string);
free(string);
zmq_msg_close (&reply);
}
zmq_close (requester);
zmq_term (context);
return 0;
}
测试结果:
客户端:
服务端
----------------------------------------------------------------------------------------------------
ubuntu、debian环境安装前
sudo apt-get install libtool, autoconf, automake, uuid-dev(or equivalent)
二、zeromq的安装
1、在http://www.zeromq.org/area:download页面下载最新的zeromq 2.1.7.
wget http://download.zeromq.org/zeromq-2.1.7.tar.gz
2、安装相关软件
$ sudo apt-get install libtool autoconf automake
$ sudo apt-get install uuid-dev g++
$ sudo apt-get install python-dev
3、编译安装zmq
$ ./configure
$ make
$ sudo make install
$ sudo ldconfig
4、pyzmq
在http://www.zeromq.org/bindings:python下载。
$ python setup.py build_ext --inplace
$ python setup.py test
$ sudo python setup.py install
5、测试
$ python -c "import zmq"
--------------------------------------------------------------------------------------------------
1.安装必要的包及开发环境
apt-get install build-essential
apt-get install uuid-dev #编译ØMQ时需要
apt-get install python-dev #安装pyzmq时需要
2.安装ØMQ
wget http://www.zeromq.org/local--files/area:download/zeromq-2.0.7.tar.gz
tar zvxf zeromq-2.0.7.tar.gz
cd zeromq-2.0.7
./configure --with-python
make
make install
sudo ldconfig
3.安装pyzmq
git clone git://github.com/ellisonbg/pyzmq.git
cd pyzmq
把setup.cfg.template改为setup.cfg,修改library_dirs 和include_dirs如下:
library_dirs = /usr/local/lib
include_dirs = /usr/local/include
使用"python setup.py install"安装pyzmq
--------------------------------------------------------------------------------------------------------------
python zmq参考
http://hi.baidu.com/freewindwu/blog/item/2c20f21ce7af24e6e1fe0b0f.html
http://nichol.as/zeromq-an-introduction
异步
https://github.com/zeromq/pyzmq/blob/master/examples/logger/zmqlogger.py