Hi,
I am using lwip/sockets.h library to create a TCP Server
to handle just one TCP client connection.
In listen() function I set the amount of TCP clients in queue to
to 0.
But, when I initiate the TCP Server, and try to connect with two TCP clients
at the same time with hercules TCP Client, I get both of them connected to my
TCP Server.
How can I handle this event to just get one of them connected?
I am using an freeRTOS task to set up my TCP server, and then
in the while loop, i used the blocking function: accept().
The while loop doesnt have a delay to execute, but even with a delay
it doesnt work the way I want.
Thank you for your time.
How to establish TCP client's limit in tcp server socket in ESP32?
-
- Posts: 4
- Joined: Thu Aug 05, 2021 10:38 pm
-
- Posts: 9746
- Joined: Thu Nov 26, 2015 4:08 am
Re: How to establish TCP client's limit in tcp server socket in ESP32?
Your capslock seems to have been accidentally stuck on while typing the title of this topic. I took the liberty to correct that for you. Also, can you post your code somewhere? Your description isn't super clear.
-
- Posts: 4
- Joined: Thu Aug 05, 2021 10:38 pm
Re: How to establish TCP client's limit in tcp server socket in ESP32?
- err = listen(listen_sock, 1); // listen() function
- if (err != 0) {
- ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);
- goto CLEAN_UP;
- }
- while (1) { // while of freeRTOS task without delay
- struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
- uint addr_len = sizeof(source_addr);
- int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);
- if (sock < 0) {
- ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
- break;
- }
- // Convert ip address to string
- if (source_addr.ss_family == PF_INET) {
- inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
- } else if (source_addr.ss_family == PF_INET6) {
- inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
- }
- ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
- do_retransmit(sock);
- shutdown(sock, 0);
- close(sock);
- }
- CLEAN_UP:
- close(listen_sock);
- vTaskDelete(NULL);
- }
Who is online
Users browsing this forum: Baidu [Spider], BipinDangwal, Google [Bot], MicroController and 118 guests