断开连接 - TcpClientDisconnect
函数简介
断开TCP客户端连接。
接口名称
TcpClientDisconnect
DLL调用
int32_t TcpClientDisconnect(int64_t instance, int64_t client_handle)
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | int64_t | OLAPlug对象的指针 |
| client_handle | int64_t | 客户端句柄 |
示例
// 断开连接
int32_t result = TcpClientDisconnect(instance, client);
if (result == 1) {
printf("断开连接成功\n");
}
// 在回调中断开连接
void OnTcpClientEvent(int64_t client_handle, int32_t event_type,
int64_t data, int32_t data_len, int64_t user_data) {
if (event_type == 2) { // 收到数据
const char* msg = (char*)data;
if (strncmp(msg, "bye", 3) == 0) {
printf("收到断开指令\n");
TcpClientDisconnect(instance, client_handle);
}
} else if (event_type == 3) { // 连接断开
printf("连接已断开\n");
}
}
返回值
| 返回值 | 说明 |
|---|---|
| 0 | 失败 |
| 1 | 成功 |
注意事项
- 断开后会触发回调(event_type=3)
- 断开后可以重新调用
TcpClientConnect连接 - 如果未连接,调用会失败
- 断开是异步操作,可能不会立即完成
