Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157460
  • 博文数量: 50
  • 博客积分: 83
  • 博客等级: 民兵
  • 技术积分: 297
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-12 11:47
文章分类
文章存档

2012年(43)

2011年(7)

分类:

2012-06-14 23:56:03

原文地址:zeromq安装手册 作者:yifangyou

边安装边写
1.下载
posix版:
win版:
2.安装
  1. tar -zxvf zeromq-2.1.7.tar.gz
  2. cd zeromq-2.1.7
  3. ls
  4. ./configure --prefix=/data/zeromq
  5. make
  6. make install

3.测试
  服务端测试代码,服务端服务器上的zmq是按照默认目录安装,因此和下面的client.c的编译不一样:
 
  1. /*gcc server.c -o server -lzmq
  2.   ./server
  3.   接收客户消息,并回复随机数字
  4. */
  5. #include <zmq.h>
  6.  #include <stdio.h>
  7.  #include <unistd.h>
  8.  #include <string.h>
  9.  #include <stdlib.h>
  10. int main (void)
  11.  {
  12.     void *context = zmq_init (1);

  13.     // Socket to talk to clients
  14.     void *responder = zmq_socket (context, ZMQ_REP);
  15.     //监听5555端口
  16.     zmq_bind (responder, "tcp://*:5555");
  17.     printf("binding on port 5555.\nwaiting client send message...\n");
  18.     while (1) {
  19.         // Wait for next request from client
  20.         zmq_msg_t request;
  21.         zmq_msg_init (&request);
  22.         //接收客户端消息
  23.         zmq_recv (responder, &request, 0);
  24.         int size = zmq_msg_size (&request);
  25.         
  26.         //打印客户端消息
  27.         char *string = malloc (size + 1);
  28.         memset(string,0,size+1);
  29.         memcpy (string, zmq_msg_data (&request), size);
  30.         printf ("Received Hello string=[%s]\n",string);
  31.         free(string);
  32.         zmq_msg_close (&request);

  33.         // Do some 'work'
  34.         sleep (1);

  35.         // Send reply back to client
  36.         zmq_msg_t reply;
  37.         char res[128]={0};
  38.         
  39.         //回复客户端
  40.         snprintf(res,127,"reply:%d",random());
  41.         zmq_msg_init_size (&reply, strlen(res));
  42.         memcpy (zmq_msg_data (&reply), res, strlen(res));
  43.         //发送
  44.         zmq_send (responder, &reply, 0);
  45.         zmq_msg_close (&reply);
  46.     }
  47.     // We never get here but if we did, this would be how we end
  48.     zmq_close (responder);
  49.     zmq_term (context);
  50.     return 0;
  51.  }
  客户端代码,若是server和客户端端不是同一台机器,需要把localhost改为server的ip:
 
  1. /* gcc client.c -o client -lzmq -L/data/zeromq/lib -I/data/zeromq/include
  2.    ./client "I am jack"
  3.    向服务器发送消息,并打印回复消息
  4. */
  5. #include <zmq.h>
  6.  #include <string.h>
  7.  #include <stdio.h>
  8.  #include <unistd.h>
  9.  #include <stdlib.h>
  10. int main (int argc,char**argv)
  11.  {
  12.          //参数1为要发送的消息
  13.       if(argc < 2){
  14.            printf("need send msg\n");
  15.            return 1;
  16.       }
  17.     void *context = zmq_init (1);

  18.     // Socket to talk to server
  19.     printf ("Connecting to hello world server...\n");
  20.     //连接server
  21.     void *requester = zmq_socket (context, ZMQ_REQ);
  22.     zmq_connect (requester, "tcp://localhost:5555");
  23.     
  24.     int request_nbr=0;
  25.     for (request_nbr = 0; request_nbr != 10; request_nbr++) {
  26.          //构建发送消息
  27.         zmq_msg_t request={0};
  28.         //申请消息的空间
  29.         zmq_msg_init_size (&request, strlen(argv[1]));
  30.         //填充消息
  31.         memcpy (zmq_msg_data (&request), argv[1], strlen(argv[1]));
  32.         printf ("Sending Hello %d\n", request_nbr);
  33.         //发送消息
  34.         zmq_send (requester, &request, 0);
  35.         zmq_msg_close (&request);

  36.         //准备接收回复
  37.         zmq_msg_t reply={0};
  38.         zmq_msg_init (&reply);
  39.         //接收回复
  40.         zmq_recv (requester, &reply, 0);
  41.         
  42.         //读取回复消息
  43.         int size = zmq_msg_size (&reply);
  44.         char *string = malloc (size + 1);
  45.         memset(string,0,size+1);
  46.         memcpy (string, zmq_msg_data (&reply), size);
  47.         printf ("recv from server string=[%s]\n",string);
  48.         free(string);
  49.         zmq_msg_close (&reply);
  50.     }
  51.     zmq_close (requester);
  52.     zmq_term (context);
  53.     return 0;
  54.  }
测试结果:
客户端:
 

服务端

end

阅读(2827) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~