[zz]zeroMQ安装
边安装边写
1.下载
posix版:http://download.zeromq.org/zeromq-2.1.7.tar.gz
win版:http://download.zeromq.org/zeromq-2.1.7.zip
2.安装
- tar -zxvf zeromq-2.1.7.tar.gz
- cd zeromq-2.1.7
- ls
- ./configure --prefix=/data/zeromq
- make
- make install
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;
- }
测试结果:
客户端:
服务端
end