tcp_client例程为何去掉发送后,一直接收就会容易出现数据粘包
Posted: Fri Apr 01, 2022 3:05 pm
Code: Select all
while (1) {
//int err = send(sock, payload, strlen(payload), 0);
// if (err < 0) {
// ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
// break;
// }
int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
// Error occurred during receiving
if (len < 0) {
ESP_LOGE(TAG, "recv failed: errno %d", errno);
break;
}
// Data received
else {
rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip);
ESP_LOGI(TAG, "%s", rx_buffer);
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
[22:43:18.320]收←◆[0;32mI (13311) example: 00 02 00 00 00 06 01 01 00 00 00 0A[0m
[22:43:18.570]收←◆[0;32mI (13561) example: 00 02 00 00 00 06 01 01 00 00 00 0A[0m
[22:43:18.822]收←◆[0;32mI (13811) example: 00 02 00 00 00 06 01 01 00 00 00 0A00 02 00 00 00 06 01 01 00 00 00 0A[0m
[22:43:19.070]收←◆[0;32mI (14061) example: 00 02 00 00 00 06 01 01 00 00 00 0A[0m
[22:43:19.322]收←◆[0;32mI (14311) example: 00 02 00 00 00 06 01 01 00 00 00 0A[0m