Skip to content

Commit

Permalink
Merge pull request #1 from booksbyus/master
Browse files Browse the repository at this point in the history
从booksbyus/zguide来源处更新代码
  • Loading branch information
yunfan188 authored Mar 28, 2018
2 parents 10b9a5d + bafb600 commit c420c52
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
50 changes: 50 additions & 0 deletions examples/C/hwclient2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
**Hello World client
DEALER socket connect to tcp://localhost:5555
send "Hello" to server,expect receive "World"
*/
#include "zhelpers.h"
#include <unistd.h>

int main (void)
{
printf ("Connecting to hello world server...\n");
void *context = zmq_ctx_new ();
void *requester = zmq_socket (context, ZMQ_DEALER);
zmq_connect (requester, "tcp://localhost:5555");
int request_nbr; //request number
int reply_nbr = 0; //receive respond number
for (request_nbr = 0; request_nbr < 10; request_nbr++)
{
char buffer [10];
memset(buffer,0,sizeof(buffer));
printf ("Sending request msg: Hello NO=%d...\n", request_nbr+1);
//send request msg to server
s_sendmore(requester,""); //send multi part msg,the first part is empty part
zmq_send (requester, "Hello", 5, 0); //the second part is your request msg
//receive reply msg
int len;
len = zmq_recv (requester, buffer, 10, 0);
if(len == -1){
printf("Error:%s\n", zmq_strerror(errno));
exit(-1);
}
//if the first part you received is empty part,then continue receiving next part
if (strcmp(buffer,"") == 0){
memset(buffer,0,sizeof(buffer));
len = zmq_recv(requester, buffer, 10, 0);
if(len == -1){
printf("Error:%s\n", zmq_strerror(errno));
exit(-1);
}
printf("Received respond msg: %s NO=%d\n\n", buffer,++reply_nbr);
}
//if the first part you received is not empty part,discard the whole ZMQ msg
else{
printf("Discard the ZMQ message!\n",buffer);
}
}
zmq_close(requester);
zmq_ctx_destroy (context);
return 0;
}
26 changes: 26 additions & 0 deletions examples/C/hwserver2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
**Hello World server
REP socket bind to tcp://*:5555
expect receive"Hello",use "World" to responsd
*/

#include "zhelpers.h"
#include <unistd.h>

int main (void)
{
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
while (1)
{
char buffer [10]={0};
zmq_recv (responder, buffer, 10, 0);
printf("Received request msg: %s\n", buffer);
printf("Send respond msg: World\n\n");
zmq_send (responder, "World", 5, 0);
sleep (1); //Do some work
}
return 0;
}

0 comments on commit c420c52

Please sign in to comment.