本文整理汇总了C++中zmq_socket函数的典型用法代码示例。如果您正苦于以下问题:C++ zmq_socket函数的具体用法?C++ zmq_socket怎么用?C++ zmq_socket使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zmq_socket函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: hx_zmq_construct_socket
value hx_zmq_construct_socket (value context_handle,value type)
{
val_is_kind(context_handle,k_zmq_context_handle);
if (!val_is_int(type)) {
val_throw(alloc_int(EINVAL));
return alloc_null();
}
void *s = zmq_socket (val_data(context_handle),val_int(type));
int err = zmq_errno();
if (s == NULL) {
val_throw (alloc_int(err));
return alloc_null();
}
// See: http://nekovm.org/doc/ffi#abstracts_and_kinds
value v = alloc_abstract(k_zmq_socket_handle,s);
val_gc(v,finalize_socket); // finalize_socket is called when the abstract value is garbage collected
return v;
}
示例2: zmq_socket
bool ztop::InitialService(std::vector<std::pair<std::string, std::string>> servicePair, ztop*pztop)
{
auto lambda_setsocket = [](std::pair<std::string, std::string>& sPair, ztop*pztop){
void*s = zmq_socket(pztop->ctx, ZMQ_PUSH);
int rc = zmq_bind(s, sPair.second.c_str());
if (rc != -1)
{
pztop->pairs.insert(std::make_pair(sPair.first, s));
pztop->vItems.push_back({ s, 0, ZMQ_POLLIN, 0 });
/* pztop->jobSendSoketSets.insert(s);*/
std::cerr << sPair.first << " start success:" << sPair.second << std::endl;
}
else
std::cerr << __FUNCTION__ << ":"<<"service not start,with err:=====>" << zmq_strerror(zmq_errno());
};
for_each(servicePair.begin(), servicePair.end(), [&](std::pair<std::string, std::string>& sPair){
lambda_setsocket(sPair, pztop);
});
return true;
}
示例3: create_sub_socket
int create_sub_socket(void* ctx) {
std::stringstream conn;
conn << "tcp://" << g_sip << ":" << g_port_sub;
void * sub = zmq_socket(ctx, ZMQ_SUB);
int rc = zmq_connect(sub, conn.str().c_str());
assert(rc == 0);
if (rc != 0) {
std::cerr << "Error Connect to " << conn.str() << " errno " << errno << std::endl;
zmq_close(sub);
exit(EXIT_FAILURE);
}
char temp[256] = { 0 };
sprintf(temp, "%s ", g_sn);
printf("Subscribe id: %s\n", temp);
rc = zmq_setsockopt(sub, ZMQ_SUBSCRIBE, temp, strlen(temp));
assert(rc == 0);
rc = zmq_setsockopt(sub, ZMQ_SUBSCRIBE, "ALL ", strlen("ALL "));
assert(rc == 0);
g_sub_socket = sub;
return 0;
}
示例4: redirect_count
//********************************************************************************
// Function: redirect_count
//
// Description: Recieve Control Plane redirect count.
//********************************************************************************
static int32_t redirect_count(void)
{
void *context = zmq_ctx_new ();
void *requester = zmq_socket(context, ZMQ_REQ);
int32_t timeo{};
zmq_setsockopt(requester, ZMQ_LINGER, (void*) &timeo, sizeof(timeo));
char buffer[64];
sprintf(buffer, "tcp://localhost:%d", TCPLANE_SERVICE);
zmq_connect(requester, buffer);
sprintf(buffer, "%d", redirectCount);
zmq_send(requester, buffer, strlen(buffer), 0);
size_t size = timed_read(requester, buffer, sizeof(buffer), READ_TIMEOUT);
if(size)
size = atoi(buffer);
zmq_close(requester);
zmq_ctx_destroy(context);
return size;
}
示例5: main
int main (void)
{
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int i=1;
char skeystr[] = "JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6";
uint8_t server_key [32];
zmq_z85_decode (server_key, skeystr);
zmq_setsockopt(responder, ZMQ_CURVE_SERVER, &i, sizeof(i));
zmq_setsockopt(responder, ZMQ_CURVE_SECRETKEY, &server_key, sizeof(server_key));
int rc = zmq_bind (responder, "tcp://*:55555");
assert (rc == 0);
while (1) {
char buffer [10];
zmq_recv (responder, buffer, 10, 0);
printf ("Received Hello\n");
sleep (1);
zmq_send (responder, "World", 5, 0);
}
return 0;
}
示例6: zmq_ctx_new
// .split worker task
// While this example runs in a single process, that is just to make
// it easier to start and stop the example. Each thread has its own
// context and conceptually acts as a separate process.
// This is the worker task, using a REQ socket to do load-balancing.
// Because s_send and s_recv can't handle 0MQ binary identities, we
// set a printable text identity to allow routing.
static void *worker_task(void *args)
{
void *context = zmq_ctx_new();
void *worker = zmq_socket(context, ZMQ_REQ);
#if (defined (WIN32))
s_set_id(worker, (intptr_t)args);
zmq_connect(worker, "tcp://localhost:5673"); // backend
#else
s_set_id(worker);
zmq_connect(worker, "ipc://backend.ipc");
#endif
// Tell broker we're ready for work
s_send(worker, "READY");
while (1) {
// Read and save all frames until we get an empty frame
// In this example there is only 1, but there could be more
char *identity = s_recv(worker);
char *empty = s_recv(worker);
assert(*empty == 0);
free(empty);
// Get request, send reply
char *request = s_recv(worker);
printf("Worker: %s from %s\n", request, identity);
free(request);
s_sendmore(worker, identity);
s_sendmore(worker, "");
s_send(worker, "OK");
free(identity);
}
zmq_close(worker);
zmq_ctx_destroy(context);
return NULL;
}
示例7: main
int main (void)
{
setup_test_environment ();
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sock = zmq_socket (ctx, ZMQ_PUB);
assert (sock);
int rc = zmq_connect (sock, "tcp://localhost:1234");
assert (rc == 0);
rc = zmq_connect (sock, "tcp://[::1]:1234");
assert (rc == 0);
rc = zmq_connect (sock, "tcp://localhost:invalid");
assert (rc == -1);
rc = zmq_connect (sock, "tcp://in val id:1234");
assert (rc == -1);
rc = zmq_connect (sock, "tcp://");
assert (rc == -1);
rc = zmq_connect (sock, "tcp://192.168.0.200:*");
assert (rc == -1);
rc = zmq_connect (sock, "invalid://localhost:1234");
assert (rc == -1);
assert (errno == EPROTONOSUPPORT);
rc = zmq_close (sock);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
}
示例8: main
int main(int argc, char *argv[])
{
if (argc < 3) return EXIT_FAILURE;
int M = atoi(argv[1]);
int N = atoi(argv[2]);
printf("M: %d, N: %d\n", M, N);
void *ctx = zmq_init(1);
void *b = zmq_socket(ctx, ZMQ_PAIR);
zmq_connect(b, "tcp://localhost:4444");
zmq_msg_t msg;
int val[M];
long long start_time = sc_time();
int i;
for (i=0; i<N; i++) {
int *buf = (int *)malloc(M * sizeof(int));
memset(val, i, M * sizeof(int));
memcpy(buf, val, M * sizeof(int));
zmq_msg_init_data(&msg, buf, M * sizeof(int), _dealloc, NULL);
zmq_send(b, &msg, 0);
zmq_msg_close(&msg);
zmq_msg_init(&msg);
zmq_recv(b, &msg, 0);
memcpy(val, (int *)zmq_msg_data(&msg), zmq_msg_size(&msg));
zmq_msg_close(&msg);
}
long long end_time = sc_time();
printf("zmq_a: Time elapsed: %f sec\n", sc_time_diff(start_time, end_time));
zmq_close(b);
zmq_term(ctx);
return EXIT_SUCCESS;
}
示例9: zmq_ctx_new
bool SubscriberZMQ::_onInit(TUInt port)
{
m_context = zmq_ctx_new();
if (m_context == 0)
{
zmqlog("Error occurred during zmq_ctx_new()");
return false;
}
m_socket = zmq_socket(m_context, ZMQ_SUB);
if (m_socket == 0)
{
zmqlog("Error occurred during zmq_socket()");
clear();
return false;
}
TChar address[100] = {'\0'};
sprintf(address, "tcp://localhost:%u", port);
int result = zmq_connect(m_socket, address);
if (result != 0)
{
zmqlog("Error occurred during zmq_init()");
clear();
return false;
}
const TString& subscribeId = getSubscribeId();
result = zmq_setsockopt (m_socket, ZMQ_SUBSCRIBE, subscribeId.c_str(), subscribeId.length() - 1);
if (result != 0)
{
zmqlog("Error occurred during zmq_setsockopt");
clear();
return false;
}
return true;
}
示例10: main
int main ()
{
void *ctx = zmq_init (1);
assert (ctx);
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer);
int rc = zmq_connect (dealer, "tcp://localhost:2211");
assert (rc == 0);
while (1) {
// Send signal to parent
zmq_msg_t msg;
zmq_msg_init (&msg);
zmq_sendmsg (dealer, &msg, 0);
zmq_msg_close (&msg);
// Do some work
sleep (1);
}
return 0;
}
示例11: main
int main(void)
{
void *context = zmq_ctx_new();
//Socket to connect to Server
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_connect(requester, "tcp://localhost:8888");
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;
}
示例12: main
void main(){
fprintf(stdout,"##### Client-1 is running - C Broker Test #####\n");
void *context = zmq_ctx_new ();
void *requester = zmq_socket (context, ZMQ_PUSH);
zmq_connect (requester, "tcp://127.0.0.1:5559");
char *str_message="i am client-1";
int request_number;
//sending 1000000 requests from client
for (request_number = 1; request_number <= 1000000; request_number++) {
zstr_send (requester, str_message);
}
zmq_close (requester);
zmq_ctx_destroy (context);
}
示例13: main
int main (int argc, char const *argv[]) {
void* context = zmq_ctx_new();
//create a SUB socket
void* subscriber = zmq_socket(context, ZMQ_SUB);
const char* filter;
if(argc > 1) {
filter = argv[1];
} else {
filter = "Company1|";
}
printf("Collecting stock information from the server.\n");
int conn = zmq_connect(subscriber, "tcp://localhost:4040");
// must set a subscription for SUB socket
conn = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, filter, strlen(filter));
int i = 0;
for(i = 0; i < 10; i++) {
zmq_msg_t reply;
zmq_msg_init(&reply);
// receive the message, previous message is deallocated
zmq_msg_recv(&reply, subscriber, 0);
int length = zmq_msg_size(&reply);
char* value = malloc(length + 1);
memcpy(value, zmq_msg_data(&reply), length);
zmq_msg_close(&reply);
printf("%s\n", value);
free(value);
}
zmq_close(subscriber);
zmq_ctx_destroy(context);
return 0;
}
示例14: Java_org_zeromq_ZMQ_00024Socket_construct
JNIEXPORT void JNICALL
Java_org_zeromq_ZMQ_00024Socket_construct (JNIEnv *env, jobject obj, jobject context, jint type)
{
void *s = get_socket (env, obj);
if (s)
return;
void *c = fetch_context (env, context);
if (c == NULL) {
raise_exception (env, EINVAL);
return;
}
s = zmq_socket (c, type);
int err = zmq_errno();
if (s == NULL) {
raise_exception (env, err);
return;
}
put_socket(env, obj, s);
}
示例15: main
int main(void)
{
// Prepare our context and socket
void *context = zmq_ctx_new();
void *receiver = zmq_socket(context, ZMQ_PULL);
zmq_bind(receiver, "tcp://*:5558");
// Wait for start of batch
char *string = s_recv(receiver);
free(string);
printf("start of batch\n");
// Start our clock now
int64_t start_time = s_clock();
// Process 100 confirmations
int task_nbr;
for(task_nbr=0; task_nbr<100; task_nbr++)
{
char *string = s_recv(receiver);
free(string);
if(task_nbr % 10 == 0)
{
printf(":");
}
else
{
printf(".");
}
fflush(stdout);
}
// Calculate and report duration of batch
printf("\nTotal elapsed time: %d msec\n", (int)(s_clock() - start_time));
zmq_close(receiver);
zmq_ctx_destroy(context);
return 0;
}