当前位置: 首页>>代码示例>>C++>>正文


C++ zmq_ctx_destroy函数代码示例

本文整理汇总了C++中zmq_ctx_destroy函数的典型用法代码示例。如果您正苦于以下问题:C++ zmq_ctx_destroy函数的具体用法?C++ zmq_ctx_destroy怎么用?C++ zmq_ctx_destroy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了zmq_ctx_destroy函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: ntop_zmq_connect

static int ntop_zmq_connect(lua_State* vm) {
  char *endpoint, *topic;
  void *context, *subscriber;

  if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_PARAM_ERROR);
  if((endpoint = (char*)lua_tostring(vm, 1)) == NULL)  return(CONST_LUA_PARAM_ERROR);

  if(ntop_lua_check(vm, __FUNCTION__, 2, LUA_TSTRING)) return(CONST_LUA_PARAM_ERROR);
  if((topic = (char*)lua_tostring(vm, 2)) == NULL)     return(CONST_LUA_PARAM_ERROR);

  context = zmq_ctx_new(), subscriber = zmq_socket(context, ZMQ_SUB);

  if(zmq_connect(subscriber, endpoint) != 0) {
    zmq_close(subscriber);
    zmq_ctx_destroy(context);
    return(CONST_LUA_PARAM_ERROR);
  }

  if(zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, topic, strlen(topic)) != 0) {
    zmq_close(subscriber);
    zmq_ctx_destroy(context);
    return -1;
  }

  lua_pushlightuserdata(vm, context);
  lua_setglobal(vm, "zmq_context");

  lua_pushlightuserdata(vm, subscriber);
  lua_setglobal(vm, "zmq_subscriber");

  return(CONST_LUA_OK);
}
开发者ID:ctcble,项目名称:ntopng,代码行数:32,代码来源:Lua.cpp

示例2: main

int main (void)
{
    setup_test_environment();
    
    void *ctx1 = zmq_ctx_new ();
    assert (ctx1);

    void *ctx2 = zmq_ctx_new ();
    assert (ctx2);

    void *router = zmq_socket (ctx1, ZMQ_ROUTER);    
    int on = 1;
    int rc = zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &on, sizeof (on));
    assert (rc == 0);
    rc = zmq_bind (router, "tcp://127.0.0.1:5555");
    assert (rc != -1);
 
    //  Repeat often enough to be sure this works as it should
    for (int cycle = 0; cycle < 100; cycle++) {
        //  Create dealer with unique explicit identity
        //  We assume the router learns this out-of-band
        void *dealer = zmq_socket (ctx2, ZMQ_DEALER);
        char identity [10];
        sprintf (identity, "%09d", cycle);
        rc = zmq_setsockopt (dealer, ZMQ_IDENTITY, identity, 10);
        assert (rc == 0);
        int rcvtimeo = 1000;
        rc = zmq_setsockopt (dealer, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int));
        assert (rc == 0);
        rc = zmq_connect (dealer, "tcp://127.0.0.1:5555");
        assert (rc == 0);

        //  Router will try to send to dealer, at short intervals.
        //  It typically takes 2-5 msec for the connection to establish
        //  on a loopback interface, but we'll allow up to one second
        //  before failing the test (e.g. for running on a debugger or
        //  a very slow system).
        for (int attempt = 0; attempt < 500; attempt++) {
            zmq_poll (0, 0, 2);
            rc = zmq_send (router, identity, 10, ZMQ_SNDMORE);
            if (rc == -1 && errno == EHOSTUNREACH)
                continue;
            assert (rc == 10);
            rc = zmq_send (router, "HELLO", 5, 0);
            assert (rc == 5);
            break;
        }
        uint8_t buffer [5];
        rc = zmq_recv (dealer, buffer, 5, 0);
        assert (rc == 5);
        assert (memcmp (buffer, "HELLO", 5) == 0);
        close_zero_linger (dealer);
    }
    zmq_close (router);
    zmq_ctx_destroy (ctx1);
    zmq_ctx_destroy (ctx2);

    return 0;
}
开发者ID:HJoYer,项目名称:libzmq,代码行数:59,代码来源:test_issue_566.cpp

