Networking Reference
In-Depth Information
The following is the server code, which sends the world message to clients:
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "zmq.h"
int main (int argc, char const *argv[]) {
void* context = zmq_ctx_new();
void* respond = zmq_socket(context, ZMQ_REP);
zmq_bind(respond, "tcp://*:4040");
printf("Starting…\n");
for(;;) {
zmq_msg_t request;
zmq_msg_init(&request);
zmq_msg_recv(&request, respond, 0);
printf("Received: hello\n");
zmq_msg_close(&request);
sleep(1); // sleep one second
zmq_msg_t reply;
zmq_msg_init_size(&reply, strlen("world"));
memcpy(zmq_msg_data(&reply), "world", 5);
zmq_msg_send(&reply, respond, 0);
zmq_msg_close(&reply);
}
zmq_close(respond);
zmq_ctx_destroy(context);
return 0;
}
Downloading the example code
You can download the example code iles for all Packt topics you
have purchased from your account at http://www.PacktPub.com .
If you purchased this topic elsewhere, you can visit
http://www.PacktPub.com/support and register to have the
files e-mailed directly to you.
 
Search MirCeyron ::




Custom Search