mco_iot_comm_connect_async
此函数会在指定地址异步调用 connect()
并在连接建立之前立即返回。
如果连接成功建立,则会调用 onConnect()
回调函数。否则将调用 onDestroy()
回调函数。
MCO_RET mco_iot_comm_connect_async(
mco_iot_comm_h comm,
const char *address,
timer_unit timeout,
unsigned int n_attempts,
timer_unit interval,
mco_sock_params_t *params
);
有关概述,请参阅主动复制结构页面。
参数
comm
mco_iot_comm_h
通信器对象。
address
const char
要侦听的IP地址(iface:port
或 port
)。
timeout
timer_unit
连接超时时间(以毫秒为单位)。
n_attempts
unsigned int
连接尝试的次数,即connect()
被调用的次数。
interval
timer_unit
连接尝试之间的间隔(以毫秒为单位)。
params
mco_sock_params_t
为网络通信定义的套接字参数。
返回
MCO_S_OK
套接字已成功创建并开始监听。
MCO_E_IOT_INVALID_HANDLE
通信器句柄 comm
无效。
MCO_E_NOMEM
无法为内部结构分配内存。
MCO_E_NW_*
网络错误:无法解析地址、创建套接字、绑定套接字或监听套接字。
示例
下面的代码片段异步连接到地址 192.168.0.1:15000
,连接超时时间为 2 秒,尝试连接 10 次,每次尝试间隔 1 秒:
int main(int argc, char *argv[])
{
...
CHECK(mco_iot_comm_connect_async(comm, "192.168.0.1:15000", 2000, 10, 1000, 0));
...
}
文件
- 头文件:
mcoiot.h
- 源文件:
mcoiotcomm.c
- 库:
libmcoiotcomm.a