Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <zmq.h>
- //const char *ADDRESS = "tcp://127.0.0.1:21000";
- const char *ADDRESS = "epgm://10.0.0.103;239.0.0.1:5000";
- #define HANDLE_ERROR(ret) (handle_error((ret), __FILE__, __LINE__))
- void handle_error(int ret, char *file, int line)
- {
- if (ret == 0)
- return;
- printf("ERROR (%s:%d) -- %s\n", file, line, strerror(errno));
- assert(0);
- }
- void run_client()
- {
- int ret;
- void *context = zmq_init(1);
- void *recvSocket = NULL;
- recvSocket = zmq_socket(context, ZMQ_SUB);
- assert(recvSocket);
- // Set a filter to receive all subscribed data.
- ret = zmq_setsockopt(recvSocket, ZMQ_SUBSCRIBE, "", 0);
- HANDLE_ERROR(ret);
- ret = zmq_connect(recvSocket, ADDRESS);
- HANDLE_ERROR(ret);
- while (1)
- {
- zmq_msg_t message;
- zmq_msg_init(&message);
- int size = zmq_recvmsg(recvSocket, &message, 0);
- char *string = zmq_msg_data(&message);
- printf("%s size %d\n", string, size);
- zmq_msg_close(&message);
- }
- zmq_close(recvSocket);
- zmq_term(context);
- }
- void run_server()
- {
- int ret;
- void *context = zmq_init(1);
- void *sendSocket = NULL;
- sendSocket = zmq_socket(context, ZMQ_PUB);
- assert(sendSocket);
- //ret = zmq_bind(sendSocket, ADDRESS);
- ret = zmq_connect(sendSocket, ADDRESS);
- HANDLE_ERROR(ret);
- while (1) {
- char buf[80] = "hello";
- zmq_msg_t message;
- zmq_msg_init_size(&message, strlen(buf));
- memcpy(zmq_msg_data(&message), buf, strlen(buf));
- ret = zmq_msg_send(&message, sendSocket, 0);
- if (ret != strlen(buf))
- {
- HANDLE_ERROR(-1);
- }
- zmq_msg_close(&message);
- }
- zmq_close(sendSocket);
- zmq_term(context);
- }
- int main(int argc, char *argv[])
- {
- printf("arg: %s\n", argv[1]);
- if (argc != 2) {
- printf("ERROR: 1 argument expected!\n");
- return -1;
- }
- if (strcmp(argv[1], "client") == 0) {
- printf("Running as client\n");
- run_client();
- }
- else {
- printf("Running as server\n");
- run_server();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement