![]() |
libzmq master
The Intelligent Transport Layer
|
00001 /* 00002 Copyright (c) 2011 250bpm s.r.o. 00003 00004 This file is part of 0MQ. 00005 00006 0MQ is free software; you can redistribute it and/or modify it under 00007 the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 0MQ is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include <assert.h> 00021 #include <string.h> 00022 00023 #include "../include/zmq.h" 00024 00025 int main (int argc, char *argv []) 00026 { 00027 // Create the infrastructure 00028 void *ctx = zmq_init (0); 00029 assert (ctx); 00030 void *sb = zmq_socket (ctx, ZMQ_XREP); 00031 assert (sb); 00032 int rc = zmq_bind (sb, "inproc://a"); 00033 assert (rc == 0); 00034 void *sc = zmq_socket (ctx, ZMQ_XREQ); 00035 assert (sc); 00036 rc = zmq_connect (sc, "inproc://a"); 00037 assert (rc == 0); 00038 00039 // Send 2-part message. 00040 rc = zmq_send (sc, "A", 1, ZMQ_SNDMORE); 00041 assert (rc == 1); 00042 rc = zmq_send (sc, "B", 1, 0); 00043 assert (rc == 1); 00044 00045 // Identity comes first. 00046 zmq_msg_t msg; 00047 rc = zmq_msg_init (&msg); 00048 assert (rc == 0); 00049 rc = zmq_recvmsg (sb, &msg, 0); 00050 assert (rc >= 0); 00051 int more; 00052 size_t more_size = sizeof (more); 00053 rc = zmq_getmsgopt (&msg, ZMQ_MORE, &more, &more_size); 00054 assert (rc == 0); 00055 assert (more == 1); 00056 00057 // Then the first part of the message body. 00058 rc = zmq_recvmsg (sb, &msg, 0); 00059 assert (rc == 1); 00060 more_size = sizeof (more); 00061 rc = zmq_getmsgopt (&msg, ZMQ_MORE, &more, &more_size); 00062 assert (rc == 0); 00063 assert (more == 1); 00064 00065 // And finally, the second part of the message body. 00066 rc = zmq_recvmsg (sb, &msg, 0); 00067 assert (rc == 1); 00068 more_size = sizeof (more); 00069 rc = zmq_getmsgopt (&msg, ZMQ_MORE, &more, &more_size); 00070 assert (rc == 0); 00071 assert (more == 0); 00072 00073 // Deallocate the infrastructure. 00074 rc = zmq_close (sc); 00075 assert (rc == 0); 00076 rc = zmq_close (sb); 00077 assert (rc == 0); 00078 rc = zmq_term (ctx); 00079 assert (rc == 0); 00080 return 0 ; 00081 } 00082