被send()返回error=11折磨
Posted: Fri May 06, 2022 3:18 am
代码如下,使用send()发送一段时间,在室内只要网络稍微一差就会出现error=11,例如网络ping值大于100ms就会出现,即使ping值恢复到10ms以内,也恢复不了。已经被这个问题折磨了好久
- int tcpServerPublish(int sock, const uint8_t *data, size_t len)
- {
- if (sock == INVALID_SOCK)
- return -1;
- int to_write = len;
- int last_errno = 0;
- int try_send_cnt = 0;
- while (to_write > 0)
- {
- int written = send(sock, data + (len - to_write), to_write, MSG_DONTWAIT);
- if (written < 0)
- {
- if (errno != EINPROGRESS && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
- {
- if (errno != last_errno)
- {
- last_errno = errno;
- ESP_LOGE(TAG, "%s(%d),[sock=%d]: Error occurred,error=%d", __func__, __LINE__, sock, errno);
- }
- return -1;
- }
- else
- {
- if (errno != last_errno)
- {
- last_errno = errno;
- ESP_LOGW(TAG, "%s,[sock=%d]: Error occurred ,error=%d", __func__, sock, errno);
- }
- vTaskDelay(pdMS_TO_TICKS(10));
- if (try_send_cnt++ > 10)
- {
- ESP_LOGE(TAG, "%s,[sock=%d]: Error occurred ,error=%d", __func__, sock, errno);
- return -2;
- }
- }
- }
- else
- {
- // todo
- // send success
- to_write -= written;
- ESP_LOGD(TAG, "send %d bytes,last %d", written, to_write);
- try_send_cnt = 0;
- }
- }
- return len;
- }