Problem with ESP32 TCP Server

mustray
Posts: 6
Joined: Wed Dec 19, 2018 6:01 am

Problem with ESP32 TCP Server

Postby mustray » Wed Jun 19, 2019 5:57 am

Hi All,
I am working with esp32 in station mode as a TCP server. I want 6 TCP server to run simultaneously. But the problem is i see 7 tcp clients connect through the hercules terminal. In listen() i have kept the backlog value as -1. But still the 7 client connects.
In my while loop am only calling accept() 6 times. I dont see any data for the 7 client unless i disconnect one of the 6 clients connected to my servers. I dont want the 7th client to get connected.

Any help is appreciated.

mikemoy
Posts: 626
Joined: Fri Jan 12, 2018 9:10 pm

Re: Problem with ESP32 TCP Server

Postby mikemoy » Thu Jun 20, 2019 6:01 am

How can we help if we cant see your code ?

mustray
Posts: 6
Joined: Wed Dec 19, 2018 6:01 am

Re: Problem with ESP32 TCP Server

Postby mustray » Thu Jun 20, 2019 12:04 pm

@mike Thanks for your concern.
Here is the code i have written

listener = socket(addr_family, SOCK_STREAM, ip_protocol);
bind(listener, (struct sockaddr *)&destAddr, sizeof(destAddr));
if (listen(listener, -1) == -1)
{
ESP_LOGI(TAG_TCP, "Listen ERROR! ");
}
while(1)
{
if(total_clients_accepted < 6)
{
addrlen = sizeof remoteaddr;
newfd = accept(listener,(struct sockaddr *)&remoteaddr,&addrlen);
if (newfd == -1)
{
ESP_LOGI(TAG_TCP, "Accept New Connection");
}
else
{
FD_SET(newfd, &master);
if (newfd > fdmax)
{
fdmax = newfd;
}
ESP_LOGI(TAG_TCP, "New connection from %s on socket %c\n",inet_ntop(remoteaddr.ss_family,get_in_addr((struct sockaddr*)&remoteaddr),remoteIP, INET6_ADDRSTRLEN),newfd);
total_clients_accepted += 1;
ESP_LOGI(TAG_TCP, " Total Clients Connected = %d", total_clients_accepted);
}
}
}
Please let me know.

Also i have set the setsockopt() for internal keepalvies, but it is also not working.
int keepAlive = 1;
int keepInterval = 1000;
setsockopt (listener, IPPROTO_TCP, TCP_KEEPALIVE, (void*) &keepAlive, sizeof (keepAlive));
setsockopt (listener, IPPROTO_TCP, TCP_KEEPINTVL, (void*) &keepInterval, sizeof (keepInterval));
Please let me know what needs to be corrected.

mustray
Posts: 6
Joined: Wed Dec 19, 2018 6:01 am

Re: Problem with ESP32 TCP Server

Postby mustray » Sat Jun 22, 2019 9:14 am

?

Who is online

Users browsing this forum: cdollar and 135 guests