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 c, *src_buf, *recv_buf;
56 int recv_buf_size,
ret;
58 const char *bind_address =
"tcp://localhost:5555";
59 const char *infilename = NULL;
63 while ((c =
getopt(argc, argv,
"b:hi:")) != -1) {
79 if (!infilename || !strcmp(infilename,
"-")) {
83 infile = fopen(infilename,
"r");
87 "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
94 "Could not create ZMQ context: %s\n", zmq_strerror(errno));
98 socket = zmq_socket(ctx, ZMQ_REQ);
101 "Could not create ZMQ socket: %s\n", zmq_strerror(errno));
106 if (zmq_connect(socket, bind_address) == -1) {
108 bind_address, zmq_strerror(errno));
115 while ((c = fgetc(infile)) != EOF)
127 if (zmq_send(socket, src_buf, strlen(src_buf), 0) == -1) {
133 if (zmq_msg_init(&msg) == -1) {
135 "Could not initialize receiving message: %s\n", zmq_strerror(errno));
140 if (zmq_msg_recv(&msg, socket, 0) == -1) {
142 "Could not receive message: %s\n", zmq_strerror(errno));
148 recv_buf_size = zmq_msg_size(&msg) + 1;
152 "Could not allocate receiving message buffer\n");
157 memcpy(recv_buf, zmq_msg_data(&msg), recv_buf_size);
158 recv_buf[recv_buf_size-1] = 0;
159 printf(
"%s\n", recv_buf);
165 zmq_ctx_destroy(ctx);