engdavidiogo commented on issue #16044: URL: https://github.com/apache/nuttx/issues/16044#issuecomment-2754960183
Sorry for the delay! @acassis, thank you for the questions. To answer them, I used a code sample attached at the end of this comment. Here’s what I found: 1. When I use the `select()` function on an **ESP32-C6** with the `esp32c6-devkitm:wifi` configuration, I observe the same behavior where `select()` never returns. Evidence below:  2. When I use the `select()` function in **POSIX *(Ubuntu)*** systems with the `sim:tcpblaster` configuration, everything seems to work as expected! Evidence below:  3. Unfortunately, I don’t currently have another board with the necessary features for testing, but I can arrange one if needed. --- Example code: ```c #include <stdio.h> #include <string.h> #include <stdbool.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <sys/socket.h> #include <arpa/inet.h> #include <pthread.h> #include <netinet/in.h> #include <sys/select.h> #define SERVER_PORT 80 #define MAX_BUFFER 1024 static int server_sock = -1; static volatile bool server_running = true; static void handle_client(int client_sock) { char buffer[MAX_BUFFER]; ssize_t nbytes = recv(client_sock, buffer, MAX_BUFFER - 1, 0); if (nbytes <= 0) { close(client_sock); return; } buffer[nbytes] = '\0'; if (strstr(buffer, "GET /state") != NULL) { const char *response_json = "{\"status\":\"ok\"}\r\n"; char response[MAX_BUFFER]; snprintf(response, sizeof(response), "HTTP/1.1 200 OK\r\n" "Content-Type: application/json\r\n" "Content-Length: %zu\r\n" "Connection: close\r\n\r\n" "%s", strlen(response_json), response_json); send(client_sock, response, strlen(response), 0); } else { const char *not_found = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"; send(client_sock, not_found, strlen(not_found), 0); } close(client_sock); } static void *webserver_thread(void *arg) { struct sockaddr_in server_addr, client_addr; socklen_t client_len = sizeof(client_addr); server_sock = socket(AF_INET, SOCK_STREAM, 0); if (server_sock < 0) { perror("Error creating server socket"); return NULL; } int opt = 1; setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { perror("Error binding socket"); close(server_sock); return NULL; } if (listen(server_sock, 5) < 0) { perror("Error starting listen"); close(server_sock); return NULL; } printf("HTTP Server started on port %d\n", SERVER_PORT); while (server_running) { fd_set read_fds; struct timeval timeout; FD_ZERO(&read_fds); FD_SET(server_sock, &read_fds); timeout.tv_sec = 1; timeout.tv_usec = 0; int ret = select(server_sock + 1, &read_fds, NULL, NULL, &timeout); printf("value returned by select -> %d\n", ret); if (ret < 0) { perror("select failed"); break; } else if (ret == 0) { continue; // Timeout } if (FD_ISSET(server_sock, &read_fds)) { int client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &client_len); if (client_sock >= 0) { handle_client(client_sock); } } } close(server_sock); return NULL; } int main(int argc, char *argv[]) { pthread_t tid; pthread_create(&tid, NULL, webserver_thread, NULL); pthread_detach(tid); while (1) { sleep(60); } return 0; } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
