so I wrote this Server/Client applications that communicate with each other over send() and recv(). It’s written in C. The Server can handle multiple connection over threads. I have a Ping function that checks every 90 seconds if the Clients are still alive. Now my problem is, if one of the Clients crashes the CPU on the Server-Side instantly uses 100%. This is my do while loop that waits for incoming commands of the client and ends if the string end is being send. But if the Client crashes there is no end string coming and I think it keeps spinning around in this loop.
do
{
TCP_recv (&client, buffer, BUF-1);
if (!strcmp(buffer,"mysql"))
{
TCP_recv (&client, buffer, BUF-1);
mysql_real_query(my, buffer, strlen(buffer));
syslog(LOG_INFO,"%s",buffer);
TCP_send (&client, "Server got the MySQL string!", BUF-1);
}
}
while (strcmp(buffer,"end"));
Has anyone got an idea how the server can instantly react on that and not loop to death?
PS: If my kill-all-clients-who-not-respond function kicks in and closes the socket the CPU usage instantly returns to normal.
Typically this happens when using select() and the client has disconnected/crashed and you didnt remove the socket FD from those being checked. This result is that it will continuously iterate over the while loop without blocking on send()/recv(). Check that you are not trying to receive from the crashed client, by checking the return from recv() and/or send().