My log says:
- Successfully connected
- Data sent successfully
- Failed to receive data: 11 - No more processes
It fails after the timeout. If I do not have the timeout, recv hangs forever.
This code runs in a separate thread. I have boiled down the code to this.
Any suggestions would be apreciated
I should mention, that I have also a webserver and a httpClient running.
But there should be plenty of resources.
I only send 12 bytes of data, and the reply is less than 100 bytes.
- struct sockaddr_in dest_addr;
- inet_pton(AF_INET, host_ip, &dest_addr.sin_addr);
- dest_addr.sin_family = AF_INET;
- dest_addr.sin_port = htons(PORT);
- addr_family = AF_INET;
- ip_protocol = IPPROTO_IP;
- int sock = socket(addr_family, SOCK_STREAM, ip_protocol);
- if (sock < 0)
- {
- ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
- break;
- }
- ESP_LOGI(TAG, "Socket created ");
- ESP_LOGI(TAG, "Connecting to %s:%d", host_ip, PORT);
- int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
- if (err != 0)
- {
- ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
- break;
- }
- ESP_LOGI(TAG, "Successfully connected");
- int nSend = send(sock, sendbuffer, _len_trans, 0);
- if (nSend < 0) {
- ESP_LOGE(TAG, "Failed to send data");
- }
- else {
- ESP_LOGI(TAG, "Data sent successfully");
- }
- // Set timeout for recv function
- struct timeval timeout;
- timeout.tv_sec = 5; // 5 second timeout
- timeout.tv_usec = 0;
- if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
- ESP_LOGE(TAG, "Failed to set socket receive timeout");
- }
- int bytes_received = recv(sock, _frame, _len_recv, 0);
- if (bytes_received < 0) {
- ESP_LOGE(TAG, "Failed to receive data: %i - %s", errno, strerror(errno));
- }
- if (bytes_received == 0) {
- ESP_LOGI(TAG, "Server closed connection");
- }
- if (bytes_received > 0) {
- ESP_LOGI(TAG, "Data received successfully");
- }
- if (sock != -1)
- {
- shutdown(sock, 0);
- close(sock);
- }