示例3: main

int main(int argc, char * argv[])
{
	void * pCtx = NULL;
	void * pSock = NULL;
	const char * pAddr = "tcp://*:7766";

	//创建context,zmq的socket 需要在context上进行创建 
	if ((pCtx = zmq_ctx_new()) == NULL)
	{
		return 0;
	}
	//创建zmq socket ,socket目前有6中属性 ,这里使用dealer方式
	//具体使用方式请参考zmq官方文档(zmq手册) 
	if ((pSock = zmq_socket(pCtx, ZMQ_DEALER)) == NULL)
	{
		zmq_ctx_destroy(pCtx);
		return 0;
	}
	int iRcvTimeout = 5000;// millsecond
	//设置zmq的接收超时时间为5秒 
	if (zmq_setsockopt(pSock, ZMQ_RCVTIMEO, &iRcvTimeout, sizeof(iRcvTimeout)) < 0)
	{
		zmq_close(pSock);
		zmq_ctx_destroy(pCtx);
		return 0;
	}
	//绑定地址 tcp://*:7766 
	//也就是使用tcp协议进行通信,使用网络端口 7766
	if (zmq_bind(pSock, pAddr) < 0)
	{
		zmq_close(pSock);
		zmq_ctx_destroy(pCtx);
		return 0;
	}
	printf("bind at : %s\n", pAddr);
	while (1)
	{
		char szMsg[1024] = { 0 };
		printf("waitting...\n");
		errno = 0;
		//循环等待接收到来的消息,当超过5秒没有接到消息时,
		//zmq_recv函数返回错误信息 ,并使用zmq_strerror函数进行错误定位 
		if (zmq_recv(pSock, szMsg, sizeof(szMsg), 0) < 0)
		{
			printf("error = %s\n", zmq_strerror(errno));
			continue;
		}
		printf("received message : %s\n", szMsg);
	}

	return 0;
}
开发者ID:zhouxicai,项目名称:ToolCode,代码行数:52,代码来源:zmq_recv.cpp

示例4: main

int main(int argc, char * argv[])
{
	void * pCtx = NULL;
	void * pSock = NULL;
	//使用tcp协议进行通信,需要连接的目标机器IP地址为192.168.1.2
	//通信使用的网络端口 为7766 
	const char * pAddr = "tcp://127.0.0.1:7766";//"tcp://192.168.1.198:7766";

	//创建context 
	if ((pCtx = zmq_ctx_new()) == NULL)
	{
		return 0;
	}
	//创建socket 
	if ((pSock = zmq_socket(pCtx, ZMQ_DEALER)) == NULL)
	{
		zmq_ctx_destroy(pCtx);
		return 0;
	}
	int iSndTimeout = 5000;// millsecond
	//设置接收超时 
	if (zmq_setsockopt(pSock, ZMQ_RCVTIMEO, &iSndTimeout, sizeof(iSndTimeout)) < 0)
	{
		zmq_close(pSock);
		zmq_ctx_destroy(pCtx);
		return 0;
	}
	//连接目标IP192.168.1.2,端口7766 
	if (zmq_connect(pSock, pAddr) < 0)
	{
		zmq_close(pSock);
		zmq_ctx_destroy(pCtx);
		return 0;
	}
	//循环发送消息 
	while (1)
	{
		static int i = 0;
		char szMsg[1024] = { 0 };
		sprintf_s(szMsg, "hello world : %3d", i++);
		printf("Enter to send...\n");
		if (zmq_send(pSock, szMsg, sizeof(szMsg), 0) < 0)
		{
			fprintf(stderr, "send message faild\n");
			continue;
		}
		printf("send message : [%s] succeed\n", szMsg);
		getchar();
	}

	return 0;
}
开发者ID:zhouxicai,项目名称:ToolCode,代码行数:52,代码来源:zmq_send_1.cpp

示例5: zmq_ctx_new

