mco_iot_comm_listen
此函数创建一个套接字以在指定的 IP 地址上监听。
套接字参数 params 可以为 NULL,在这种情况下,sock_domain 和 ssl_params 元素将从通信器中获取。
请注意,在单个通信器内可以创建并监听任意数量的套接字。
MCO_RET mco_iot_comm_listen(
mco_iot_comm_h comm,
const char *address,
mco_sock_params_t *params
);有关概述,请参阅主动复制结构页面。
参数
comm
mco_iot_comm_h
通信器对象。
address
const char
要侦听的IP地址(iface:port 或 port)。
params
mco_sock_params_t
为网络通信定义的套接字参数。
返回
MCO_S_OK
套接字已成功创建并开始监听。
MCO_E_IOT_INVALID_HANDLE
通信器句柄 comm 无效。
MCO_E_NOMEM
无法为内部结构分配内存。
MCO_E_NW_*
网络错误:无法解析地址、创建套接字、绑定套接字或监听套接字。
示例
下面的代码片段初始化了 ARF(物联网)运行时环境,打开并连接数据库,创建通信器和复制器,然后开始监听 15000 端口:
int main(int argc, char *argv[])
{
mco_db_params_t db_params;
mco_device_t dev;
mco_db_h db;
mco_iot_replicator_params_t repl_params;
mco_iot_comm_params_t comm_params;
mco_iot_replicator_h repl;
mco_iot_comm_h comm;
const char *conn_string = (argc > 1) ? argv[1] : "127.0.0.1:15000";
...
/* Initialize SmartEDB and IoT runtimes */
mco_error_set_handler(&sample_errhandler);
mco_runtime_start();
mco_iot_init();
/* Create database */
dev.type = MCO_MEMORY_CONV;
dev.assignment = MCO_MEMORY_ASSIGN_DATABASE;
dev.size = DEVICE_DATABASE_SIZE;
dev.dev.conv.ptr = (void*)malloc(dev.size);
mco_db_params_init (&db_params);
db_params.db_max_connections = 5;
if (argc > 2) {
db_params.iot_agent_id = atoi(argv[2]); /* Override the agent_id in the schema */
}
CHECK(mco_db_open_dev(db_name, iotdevice_get_dictionary(), &dev, 1, &db_params));
CHECK(mco_db_connect(db_name, &db));
mco_iot_comm_params_init(&comm_params);
CHECK(mco_iot_comm_create(&comm_params, &comm));
mco_iot_replicator_params_init(&repl_params);
CHECK(mco_iot_replicator_create(db, comm, &repl_params, &repl));
...
CHECK(mco_iot_comm_listen(comm, "15000", 0));
...
}文件
- 头文件:
mcoiot.h - 源文件:
mcoiotcomm.c - 库:
libmcoiotcomm.a
