ZEROMQ Learning notes 2--Simple client and server-side test programs

Source: Internet
Author: User

1. Preface

ZEROMQ provides guide,http://zguide.zeromq.org/to help beginners get started quickly, providing c\c++\php and many more languages.

2. Test procedure

Use the C language test program given by ZEROMQ to Hwserver and hwclient.

The Hwserver code is as follows:

1#include <stdio.h>2#include <unistd.h>3#include <string.h>4#include <assert.h>5#include <zmq.h>6                                                                                                        7 intMain (void)                                                                                        8 {                                                                                                      9     //Socket to talk to clientsTen     void*context =zmq_ctx_new ();  One     void*responder =zmq_socket (context, zmq_rep);  A     intrc = Zmq_bind (Responder,"tcp://*:5555");  -ASSERT (rc = =0);  -                                                                                                         the      while(1) {                                                                                         -         CharBuffer [Ten];  -ZMQ_RECV (responder, buffer,Ten,0);  -printf ("Received hello\n");  +Sleep (1);//Do some ' work ' -Zmq_send (Responder," World",5,0);  +     }                                                                                                   A     return 0;  at}

The Hwclient code is as follows:

1 //Hello World Client 2#include <zmq.h>3#include <string.h>4#include <stdio.h>5#include <unistd.h>6                                                                                                        7 intMain (void)                                                                                        8 {                                                                                                      9printf ("Connecting to Hello World server...\n"); Ten                                                                                                         One     /*to create a new context*/                                                                              A     void*context =zmq_ctx_new ();  -     void*requester =zmq_socket (context, zmq_req);  -     /*connect to the local server via TCP protocol, port 5555*/                                                           theZmq_connect (Requester,"tcp://localhost:5555");  -                                                                                                         -     intREQUEST_NBR;  -      for(REQUEST_NBR =0; Request_nbr! =Ten; request_nbr++) {                                           +         CharBuffer [Ten];  -printf ("Sending Hello%d...\n", REQUEST_NBR);  +Zmq_send (Requester,"Hello",5,0);  AZMQ_RECV (requester, buffer,Ten,0);  atprintf ("Received World%d\n", REQUEST_NBR);  -     }                                                                                                   -                                                                                                         - Zmq_close (requester);  - Zmq_ctx_destroy (context);  -                                                                                                         in     return 0;  -}

3. Compile and Execute

I installed the ZEROMQ in the/USR/LOCAL/ZEROMQ directory, makefile set up the include and Lib connection, the code passed normally, but at the time of execution prompt:

[Email protected] hwserver]#./hwserver
./hwserver:error while loading shared libraries:libzmq.so.5:cannot open Shared object file:no such file or directory

After checking the Internet, we found that Zeromq's Lib path was not added to ld.so.conf.

Reference: http://blog.csdn.net/guoyilongedu/article/details/17450815

Add the libzeromq.so path to the Tao Ld.so.conf, the following steps:

Go to etc under cd/etc

Edit ld.so.conf, sudo vim ld.so.conf

Join Libzmq.so's Path/usr/local/zeromq/lib

Save and then execute the command ldconfig

After execution completes, the compilation succeeds:

Starting Hwserver and Hwclient, the execution results are as follows:

ZEROMQ Learning notes 2--Simple client and server-side test programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.