// // Created by xajhu on 2021/7/15 0015. // #include #include #include #include "cmdline.h" #include "user_errno.h" int mq_cmd_run(const char *pSvr, const char *pCmd) { zmq_msg_t msg; int ret; void *context; void *requester; char *pRsp; unsigned int size; if (pSvr == NULL || pCmd == NULL || strlen(pSvr) == 0) { return -ERR_INPUT_PARAMS; } context = zmq_ctx_new(); requester = zmq_socket(context, ZMQ_REQ); ret = zmq_connect(requester, pSvr); if (ret != 0) { return -ERR_MQ_CONN_SERVER; } zmq_msg_init_size(&msg, strlen(pCmd)); memcpy(zmq_msg_data(&msg), pCmd, strlen(pCmd)); zmq_msg_send(&msg, requester, 0); zmq_msg_close(&msg); sleep(1); zmq_msg_init(&msg); zmq_msg_recv(&msg, requester, 0); pRsp = zmq_msg_data(&msg); size = zmq_msg_size(&msg); pRsp[size] = 0; printf("Service Response: {\n%s}, size: %u\n", (const char *)pRsp, size); zmq_msg_close(&msg); zmq_close(requester); zmq_ctx_destroy(context); return ERR_SUCCESS; }