kroiz
kroiz

Reputation: 1772

Tcp accept fails after first connection after 1 hour

I have written C++ client server application and the server is crashing. The scenario

  1. Start server
  2. 1 hour later (not before) Client connect

Then the Server which is waiting in accept returns -1 with errno "Too many open files".

Nothing else special is running on the machine which led me to believe that accept is opening many file descriptors while waiting. Is that true? How can I fix this so the client could connect anytime?

the relevant server code:

int sockClient;
while (true) {

    sockaddr_in* clientSockAddr = new sockaddr_in();
    socklen_t clientSockAddrLen = sizeof(sockaddr_in);

    sockClient = accept(sockServer, (sockaddr *) clientSockAddr,
                        &clientSockAddrLen);

    if(sockClient == -1 ){
        std::ostringstream s;
        s << "TCP Server: accept connection error." << std::strerror(errno);
        throw runtime_error(s.str());
    }

    connection->communicate(sockClient, clientSockAddr, clientSockAddrLen);
}

Upvotes: 0

Views: 171

Answers (1)

user207421
user207421

Reputation: 310860

You have a file descriptor leak somewhere. Possibly you aren't closing accepted sockets when you've finished with them, or else it's on a file somewhere.

Upvotes: 1

Related Questions