bool Service::subscribe( const std::string& brokerAddress,
                         const Publisher& publisher )
{
    void* context = zmq_ctx_new();
    void* socket = zmq_socket( context, ZMQ_REQ );
    const std::string zmqAddress = std::string("tcp://" ) + brokerAddress;
    if( zmq_connect( socket, zmqAddress.c_str( )) == -1 )
    {
        ZEQINFO << "Can't reach connection broker at " << brokerAddress
                << std::endl;
        zmq_close( socket );
        zmq_ctx_destroy( context );
        return false;
    }

    const std::string& address = publisher.getAddress();
    zmq_msg_t request;
    zmq_msg_init_size( &request, address.size( ));
    memcpy( zmq_msg_data( &request ), address.c_str(), address.size( ));

    if( zmq_msg_send( &request, socket, 0 ) == -1 )
    {
        zmq_msg_close( &request );
        ZEQINFO << "Can't send connection request " << address << " to "
                << brokerAddress << ": " << zmq_strerror( zmq_errno( ))
                << std::endl;
        return false;
    }
    zmq_msg_close( &request );

    zmq_msg_t reply;
    zmq_msg_init( &reply );
    if( zmq_msg_recv( &reply, socket, 0 )  == -1 )
    {
        zmq_msg_close( &reply );
        ZEQINFO << "Can't receive connection reply from " << brokerAddress
                << std::endl;
        return false;
    }

    const std::string result( (const char*)zmq_msg_data( &reply ),
                              zmq_msg_size( &reply ));
    zmq_msg_close( &reply );

    zmq_close( socket );
    zmq_ctx_destroy( context );

    return address == std::string( result );
}
开发者ID:segalindo,项目名称:zeq,代码行数:49,代码来源:service.cpp

示例6: main

int
main (int argc, char **argv)
{
    int i;
    char *endpoint = NULL;
    void *context, *socket;

    if (argc <= 2) {
        _usage(argv[0]);
        return -1;
    }

    endpoint = argv[1];

    context = zmq_ctx_new();
    if (!context) {
        _ERR("ZeroMQ context: %s\n", zmq_strerror(errno));
        return -1;
    }

    socket = zmq_socket(context, ZMQ_PUSH);
    if (!socket) {
        _ERR("ZeroMQ socket: %s\n", zmq_strerror(errno));
        zmq_ctx_destroy(context);
        return -1;
    }

    if (zmq_connect(socket, endpoint) == -1) {
        _ERR("ZeroMQ connect: %s: %s\n", endpoint, zmq_strerror(errno));
        zmq_close(socket);
        zmq_ctx_destroy(context);
        return -1;
    }

    for (i = 2; i != (argc - 1); i++) {
        if (zmq_send(socket, argv[i], strlen(argv[i]), ZMQ_SNDMORE) == -1) {
            _ERR("ZeroMQ send: %s\n", zmq_strerror(errno));
        }
    }
    if (zmq_send(socket, argv[i], strlen(argv[i]), 0) == -1) {
        _ERR("ZeroMQ send: %s\n", zmq_strerror(errno));
    }

    zmq_close(socket);
    zmq_ctx_destroy(context);

    return 0;
}
开发者ID:just-hu,项目名称:zlmb,代码行数:48,代码来源:exp_client.c

示例7: main

int main (void)
{
  printf ("Connecting to math server…\n");
  void *context = zmq_ctx_new ();
  void *requester = zmq_socket (context, ZMQ_REQ);
  zmq_connect (requester, "tcp://localhost:5555");
  srand(time(NULL));

  int request_nbr;
  for (request_nbr = 0; request_nbr < 1000; request_nbr++) {
    char buffer [10];
    int a = randof(30);
    int b = randof(30);
    printf ("Sending (%d, %d)…\n", a, b);
    char to_send[100];
    sprintf(to_send, "%d %d", a, b);
    s_send(requester, to_send);
    char* ret = s_recv(requester);
    int sum = atoi(ret);
    printf("%d + %d = %d\n", a, b, sum);
    free(ret);
  }
  zmq_close (requester);
  zmq_ctx_destroy (context);
  return 0;
}
开发者ID:player1537,项目名称:cee-stuff,代码行数:26,代码来源:mathy-add-client.c

示例8: zmq_close

ExportInterface::~ExportInterface() {
  if(topic)    free(topic);
  if(endpoint) free(endpoint);

  zmq_close(publisher);
  zmq_ctx_destroy(context);
}
开发者ID:tonychung00,项目名称:ntopng,代码行数:7,代码来源:ExportInterface.cpp

示例9: assert

//-----------------------------------------------------------------------------
// TODO: determine level of detail for return value, i.e. void, bool or enum or
// if the errors should cause this function to automatically try again.
connection_c::error_e connection_c::close( void ) {
  // sanity checks
  assert( _open );

  // close the socket
  if( zmq_close( _socket ) == -1 ) {
    if( errno == ENOTSOCK ) {
      // the provided socket was invalid
      return ERROR_SOCKET;
    }
  }

  // if a client or router, destroy the context
  if( _role == CLIENT || _role == ROUTER ) {
    if( zmq_ctx_destroy( _context ) == -1 ) {
      if( errno == EFAULT ) {
        // The provided context was invalid
        return ERROR_CONTEXT;
      } else if( errno == EINTR ) {
        // Termination was interrupted by a signal, can be restarted.
        return ERROR_INTERRUPT;
      } else {
        // Note: above should trap specifics, but if fall through then return
        // a socket error  
        return ERROR_SOCKET;
      }    
    }
  }

  _open = false;
  return ERROR_NONE;
}
开发者ID:jacquelinekay,项目名称:reveal,代码行数:35,代码来源:connection.cpp

示例10: main

int main (int argc, char *argv [])
{
  // Socket to talk to server
  printf ("Collecting updates from weather server…\n");
  void *context = zmq_ctx_new ();
  void *subscriber = zmq_socket (context, ZMQ_SUB);
  int rc = zmq_connect (subscriber, "tcp://localhost:5556");
  assert (rc == 0);

  // Subscribe to zipcode, default is NYC, 10001
  char *filter = (argc > 1)? argv [1]: "10001 ";
  rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
		       filter, strlen (filter));
  assert (rc == 0);

  // Process 100 updates
  int update_nbr;
  long total_temp = 0;
  for (update_nbr = 0; update_nbr < 100; update_nbr++) {
    char *string = s_recv (subscriber);

    int zipcode, temperature, relhumidity;
    sscanf (string, "%d %d %d",
	    &zipcode, &temperature, &relhumidity);
    total_temp += temperature;
    free (string);
  }
  printf ("Average temperature for zipcode '%s' was %dF\n",
	  filter, (int) (total_temp / update_nbr));

  zmq_close (subscriber);
  zmq_ctx_destroy (context);
  return 0;
}
开发者ID:player1537,项目名称:cee-stuff,代码行数:34,代码来源:weather-client.c

示例11: main

int 
main(int argc, char* argv[])
{
  void* ctx = zmq_ctx_new();
  void* rep = zmq_socket(ctx, ZMQ_REP);
  zmq_connect(rep, "tcp://localhost:6666");

  do {
    int count = 0, server_id;
    char buf[128];

    if (argc < 2) {
      fprintf(stderr, "arguments error ...\n");
      break;
    }
    server_id = atoi(argv[1]);

    fprintf(stdout, "reply server[%d] init success ...\n", server_id);
    while (1) {
      memset(buf, 0, sizeof(buf));
      zmq_recv(rep, buf, sizeof(buf), 0);
      fprintf(stdout, "recevied request {%s}\n", buf);

      sprintf(buf, "[%d] reply [%d]\n", server_id, ++count);
      zmq_send(rep, buf, strlen(buf), 0);
    }
  } while (0);

  zmq_close(rep);
  zmq_ctx_destroy(ctx);

  return 0;
}
开发者ID:hbfhaapy,项目名称:study,代码行数:33,代码来源:rep.c

示例12: main

