libzmq master
The Intelligent Transport Layer

remote_thr.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #include "../include/zmq.h"
00023 #include "../include/zmq_utils.h"
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 int main (int argc, char *argv [])
00029 {
00030     const char *connect_to;
00031     int message_count;
00032     int message_size;
00033     void *ctx;
00034     void *s;
00035     int rc;
00036     int i;
00037     zmq_msg_t msg;
00038 
00039     if (argc != 4) {
00040         printf ("usage: remote_thr <connect-to> <message-size> "
00041             "<message-count>\n");
00042         return 1;
00043     }
00044     connect_to = argv [1];
00045     message_size = atoi (argv [2]);
00046     message_count = atoi (argv [3]);
00047 
00048     ctx = zmq_init (1);
00049     if (!ctx) {
00050         printf ("error in zmq_init: %s\n", zmq_strerror (errno));
00051         return -1;
00052     }
00053 
00054     s = zmq_socket (ctx, ZMQ_PUSH);
00055     if (!s) {
00056         printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
00057         return -1;
00058     }
00059 
00060     //  Add your socket options here.
00061     //  For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM.
00062 
00063     rc = zmq_connect (s, connect_to);
00064     if (rc != 0) {
00065         printf ("error in zmq_connect: %s\n", zmq_strerror (errno));
00066         return -1;
00067     }
00068 
00069     for (i = 0; i != message_count; i++) {
00070 
00071         rc = zmq_msg_init_size (&msg, message_size);
00072         if (rc != 0) {
00073             printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno));
00074             return -1;
00075         }
00076 #if defined ZMQ_MAKE_VALGRIND_HAPPY
00077         memset (zmq_msg_data (&msg), 0, message_size);
00078 #endif
00079 
00080         rc = zmq_sendmsg (s, &msg, 0);
00081         if (rc < 0) {
00082             printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
00083             return -1;
00084         }
00085         rc = zmq_msg_close (&msg);
00086         if (rc != 0) {
00087             printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
00088             return -1;
00089         }
00090     }
00091 
00092     rc = zmq_close (s);
00093     if (rc != 0) {
00094         printf ("error in zmq_close: %s\n", zmq_strerror (errno));
00095         return -1;
00096     }
00097 
00098     rc = zmq_term (ctx);
00099     if (rc != 0) {
00100         printf ("error in zmq_term: %s\n", zmq_strerror (errno));
00101         return -1;
00102     }
00103 
00104     return 0;
00105 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines