本文整理汇总了C++中zmq_connect函数的典型用法代码示例。如果您正苦于以下问题:C++ zmq_connect函数的具体用法?C++ zmq_connect怎么用?C++ zmq_connect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zmq_connect函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(void) {
// Socked to talk to clients
printf("Connecting to the server\n");
void *context = zmq_ctx_new();
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_connect(requester, "tcp://localhost:5555");
int request_nbr;
for (request_nbr = 0; request_nbr < 10; request_nbr++) {
char buffer[10];
printf("Sending Hello %d...\n", request_nbr);
zmq_send(requester, "Hello", 5, 0);
zmq_recv(requester, buffer, 10, 0);
printf("Received World %d\n", request_nbr);
}
zmq_close(requester);
zmq_ctx_destroy(context);
return 0;
}
示例2: main
int main()
{
void *context = zmq_ctx_new();
// Socket to talk to server
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_connect(requester, "tcp://localhost:5559");
int request_nbr;
for (request_nbr = 0; request_nbr != 10; ++request_nbr)
{
s_send(requester, "Hello");
char *string = s_recv(requester);
printf("Received reply %d [%s]\n", request_nbr, string);
free(string);
}
zmq_close(requester);
zmq_ctx_destroy(context);
return 0;
}
示例3: main
int main (void)
{
void *context = zmq_ctx_new ();
// Socket to send messages on
void *sender = zmq_socket (context, ZMQ_PUSH);
zmq_bind (sender, "tcp://*:5557");
// Socket to send start of batch message on
void *sink = zmq_socket (context, ZMQ_PUSH);
zmq_connect (sink, "tcp://localhost:5558");
printf ("Press Enter when the workers are ready: ");
getchar ();
printf ("Sending tasks to workers…\n");
// The first message is "0" and signals start of batch
s_send (sink, "0");
// Initialize random number generator
srandom ((unsigned) time (NULL));
// Send 100 tasks
int task_nbr;
int total_msec = 0; // Total expected cost in msecs
for (task_nbr = 0; task_nbr < 100; task_nbr++) {
int workload;
// Random workload from 1 to 100msecs
workload = randof (100) + 1;
total_msec += workload;
char string [10];
sprintf (string, "%d", workload);
s_send (sender, string);
}
printf ("Total expected cost: %d msec\n", total_msec);
zmq_close (sink);
zmq_close (sender);
zmq_ctx_destroy (context);
return 0;
}
示例4: test_blocking
int test_blocking (int send_hwm_, int msg_cnt_)
{
// Set up bind socket
void *pub_socket = test_context_socket (ZMQ_PUB);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pub_socket, "inproc://a"));
// Set up connect socket
void *sub_socket = test_context_socket (ZMQ_SUB);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub_socket, "inproc://a"));
//set a hwm on publisher
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (pub_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
int wait = 1;
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (pub_socket, ZMQ_XPUB_NODROP, &wait, sizeof (wait)));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, 0, 0));
// Send until we block
int send_count = 0;
int recv_count = 0;
while (send_count < msg_cnt_) {
const int rc = zmq_send (pub_socket, NULL, 0, ZMQ_DONTWAIT);
if (rc == 0) {
++send_count;
} else if (-1 == rc) {
TEST_ASSERT_EQUAL_INT (EAGAIN, errno);
recv_count += receive (sub_socket);
TEST_ASSERT_EQUAL_INT (send_count, recv_count);
}
}
recv_count += receive (sub_socket);
// Clean up
test_context_socket_close (sub_socket);
test_context_socket_close (pub_socket);
return recv_count;
}
示例5: test_basic
void test_basic ()
{
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *router = test_context_socket (ZMQ_ROUTER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, "tcp://127.0.0.1:*"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
// Send a message to an unknown peer with the default setting
// This will not report any error
send_string_expect_success (router, "UNKNOWN", ZMQ_SNDMORE);
send_string_expect_success (router, "DATA", 0);
// Send a message to an unknown peer with mandatory routing
// This will fail
int mandatory = 1;
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
&mandatory, sizeof (mandatory)));
int rc = zmq_send (router, "UNKNOWN", 7, ZMQ_SNDMORE);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
// Create dealer called "X" and connect it to our router
void *dealer = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
// Get message from dealer to know when connection is ready
send_string_expect_success (dealer, "Hello", 0);
recv_string_expect_success (router, "X", 0);
// Send a message to connected dealer now
// It should work
send_string_expect_success (router, "X", ZMQ_SNDMORE);
send_string_expect_success (router, "Hello", 0);
test_context_socket_close (router);
test_context_socket_close (dealer);
}
示例6: main
int main (void)
{
srandom ((unsigned) time (NULL));
void *context = zmq_init (1);
void *worker = zmq_socket (context, ZMQ_REQ);
// Set random identity to make tracing easier
char identity [10];
sprintf (identity, "%04X-%04X", randof (0x10000), randof (0x10000));
zmq_setsockopt (worker, ZMQ_IDENTITY, identity, strlen (identity));
zmq_connect (worker, "tcp://localhost:5556");
// Tell queue we're ready for work
printf ("I: (%s) worker ready\n", identity);
s_send (worker, "READY");
int cycles = 0;
while (1) {
zmsg_t *zmsg = zmsg_recv (worker);
// Simulate various problems, after a few cycles
cycles++;
if (cycles > 3 && randof (5) == 0) {
printf ("I: (%s) simulating a crash\n", identity);
zmsg_destroy (&zmsg);
break;
}
else
if (cycles > 3 && randof (5) == 0) {
printf ("I: (%s) simulating CPU overload\n", identity);
sleep (5);
}
printf ("I: (%s) normal reply - %s\n", identity, zmsg_body (zmsg));
sleep (1); // Do some heavy work
zmsg_send (&zmsg, worker);
}
zmq_close (worker);
zmq_term (context);
return 0;
}
示例7: main
int main ()
{
void *ctx = zmq_init (1);
assert (ctx);
void *pub = zmq_socket (ctx, ZMQ_PUB);
assert (pub);
int rc = zmq_bind (pub, "tcp://*:2211");
assert (rc == 0);
void *sub = zmq_socket (ctx, ZMQ_SUB);
assert (sub);
uint64_t size = 1;
rc = zmq_setsockopt (sub, ZMQ_HWM, &size, sizeof (size));
assert (rc == 0);
int64_t swap = 10000000;
rc = zmq_setsockopt (sub, ZMQ_SWAP, &swap, sizeof (swap));
assert (rc == 0);
rc = zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0);
assert (rc == 0);
rc = zmq_connect (sub, "tcp://127.0.0.1:2211");
assert (rc == 0);
int count;
for (count = 0; count < 100000; count++) {
zmq_msg_t msg;
zmq_msg_init_size (&msg, 0);
zmq_send (pub, &msg, 0);
zmq_msg_close (&msg);
}
int linger = 0;
zmq_setsockopt (pub, ZMQ_LINGER, &linger, sizeof (int));
zmq_setsockopt (sub, ZMQ_LINGER, &linger, sizeof (int));
zmq_close (pub);
zmq_close (sub);
zmq_term (ctx);
return 0;
}
示例8: test_round_robin_out
void test_round_robin_out (void *ctx)
{
void *req = zmq_socket (ctx, ZMQ_REQ);
assert (req);
int rc = zmq_bind (req, bind_address);
assert (rc == 0);
const size_t services = 5;
void *rep [services];
for (size_t peer = 0; peer < services; peer++) {
rep [peer] = zmq_socket (ctx, ZMQ_REP);
assert (rep [peer]);
int timeout = 250;
rc = zmq_setsockopt (rep [peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
assert (rc == 0);
rc = zmq_connect (rep [peer], connect_address);
assert (rc == 0);
}
// We have to give the connects time to finish otherwise the requests
// will not properly round-robin. We could alternatively connect the
// REQ sockets to the REP sockets.
msleep (SETTLE_TIME);
// Send our peer-replies, and expect every REP it used once in order
for (size_t peer = 0; peer < services; peer++) {
s_send_seq (req, "ABC", SEQ_END);
s_recv_seq (rep [peer], "ABC", SEQ_END);
s_send_seq (rep [peer], "DEF", SEQ_END);
s_recv_seq (req, "DEF", SEQ_END);
}
close_zero_linger (req);
for (size_t peer = 0; peer < services; peer++)
close_zero_linger (rep [peer]);
// Wait for disconnects.
msleep (SETTLE_TIME);
}
示例9: test_pair_tcp
void test_pair_tcp (extra_func_t extra_func_ = NULL)
{
void *sb = test_context_socket (ZMQ_PAIR);
if (extra_func_)
extra_func_ (sb);
char my_endpoint[MAX_SOCKET_STRING];
bind_loopback_ipv4 (sb, my_endpoint, sizeof my_endpoint);
void *sc = test_context_socket (ZMQ_PAIR);
if (extra_func_)
extra_func_ (sc);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint));
bounce (sb, sc);
test_context_socket_close (sc);
test_context_socket_close (sb);
}
示例10: main
int main (void)
{
void *context = zmq_init (1);
// Socket to talk to server
printf ("Connecting to test_rep server... \n");
void *requester = zmq_socket (context, ZMQ_SUB);
zmq_connect (requester, "tcp://127.0.0.1:5555");
zmq_setsockopt(requester, ZMQ_SUBSCRIBE, "", 0);
while(1) {
zmq_msg_t reply;
zmq_msg_init (&reply);
zmq_recv (requester, &reply, 0);
printf ("Received %s \n", zmq_msg_data(&reply) );
zmq_msg_close (&reply);
}
zmq_close (requester);
zmq_term (context);
return 0;
}
示例11: zmq_socket
static void *step2 (void *context)
{
// Bind inproc socket before starting step1
void *receiver = zmq_socket (context, ZMQ_PAIR);
zmq_bind (receiver, "inproc://step2");
pthread_t thread;
pthread_create (&thread, NULL, step1, context);
// Wait for signal and pass it on
char *string = s_recv (receiver);
free (string);
zmq_close (receiver);
// Connect to step3 and tel it we're ready
void *xmitter = zmq_socket (context, ZMQ_PAIR);
zmq_connect (xmitter, "inproc://step3");
s_send (xmitter, "READY");
zmq_close (xmitter);
return NULL;
}
示例12: main
int main()
{
void *context = zmq_ctx_new();
void *subscriber = zmq_socket(context, ZMQ_SUB);
zmq_connect(subscriber, "tcp://localhost:5563");
zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "B", 1);
while (1) {
// Read envelope with address
char *address = s_recv(subscriber);
// Read message contents
char *contents = s_recv(subscriber);
printf("[%s] %s\n", address, contents);
free(address);
free(contents);
}
zmq_close(subscriber);
zmq_ctx_destroy(context);
return 0;
}
示例13: main
int main()
{
printf("Connecting to hello world server...\n");
void *context = zmq_ctx_new();
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_connect(requester, "tcp://localhost:5555");
int request_nbr;
for (request_nbr = 0; request_nbr != 10; ++request_nbr)
{
char buffer[10];
printf("Sending Hello %d...\n", request_nbr);
zmq_send(requester, "Hello", 5, 0);
zmq_recv(requester, buffer, 10, 0);
printf("Received World %d\n", request_nbr);
}
zmq_close(requester);
zmq_ctx_destroy(context);
return 0;
}
示例14: main
int main(int argc, char *argv []) {
void *context, *socket;
char *topic, *string;
int64_t more;
if (argc < 2) {
fprintf(stderr, "USAGE: zmq_echo <address> [topic]\n");
return 1;
}
context = zmq_init(1);
printf("connecting to %s...\n", argv[1]);
socket = zmq_socket(context, ZMQ_SUB);
zmq_connect(socket, argv[1]);
if (argc > 2) {
topic = argv[2];
printf("subscribing to \"%s\" topic...\n", topic);
} else {
topic = "";
printf("subscribing to all topics...\n");
}
zmq_setsockopt(socket, ZMQ_SUBSCRIBE, topic, strlen(topic));
printf(">> ");
fflush(stdout);
while (1) {
string = zmq_recv_string(socket, &more);
printf(more ? "%s" : "%s\n>> ", string);
fflush(stdout);
free(string);
}
zmq_close(socket);
zmq_term(context);
return 0;
}
示例15: test_heartbeat_notimeout
// This checks for normal operation - that is pings and pongs being
// exchanged normally. There should be an accepted event on the server,
// and then no event afterwards.
static void
test_heartbeat_notimeout (int is_curve)
{
int rc;
// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);
void * server, * server_mon;
prep_server_socket(ctx, 1, is_curve, &server, &server_mon);
void * client = zmq_socket(ctx, ZMQ_DEALER);
if(is_curve)
setup_curve(client, 0);
rc = zmq_connect(client, "tcp://127.0.0.1:5556");
// Give it a sec to connect and handshake
msleep(100);
// By now everything should report as connected
rc = get_monitor_event(server_mon);
assert(rc == ZMQ_EVENT_ACCEPTED);
// We should still be connected because pings and pongs are happenin'
rc = get_monitor_event(server_mon);
assert(rc == -1);
rc = zmq_close (client);
assert (rc == 0);
rc = zmq_close (server);
assert (rc == 0);
rc = zmq_close (server_mon);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
}