Home > other >  Close connection with client after inactivty period
Close connection with client after inactivty period

Time:01-20

I'm currently managing a server that can serve at most MAX_CLIENTS clients concurrently.

This is the code I've written so far:

//create and bind listen_socket_

struct pollfd poll_fds_[MAX_CLIENTS];

for (auto& poll_fd: poll_fds_)
{
    poll_fd.fd = -1;
}

listen(listen_socket_, MAX_CLIENTS);

poll_fds_[0].fd = listen_socket_;
poll_fds_[0].events = POLLIN;

while (enabled)
    {
        const int result = poll(poll_fds_, MAX_CLIENTS, DEFAULT_TIMEOUT);

        if (result == 0)
        {
            continue;
        }
        else if (result < 0)
        {
            // throw error
        }
        else
        {
            for (auto& poll_fd: poll_fds_)
            {
                if (poll_fd.revents == 0)
                {
                    continue;
                }
                else if (poll_fd.revents != POLLIN)
                {
                    // throw error
                }
                else if (poll_fd.fd == listen_socket_)
                {
                    int new_socket = accept(listen_socket_, nullptr, nullptr);

                    if (new_socket < 0)
                    {
                        // throw error
                    }
                    else
                    {

                        for (auto& poll_fd: poll_fds_)
                        {
                            if (poll_fd.fd == -1)
                            {
                                poll_fd.fd = new_socket;
                                poll_fd.events = POLLIN;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    // serve connection
                }
            }
        }
    }

Everything is working great, and when a client closes the socket on its side, everything gets handled well.

The problem I'm facing is that when a client connects and send a requests, if it does not close the socket on its side afterwards, I do not detect it and leave that socket "busy".

Is there any way to implement a system to detect if nothing is received on a socket after a certain time? In that way I could free that connection on the server side, leaving room for new clients.

Thanks in advance.

CodePudding user response:

You could close the client connection when the client has not sent any data for a specific time.

For each client, you need to store the time when the last data was received.

Periodically, for example when poll() returns because the timeout expired, you need to check this time for all clients. When this time to too long ago, you can shutdown(SHUT_WR) and close() the connection. You need to determine what "too long ago" is.

If a client does not have any data to send but wants to leave the connection open, it could send a "ping" message periodically. The server could reply with a "pong" message. These are just small messages with no actual data. It depends on your client/server protocol whether you can implement this.

  •  Tags:  
  • Related