esp32基于ESPIDF通过tcp协议传输二维整型数组
时间: 2023-05-18 17:06:05 浏览: 169
可以通过以下代码实现:
```
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "lwip/sockets.h"
#define PORT 8080
void app_main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
return;
}
else
printf("Socket successfully created..\n");
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if ((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
return;
}
else
printf("Socket successfully binded..\n");
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
return;
}
else
printf("Server listening..\n");
socklen_t len = sizeof(cli);
connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...\n");
return;
}
else
printf("server acccept the client...\n");
int arr[2][2] = {{1, 2}, {3, 4}};
char buffer[1024] = {0};
memcpy(buffer, arr, sizeof(arr));
write(connfd, buffer, sizeof(buffer));
close(sockfd);
}
```
这段代码可以在 ESP32 上基于 ESP-IDF 通过 TCP 协议传输二维整型数组。在这个例子中,我们创建了一个服务器,等待客户端连接。一旦客户端连接成功,服务器将一个二维整型数组写入到套接字中,客户端可以读取并解析这个数组。
阅读全文