43 printf(
"send message to ZMQ recipient, to use with the zmq filters\n");
44 printf(
"usage: zmqsend [OPTIONS]\n");
47 "-b ADDRESS set bind address\n"
48 "-h print this help\n"
49 "-i INFILE set INFILE as input file, stdin if omitted\n");
52 int main(
int argc,
char **argv)
55 char *src_buf, *recv_buf;
57 int recv_buf_size,
ret = 0;
58 void *zmq_ctx, *socket;
59 const char *bind_address =
"tcp://localhost:5555";
60 const char *infilename =
NULL;
64 while ((
c =
getopt(argc, argv,
"b:hi:")) != -1) {
80 if (!infilename || !strcmp(infilename,
"-")) {
84 infile = fopen(infilename,
"r");
88 "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
92 zmq_ctx = zmq_ctx_new();
95 "Could not create ZMQ context: %s\n", zmq_strerror(errno));
99 socket = zmq_socket(zmq_ctx, ZMQ_REQ);
102 "Could not create ZMQ socket: %s\n", zmq_strerror(errno));
107 if (zmq_connect(socket, bind_address) == -1) {
109 bind_address, zmq_strerror(errno));
116 while ((
c = fgetc(infile)) != EOF)
128 if (zmq_send(socket, src_buf, strlen(src_buf), 0) == -1) {
134 if (zmq_msg_init(&msg) == -1) {
136 "Could not initialize receiving message: %s\n", zmq_strerror(errno));
141 if (zmq_msg_recv(&msg, socket, 0) == -1) {
143 "Could not receive message: %s\n", zmq_strerror(errno));
149 recv_buf_size = zmq_msg_size(&msg) + 1;
153 "Could not allocate receiving message buffer\n");
158 memcpy(recv_buf, zmq_msg_data(&msg), recv_buf_size);
159 recv_buf[recv_buf_size-1] = 0;
166 zmq_ctx_destroy(zmq_ctx);