Created
July 29, 2024 15:19
-
-
Save Frenzie/4a7f5411173b3abbf0ac5ab2d442e366 to your computer and use it in GitHub Desktop.
Grabs data from a zmq socket and sends it to STDOUT
This file contains hidden or 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
// gcc -o zmq_to_pipe zmq_to_pipe.c -lzmq | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <zmq.h> | |
int main(int argc, char *argv[]) { | |
if (argc != 2) { | |
fprintf(stderr, "Usage: %s <zmq_socket_path>\n", argv[0]); | |
return 1; | |
} | |
void *context = zmq_ctx_new(); | |
void *socket = zmq_socket(context, ZMQ_SUB); | |
if (zmq_connect(socket, argv[1]) != 0) { | |
fprintf(stderr, "Failed to connect to ZMQ socket: %s\n", argv[1]); | |
zmq_ctx_destroy(context); | |
return 1; | |
} | |
zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "", 0); | |
while (1) { | |
zmq_msg_t message; | |
zmq_msg_init(&message); | |
int size = zmq_msg_recv(&message, socket, 0); | |
if (size == -1) { | |
fprintf(stderr, "Error receiving from ZMQ socket: %s\n", zmq_strerror(zmq_errno())); | |
zmq_msg_close(&message); | |
break; | |
} | |
fwrite(zmq_msg_data(&message), 1, size, stdout); | |
fflush(stdout); | |
zmq_msg_close(&message); | |
} | |
zmq_close(socket); | |
zmq_ctx_destroy(context); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment