---
title: Recv
description: "接收对端发送的数据。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengcps/boostcph/instrucstreamengine/kunpengcpsinstruction_17_0015.html
sourcePath: /source/zh/kunpengcps/boostcph/instrucstreamengine/kunpengcpsinstruction_17_0015.html
indexId: f76a9164d5422162dc5f4ca2ac30ebfdb88a438ed7b72a7bb589470bda9e1fae85
---
# Recv

#### 函数功能

接收对端发送的数据。


#### 约束说明

指令流引擎要求二次开发者实现通信模块，用于数据的发送和接收。该函数是指令流引擎要求二次开发者在通信模块实现的外部符号，指令流引擎会调用该函数接收数据，需实现为阻塞式调用。


#### 函数原型

ssize_t Recv(int connection, uint8_t *buf, size_t len)


#### 参数说明

| 参数名称 | 输入/输出 | 参数类型 | 参数描述 |
| --- | --- | --- | --- |
| connection | 输入 | int | OnNewConnectionCallback返回的连接句柄。 |
| buf | 输入 | uint8\_t \* | 指向用于存放接收数据的内存指针。 |
| len | 输入 | size\_t | 用于存放接收数据的内存长度，不超过64KB。 |


#### 返回值说明

数据类型：ssize_t

取值如下：

- 大于0：实际接收到的数据字节数。
- -1：没有可接收的数据，可再次调用进行重试。
- -2：连接已经断开。


#### 调用示例

```
// 函数原型声明，实现由客户提供 
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);
}
```
