---
title: CloseConnection
description: "关闭通信连接，销毁通信资源。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengcps/boostcph/instrucstreamengine/kunpengcpsinstruction_17_0013.html
sourcePath: /source/zh/kunpengcps/boostcph/instrucstreamengine/kunpengcpsinstruction_17_0013.html
indexId: b79d83d708a69d4298de3b7760b5d53a42e7d39bd7ec197e6d23fc3a6d25f07385
---
# CloseConnection

#### 函数功能

关闭通信连接，销毁通信资源。


#### 约束说明

指令流引擎要求二次开发者实现通信模块，用于数据的发送和接收。该函数是指令流引擎要求二次开发者在通信模块实现的外部符号，指令流引擎会调用该函数销毁指定的通信连接。在通信连接中断后，SendFully和Recv调用应返回错误码。


#### 函数原型

int CloseConnection(int connection)


#### 参数说明

| 参数名称 | 输入/输出 | 参数类型 | 参数描述 |
| --- | --- | --- | --- |
| connection | 输入 | int | OnNewConnectionCallback返回的连接句柄。 |


#### 返回值说明

数据类型：int

取值如下：

- 0：表示成功。
- 非0：错误码。


#### 调用示例

```
// 函数原型声明，实现由客户提供 
using OnNewConnectionCallback = void (*)(int connection);
using RegConnectionCBFunc = int (*)(OnNewConnectionCallback newConnCb);
using CloseConnectionFunc = int (*)(int connection);
using SendFullyFunc = ssize_t (*)(int connection, uint8_t *buf, size_t len); 
using RecvFunc = ssize_t (*)(int connection, uint8_t *buf, size_t len);
const char *soPath = "./libCommunication.so";

//服务端收到新连接回调函数，由云手机引擎提供
//输入参数connection代表新连接的有效句柄
static void OnNewConnection(int conn);

void Test()  
{
    // 动态加载通信动态库的函数符号
    void *handle = dlopen(soPath, RTLD_GLOBAL | RTLD_LAZY | RTLD_NODELETE);
    RegConnectionCBFunc regConnection = (RegConnectionCBFunc)dlsym(handle, "RegConnectionCB");
    CloseConnectionFunc closeConnection = (CloseConnectionFunc)dlsym(handle, "CloseConnection");
    SendFullyFunc sendFully = (SendFullyFunc)dlsym(handle, "SendFully");
    RecvFunc recvData = (RecvFunc)dlsym(handle, "Recv");
    size_t bufSize = 1024; 
    uint8_t *data = malloc(bufSize); 
    // 创建连接，连接服务端 
    int connRet = regConnection(OnNewConnection);  
    // 发送数据
    ssize_t ret = sendFully(conn, data, bufSize); 
    // 接收数据
    ret = recvData(conn, data, bufSize);
    // 释放资源 
    free(data); 
    closeConnection(conn);
    dlclose(handle);
}
```
