本文整理汇总了C++中zsock_connect函数的典型用法代码示例。如果您正苦于以下问题:C++ zsock_connect函数的具体用法?C++ zsock_connect怎么用?C++ zsock_connect使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zsock_connect函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: server_connect
static void
server_connect (server_t *self, const char *endpoint)
{
zsock_t *remote = zsock_new (ZMQ_DEALER);
assert (remote); // No recovery if exhausted
// Never block on sending; we use an infinite HWM and buffer as many
// messages as needed in outgoing pipes. Note that the maximum number
// is the overall tuple set size.
zsock_set_sndhwm (remote, 0);
if (zsock_connect (remote, "%s", endpoint)) {
zsys_error ("bad zgossip endpoint '%s'", endpoint);
zsock_destroy (&remote);
return;
}
// Send HELLO and then PUBLISH for each tuple we have
zgossip_msg_send_hello (remote);
tuple_t *tuple = (tuple_t *) zhash_first (self->tuples);
while (tuple) {
int rc = zgossip_msg_send_publish (remote, tuple->key, tuple->value, 0);
assert (rc == 0);
tuple = (tuple_t *) zhash_next (self->tuples);
}
// Now monitor this remote for incoming messages
engine_handle_socket (self, remote, remote_handler);
zlist_append (self->remotes, remote);
}
示例2: mdp_broker_test
void
mdp_broker_test (bool verbose)
{
printf (" * mdp_broker: ");
if (verbose)
printf ("\n");
// @selftest
zactor_t *server = zactor_new (mdp_broker, "server");
if (verbose)
zstr_send (server, "VERBOSE");
zstr_sendx (server, "BIND", "ipc://@/mdp_broker", NULL);
zsock_t *client = zsock_new (ZMQ_DEALER);
assert (client);
zsock_set_rcvtimeo (client, 2000);
zsock_connect (client, "ipc://@/mdp_broker");
// TODO: fill this out
mdp_msg_t *request = mdp_msg_new ();
mdp_msg_destroy (&request);
zsock_destroy (&client);
zactor_destroy (&server);
// @end
printf ("OK\n");
}
示例3: s_can_connect
// Checks whether client can connect to server
static bool
s_can_connect (zsock_t **server, zsock_t **client)
{
int port_nbr = zsock_bind (*server, "tcp://127.0.0.1:*");
assert (port_nbr > 0);
int rc = zsock_connect (*client, "tcp://127.0.0.1:%d", port_nbr);
assert (rc == 0);
// Give the connection time to fail if that's the plan
zclock_sleep (200);
// By default PUSH sockets block if there's no peer
zsock_set_sndtimeo (*server, 200);
zstr_send (*server, "Hello, World");
zpoller_t *poller = zpoller_new (*client, NULL);
assert (poller);
bool success = (zpoller_wait (poller, 400) == *client);
zpoller_destroy (&poller);
zsock_destroy (client);
zsock_destroy (server);
*server = zsock_new (ZMQ_PUSH);
assert (*server);
*client = zsock_new (ZMQ_PULL);
assert (*client);
return success;
}
示例4: main
int main (int argc, char** argv) {
zsock_t *client = zsock_new (ZMQ_DEALER);
int i;
for (i = 0; i < 255; i++)
{
int port = 5556;
// printf ("Seeking to server at '%s.%d:%d'\n", argv [1], i, port);
zsock_connect (client, "tcp://%s.%d:%d", argv [1], i, port);
}
zsock_set_rcvtimeo (client, 2000);
while ( 1 )
{
char *reply = zstr_recv (client);
if (reply) {
puts (reply);
free (reply);
}
else
{
puts ("-");
break;
}
}
zsock_destroy (&client);
return 0;
}
示例5: zpipes_client_new
zpipes_client_t *
zpipes_client_new (const char *server_name, const char *pipe_name)
{
// Create new pipe API instance
zpipes_client_t *self = (zpipes_client_t *) zmalloc (sizeof (zpipes_client_t));
assert (self);
// Create dealer socket and connect to server IPC port
self->dealer = zsock_new (ZMQ_DEALER);
assert (self->dealer);
int rc = zsock_connect (self->dealer, "ipc://@/zpipes/%s", server_name);
assert (rc == 0);
// Open pipe for reading or writing
if (*pipe_name == '>') {
zpipes_msg_send_output (self->dealer, pipe_name + 1);
if (s_expect_reply (self, ZPIPES_MSG_OUTPUT_OK))
zpipes_client_destroy (&self);
}
else {
zpipes_msg_send_input (self->dealer, pipe_name);
if (s_expect_reply (self, ZPIPES_MSG_INPUT_OK))
zpipes_client_destroy (&self);
}
return self;
}
示例6: server_connect
static void
server_connect (server_t *self, const char *endpoint)
{
zsock_t *remote = zsock_new (ZMQ_DEALER);
assert (remote); // No recovery if exhausted
// Never block on sending; we use an infinite HWM and buffer as many
// messages as needed in outgoing pipes. Note that the maximum number
// is the overall tuple set size.
zsock_set_unbounded (remote);
if (zsock_connect (remote, "%s", endpoint)) {
zsys_warning ("bad zgossip endpoint '%s'", endpoint);
zsock_destroy (&remote);
return;
}
// Send HELLO and then PUBLISH for each tuple we have
zgossip_msg_t *gossip = zgossip_msg_new ();
zgossip_msg_set_id (gossip, ZGOSSIP_MSG_HELLO);
zgossip_msg_send (gossip, remote);
tuple_t *tuple = (tuple_t *) zhashx_first (self->tuples);
while (tuple) {
zgossip_msg_set_id (gossip, ZGOSSIP_MSG_PUBLISH);
zgossip_msg_set_key (gossip, tuple->key);
zgossip_msg_set_value (gossip, tuple->value);
zgossip_msg_send (gossip, remote);
tuple = (tuple_t *) zhashx_next (self->tuples);
}
// Now monitor this remote for incoming messages
zgossip_msg_destroy (&gossip);
engine_handle_socket (self, remote, remote_handler);
zlistx_add_end (self->remotes, remote);
}
示例7: zactor_new
zactor_t *
zactor_new (zactor_fn *actor, void *args)
{
zactor_t *self = (zactor_t *) zmalloc (sizeof (zactor_t));
if (!self)
return NULL;
self->tag = ZACTOR_TAG;
shim_t *shim = (shim_t *) zmalloc (sizeof (shim_t));
if (!shim)
return NULL;
// Create front-to-back pipe pair
self->pipe = zsock_new (ZMQ_PAIR);
assert (self->pipe);
char endpoint [32];
while (true) {
sprintf (endpoint, "inproc://zactor-%04x-%04x\n",
randof (0x10000), randof (0x10000));
if (zsock_bind (self->pipe, "%s", endpoint) == 0)
break;
}
shim->pipe = zsock_new (ZMQ_PAIR);
assert (shim->pipe);
int rc = zsock_connect (shim->pipe, "%s", endpoint);
assert (rc != -1);
shim->handler = actor;
shim->args = args;
#if defined (__UNIX__)
pthread_t thread;
pthread_create (&thread, NULL, s_thread_shim, shim);
pthread_detach (thread);
#elif defined (__WINDOWS__)
HANDLE handle = (HANDLE) _beginthreadex (
NULL, // Handle is private to this process
0, // Use a default stack size for new thread
&s_thread_shim, // Start real thread function via this shim
shim, // Which gets arguments shim
CREATE_SUSPENDED, // Set thread priority before starting it
NULL); // We don't use the thread ID
assert (handle);
// Set child thread priority to same as current
int priority = GetThreadPriority (GetCurrentThread ());
SetThreadPriority (handle, priority);
// Start thread & release resources
ResumeThread (handle);
CloseHandle (handle);
#endif
// Mandatory handshake for new actor so that constructor returns only
// when actor has also initialized. This eliminates timing issues at
// application start up.
zsock_wait (self->pipe);
return self;
}
示例8: Java_org_zeromq_czmq_Zsock__1_1connect
JNIEXPORT jint JNICALL
Java_org_zeromq_czmq_Zsock__1_1connect (JNIEnv *env, jclass c, jlong self, jstring format)
{
char *format_ = (char *) (*env)->GetStringUTFChars (env, format, NULL);
jint connect_ = (jint) zsock_connect ((zsock_t *) (intptr_t) self, "%s", format_);
(*env)->ReleaseStringUTFChars (env, format, format_);
return connect_;
}
示例9: connect_to_server_endpoint
static void
connect_to_server_endpoint (client_t *self)
{
if (zsock_connect (self->dealer, "%s", self->args->endpoint)) {
engine_set_exception (self, bad_endpoint_event);
zsys_warning ("could not connect to %s", self->args->endpoint);
}
}
示例10: subscriber_sub_socket_new
static
zsock_t* subscriber_sub_socket_new(subscriber_state_t *state)
{
zsock_t *socket = zsock_new(ZMQ_SUB);
assert(socket);
zsock_set_rcvhwm(socket, state->rcv_hwm);
// set subscription
if (!state->subscriptions || zlist_size(state->subscriptions) == 0) {
if (!state->subscriptions)
state->subscriptions = zlist_new();
zlist_append(state->subscriptions, zconfig_resolve(state->config, "/logjam/subscription", ""));
}
char *subscription = zlist_first(state->subscriptions);
bool subscribed_to_all = false;
while (subscription) {
printf("[I] subscriber: subscribing to '%s'\n", subscription);
if (streq(subscription, ""))
subscribed_to_all = true;
zsock_set_subscribe(socket, subscription);
subscription = zlist_next(state->subscriptions);
}
if (!subscribed_to_all)
zsock_set_subscribe(socket, "heartbeat");
if (!state->devices || zlist_size(state->devices) == 0) {
// convert config file to list of devices
if (!state->devices)
state->devices = zlist_new();
zconfig_t *endpoints = zconfig_locate(state->config, "/logjam/endpoints");
if (!endpoints) {
zlist_append(state->devices, "tcp://localhost:9606");
} else {
zconfig_t *endpoint = zconfig_child(endpoints);
while (endpoint) {
char *spec = zconfig_value(endpoint);
char *new_spec = augment_zmq_connection_spec(spec, 9606);
zlist_append(state->devices, new_spec);
endpoint = zconfig_next(endpoint);
}
}
}
char* device = zlist_first(state->devices);
while (device) {
printf("[I] subscriber: connecting SUB socket to logjam-device via %s\n", device);
int rc = zsock_connect(socket, "%s", device);
log_zmq_error(rc, __FILE__, __LINE__);
assert(rc == 0);
device = zlist_next(state->devices);
}
return socket;
}
示例11: zmailer_msg_test
void
zmailer_msg_test (bool verbose)
{
printf (" * zmailer_msg:");
if (verbose)
printf ("\n");
// @selftest
// Simple create/destroy test
zmailer_msg_t *self = zmailer_msg_new ();
assert (self);
zmailer_msg_destroy (&self);
// Create pair of sockets we can send through
// We must bind before connect if we wish to remain compatible with ZeroMQ < v4
zsock_t *output = zsock_new (ZMQ_DEALER);
assert (output);
int rc = zsock_bind (output, "inproc://selftest-zmailer_msg");
assert (rc == 0);
zsock_t *input = zsock_new (ZMQ_ROUTER);
assert (input);
rc = zsock_connect (input, "inproc://selftest-zmailer_msg");
assert (rc == 0);
// Encode/send/decode and verify each message type
int instance;
self = zmailer_msg_new ();
zmailer_msg_set_id (self, ZMAILER_MSG_MAIL);
zmailer_msg_set_from (self, "Life is short but Now lasts for ever");
zmailer_msg_set_to (self, "Life is short but Now lasts for ever");
zmailer_msg_set_subject (self, " test subject ");
zmailer_msg_set_request (self, " this is the text to be sent ");
// Send twice
zmailer_msg_send (self, output);
zmailer_msg_send (self, output);
for (instance = 0; instance < 2; instance++) {
zmailer_msg_recv (self, input);
assert (zmailer_msg_routing_id (self));
assert (streq (zmailer_msg_from (self), "Life is short but Now lasts for ever"));
assert (streq (zmailer_msg_to (self), "Life is short but Now lasts for ever"));
assert (streq (zmailer_msg_subject (self), " test subject "));
assert (streq (zmailer_msg_request (self), " this is the text to be sent "));
}
zmailer_msg_destroy (&self);
zsock_destroy (&input);
zsock_destroy (&output);
// @end
printf ("OK\n");
}
示例12: s_mdp_client_connect_to_broker
void s_mdp_client_connect_to_broker (mdp_client_t *self)
{
if(self->client)
zsock_destroy (&self->client);
self->client = zsock_new (ZMQ_DEALER);
assert(0==zsock_connect (self->client, "%s", self->broker));
if (self->verbose)
zclock_log ("I: connecting to broker at %s...", self->broker);
zsock_set_rcvtimeo(self->client,self->timeout);
}
示例13: main
int main(int argc, char const * const *argv)
{
int rc;
zsys_set_sndhwm(1);
zsys_set_linger(100);
void *pusher = zsock_new(ZMQ_PUSH);
assert(pusher);
zsock_set_sndhwm(pusher, 1000);
zsock_set_linger(pusher, 500);
rc = zsock_connect(pusher, "tcp://localhost:12345");
assert(rc==0);
void *puller = zsock_new(ZMQ_PULL);
assert(puller);
zsock_set_rcvhwm(puller, 1000);
zsock_set_linger(puller, 500);
rc = zsock_bind(puller, "tcp://*:12345");
if (rc != 12345){
printf("bind failed: %s\n", zmq_strerror(errno));
}
assert(rc == 12345);
void *publisher = zsock_new(ZMQ_PUB);
assert(publisher);
zsock_set_sndhwm(publisher, 1000);
zsock_set_linger(publisher, 500);
rc = zsock_bind(publisher, "tcp://*:12346");
assert(rc==12346);
// set up event loop
zloop_t *loop = zloop_new();
assert(loop);
zloop_set_verbose(loop, 0);
// push data every 10 ms
rc = zloop_timer(loop, 1, 0, timer_event, pusher);
assert(rc != -1);
zmq_pollitem_t item;
item.socket = puller;
item.events = ZMQ_POLLIN;
rc = zloop_poller(loop, &item, forward, publisher);
assert(rc == 0);
rc = zloop_start(loop);
printf("zloop return: %d", rc);
zloop_destroy(&loop);
assert(loop == NULL);
return 0;
}
示例14: socket_connect
STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
socket_obj_t *socket = self_in;
socket_check_closed(socket);
struct sockaddr sockaddr;
parse_inet_addr(socket, addr_in, &sockaddr);
int res = zsock_connect(socket->ctx, &sockaddr, sizeof(sockaddr));
RAISE_SOCK_ERRNO(res);
return mp_const_none;
}
示例15: zmonitor_test
void
zmonitor_test (bool verbose)
{
printf (" * zmonitor: ");
if (verbose)
printf ("\n");
#if defined (ZMQ_EVENT_ALL)
// @selftest
zsock_t *client = zsock_new (ZMQ_DEALER);
assert (client);
zactor_t *clientmon = zactor_new (zmonitor, client);
assert (clientmon);
if (verbose)
zstr_sendx (clientmon, "VERBOSE", NULL);
zstr_sendx (clientmon, "LISTEN", "LISTENING", "ACCEPTED", NULL);
zstr_sendx (clientmon, "START", NULL);
zsock_wait (clientmon);
zsock_t *server = zsock_new (ZMQ_DEALER);
assert (server);
zactor_t *servermon = zactor_new (zmonitor, server);
assert (servermon);
if (verbose)
zstr_sendx (servermon, "VERBOSE", NULL);
zstr_sendx (servermon, "LISTEN", "CONNECTED", "DISCONNECTED", NULL);
zstr_sendx (servermon, "START", NULL);
zsock_wait (servermon);
// Allow a brief time for the message to get there...
zmq_poll (NULL, 0, 200);
// Check client is now listening
int port_nbr = zsock_bind (client, "tcp://127.0.0.1:*");
assert (port_nbr != -1);
s_assert_event (clientmon, "LISTENING");
// Check server connected to client
zsock_connect (server, "tcp://127.0.0.1:%d", port_nbr);
s_assert_event (servermon, "CONNECTED");
// Check client accepted connection
s_assert_event (clientmon, "ACCEPTED");
zactor_destroy (&clientmon);
zactor_destroy (&servermon);
zsock_destroy (&client);
zsock_destroy (&server);
#endif
// @end
printf ("OK\n");
}