int main()
{
    void *context = zmq_ctx_new();

    void *responder = zmq_socket(context, ZMQ_REP);
    zmq_bind(responder, "tcp://*:5555");

    while(1) {
        zmq_msg_t request;
        zmq_msg_init(&request);
        zmq_msg_recv(&request, responder, 0);
        char *recvmsg ;
        int recvsize = zmq_msg_size(&request);
        //memset(recvmsg, 0, recvsize);
        memcpy(recvmsg, (char *)zmq_msg_data(&request), recvsize);
        recvmsg[recvsize] = '\0';
        printf("Received request :[%s]\n", recvmsg);
        zmq_msg_close(&request);

        sleep(1);

        zmq_msg_t reply;
        //zmq_msg_init_size(&reply, 5);
        //memcpy((void*)zmq_msg_data(&reply), "World", 5);
        zmq_msg_init_data(&reply, "World", 5, NULL, NULL);
        zmq_msg_send(&reply, responder, 0);
        zmq_msg_close(&reply);
    }
    zmq_close(responder);
    zmq_ctx_destroy(context);
    return 0;
}
开发者ID:wuyilun,项目名称:ZeromqExample,代码行数:32,代码来源:rep.c

示例13: main

int main (void)
{
    void *context = zmq_ctx_new ();
    void *worker = zmq_socket (context, ZMQ_DEALER);
    s_set_id (worker);          //  Set a printable identity
    zmq_connect (worker, "tcp://localhost:5671");

    int total = 0;
    while (1) {
        //  Tell the broker we're ready for work
        s_sendmore (worker, "");
        s_send (worker, "Hi Boss");

        //  Get workload from broker, until finished
        // free (s_recv (worker));     //  Envelope delimiter
        // char *workload = s_recv (worker);
        // int finished = (strcmp (workload, "Fired!") == 0);
        // free (workload);
        // if (finished) {
            // printf ("Completed: %d tasks\n", total);
            // break;
        // }
        total++;

        //  Do some random work
        s_sleep (randof (500) + 1);
    }
    zmq_close (worker);
    zmq_ctx_destroy (context);
    return 0;
}
开发者ID:machicao2013,项目名称:recipes,代码行数:31,代码来源:dealer.c

示例14: main

int main (int argc, char *argv [])
{
    void *context = zmq_ctx_new ();
    void *subscriber = zmq_socket (context, ZMQ_SUB);
    void *buffer = (void *) malloc(MAXSZ);
    lo_message msg;
    int rc = zmq_connect (subscriber, "tcp://localhost:5556");
    assert (rc == 0);

    //  Subscribe to all
    rc = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE,
			NULL, 0);
    assert (rc == 0);

    osc_init();
    while(1) {
      int size = zmq_recv(subscriber, buffer, MAXSZ, 0);
      msg = (lo_message) buffer;
      if (size > 0) {
	printf("received a message that's %d big\n", size);
	lo_server_dispatch_data(s, buffer, size);
      }
      else {
	printf("oops.\n");
      }
    }
    zmq_close (subscriber);
    zmq_ctx_destroy (context);
    return 0;
}
开发者ID:davelab6,项目名称:0mqosctest,代码行数:30,代码来源:client.c

示例15: main

int main(int argc, char** argv) {
    if(argc < 3) {
        std::cout << "usage: " 
                  << argv[0] 
                  << " <front-end URI> <back-end URI>"
                  << std::endl;
        std::cout << "Example: broker \"tcp://*:5555\" \"tcp://*:6666\"\n";          
        return 0;          
    }
    void* ctx = zmq_ctx_new(); 
    void* frontend = zmq_socket(ctx, ZMQ_XPUB);
    void* backend = zmq_socket(ctx, ZMQ_XSUB);
    const char* brokerURI = argv[1];
    const char* backendURI = argv[2];
    int rc = zmq_bind(frontend, brokerURI);
    assert(rc == 0);
    rc = zmq_bind(backend, backendURI);
    assert(rc == 0);
    while(1) {
        zmq_proxy(frontend, backend, 0);
    }
    rc = zmq_close(frontend);
    assert(rc == 0);
    rc = zmq_close(backend);
    assert(rc == 0);
    rc = zmq_ctx_destroy(ctx);
    assert(rc == 0);
    return 0;
}
开发者ID:ugovaretto,项目名称:rlogger,代码行数:29,代码来源:broker.cpp


注:本文中的zmq_ctx_destroy函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。