accept function always returns EWOULDBLOCK
Posted: Wed Nov 27, 2019 3:36 pm
Hi,
I am running the V4 Beta version of the Espressif IDF and am attempting to use an ESP32 as a socket server to send data to a C# dotnet client program. The socket server is operating in unblocked mode as there are a number of threads running (a separate thread is executing, receiving data from an external sensor). The thread that initiates the socket on the ESP32, binds, listens and then awaits on the accept is as follows:
with, at the moment a small POC C# client code:
The output from the ESP32 monitor provides me with the IP Address of the ESP32, and when I receive the first "Awaiting connection" message from the ESP I trigger the C# program in debug.
The C# program appears to connect successfully (tempSocket.Connected is true and the tempSocket object does have the correct IP Address and Port values), but, even though the C# client believes it is connected, I continue to get the "Awaiting Connection" mesage from the ESP32, indicating that the accept function is still returning errno == EWOULDBLOCK.
Any help or insight would be gratefully received.
Thanks
I am running the V4 Beta version of the Espressif IDF and am attempting to use an ESP32 as a socket server to send data to a C# dotnet client program. The socket server is operating in unblocked mode as there are a number of threads running (a separate thread is executing, receiving data from an external sensor). The thread that initiates the socket on the ESP32, binds, listens and then awaits on the accept is as follows:
- void SocketListenConnectTask (void *pvParamters)
- {
- char addr_str[128];
- int addr_family;
- int ip_protocol;
- struct sockaddr_in dest_addr;
- dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- dest_addr.sin_family = AF_INET;
- dest_addr.sin_port = htons(PORT);
- addr_family = AF_INET;
- ip_protocol = IPPROTO_TCP;
- inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str)-1);
- int listen_socket = socket(addr_family, SOCK_STREAM, ip_protocol);
- if (listen_socket < 0)
- {
- ESP_LOGE(TAG, "Unable to create socket: Error No: %d", errno);
- vTaskDelete(NULL);
- }
- // Mark socket as non blocking.
- int status = fcntl(listen_socket, F_SETFL, fcntl(listen_socket, F_GETFL, 0) | O_NONBLOCK);
- if (status == -1)
- {
- ESP_LOGE(TAG, "Error setting non-blocking status: %s", strerror(errno));
- vTaskDelete(NULL);
- }
- int err = bind(listen_socket, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
- if (err != 0)
- {
- ESP_LOGE(TAG, "Socket unable to bind: Error No: %s", strerror(errno));
- close(listen_socket);
- vTaskDelete(NULL);
- }
- err = listen(listen_socket, 1);
- if (err != 0)
- {
- ESP_LOGE(TAG, "Socket unable to listen: Error No: %s", strerror(errno));
- close(listen_socket);
- vTaskDelete(NULL);
- }
- else
- {
- while (1)
- {
- bool client_connected = false;
- // Listening for Client connection.
- while (!client_connected)
- {
- struct sockaddr source_addr;
- uint addr_len = sizeof(source_addr);
- telnet_socket = accept(listen_socket, (struct sockaddr *)&source_addr, &addr_len);
- if (errno == EWOULDBLOCK)
- {
- ESP_LOGI(TAG, "Awaiting connection");
- vTaskDelay(100);
- }
- else if (telnet_socket < 0)
- {
- ESP_LOGE(TAG, "Unable to accept connection %s", strerror(errno));
- vTaskDelay(100); //vTaskDelete(NULL);
- }
- else
- {
- ESP_LOGI(TAG, "Connected");
- client_connected = true;
- }
- }
- telnetClientConnected = true;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Byte[] bytesReceived = new Byte[256];
- IPAddress iPAddress = IPAddress.Parse(args[0]);
- IPEndPoint ipEndPoint = new IPEndPoint(iPAddress, 23);
- Socket tempSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- tempSocket.Connect(ipEndPoint);
- if (tempSocket.Connected)
- {
- int bytes = 0;
- do
- {
- bytes = tempSocket.Receive(bytesReceived, bytesReceived.Length, 0);
- } while (bytes > 0);
- }
- }
- }
The C# program appears to connect successfully (tempSocket.Connected is true and the tempSocket object does have the correct IP Address and Port values), but, even though the C# client believes it is connected, I continue to get the "Awaiting Connection" mesage from the ESP32, indicating that the accept function is still returning errno == EWOULDBLOCK.
Any help or insight would be gratefully received.
Thanks