-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from booksbyus/master
从booksbyus/zguide来源处更新代码
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |