46     printf(
"send message to ZMQ recipient, to use with the zmq filters\n");
 
   47     printf(
"usage: zmqsend [OPTIONS]\n");
 
   50            "-b ADDRESS        set bind address\n" 
   51            "-h                print this help\n" 
   52            "-i INFILE         set INFILE as input file, stdin if omitted\n");
 
   55 int main(
int argc, 
char **argv)
 
   58     char *src_buf, *recv_buf;
 
   60     int recv_buf_size, 
ret = 0;
 
   61     void *zmq_ctx, *socket;
 
   62     const char *bind_address = 
"tcp://localhost:5555";
 
   63     const char *infilename = 
NULL;
 
   67     while ((
c = 
getopt(argc, argv, 
"b:hi:")) != -1) {
 
   83     if (!infilename || !strcmp(infilename, 
"-")) {
 
   87         infile = fopen(infilename, 
"r");
 
   91                "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
 
   95     zmq_ctx = zmq_ctx_new();
 
   98                "Could not create ZMQ context: %s\n", zmq_strerror(errno));
 
  102     socket = zmq_socket(zmq_ctx, ZMQ_REQ);
 
  105                "Could not create ZMQ socket: %s\n", zmq_strerror(errno));
 
  110     if (zmq_connect(socket, bind_address) == -1) {
 
  112                bind_address, zmq_strerror(errno));
 
  119     while ((
c = fgetc(infile)) != EOF)
 
  131     if (zmq_send(socket, src_buf, strlen(src_buf), 0) == -1) {
 
  137     if (zmq_msg_init(&msg) == -1) {
 
  139                "Could not initialize receiving message: %s\n", zmq_strerror(errno));
 
  144     if (zmq_msg_recv(&msg, socket, 0) == -1) {
 
  146                "Could not receive message: %s\n", zmq_strerror(errno));
 
  152     recv_buf_size = zmq_msg_size(&msg) + 1;
 
  156                "Could not allocate receiving message buffer\n");
 
  161     memcpy(recv_buf, zmq_msg_data(&msg), recv_buf_size - 1);
 
  162     recv_buf[recv_buf_size-1] = 0;
 
  169     zmq_ctx_destroy(zmq_ctx);