I'm writing a TCP server in C and find something unusual happens once the listening fd get "Too many open files" error. The `accept` call doesn't block anymore and returns -1 all the time.

I also tried closing the listening fd and re-opening, re-binding it, but didn't seem to work.

My questions are why `accept` keeps returning -1 in this situation, what am I supposed to do to stop it ~~and make the server be able to accept new connections after any old clients closed~~? (the socket is of course able to `accept` correctly again when some connections closed)

====== UPDATE: clarification ======

The problem occurs just because the number of active clients is more than the limit of open fds, so I don't `close` any of the accepted fds in the sample code, just to make it reproduce more quickly.

I add the timestamp each time `accept` returns to the output and slow down `connect` frequency to once in 2 seconds, then I find that in fact the "Too many open files" error occurs immediately after the lastest success `accept`. So I think that is because when the maxium fds is reached, each call to `accept` will return immediately, and the return value is -1. (What I thought is that `accept` would still block, but returns -1 at the next incoming `connect`. The behavior of `accept` in this situation is my own theory, not from the man page. If it's wrong, please let me know).

So to my second question, to make it stop, I think it's a solution that stop to call `accept` before any connection is `close`d.

Also update the sample codes. Thanks for your help.

====== Sample codes ======

Here is how I test it. First set `ulimit -n` to a low value (like 16) and run the server program compiled from the following C source; then use the Python script to create several connections

```
/* TCP server; bind :5555 */

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUFSIZE 1024
#define PORT 5555

void error(char const* msg)
{
    perror(msg);
    exit(1);
}

int listen_port(int port)
{
    int parentfd; /* parent socket */
    struct sockaddr_in serveraddr; /* server's addr */
    int optval; /* flag value for setsockopt */
    parentfd = socket(AF_INET, SOCK_STREAM, 0);
    if (parentfd < 0) {
        error("ERROR opening socket");
    }

    optval = 1;
    setsockopt(parentfd, SOL_SOCKET, SO_REUSEADDR, 
           (const void *)&optval , sizeof(int));

    bzero((char *) &serveraddr, sizeof(serveraddr));

    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddr.sin_port = htons((unsigned short)port);

    if (bind(parentfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0) {
        error("ERROR on binding");
    }

    if (listen(parentfd, 5) < 0) {
        error("ERROR on listen");
    }
    printf("Listen :%d\n", port);
    return parentfd;
}

int main(int argc, char **argv)
{
    int parentfd; /* parent socket */
    int childfd; /* child socket */
    int clientlen; /* byte size of client's address */
    struct sockaddr_in clientaddr; /* client addr */
    int accept_count; /* times of accept called */

    accept_count = 0;
    parentfd = listen_port(PORT);

    clientlen = sizeof(clientaddr);

    while (1) {
        childfd = accept(parentfd, (struct sockaddr *) &clientaddr, (socklen_t*) &clientlen);
        printf("accept returns ; count=%d ; time=%u ; fd=%d\n", accept_count++, (unsigned) time(NULL), childfd);
        if (childfd < 0) {
            perror("error on accept");

            /* the following 2 lines try to close the listening fd and re-open it */
            // close(parentfd);
            // parentfd = listen_port(PORT);

            // the following line let the program exit at the first error
            error("--- error on accept");
        }
    }
}

```

The Python program to create connections

```
import time
import socket

def connect(host, port):
    s = socket.socket()
    s.connect((host, port))
    return s

if __name__ == '__main__':
    socks = []

    try:
        try:
            for i in xrange(100):
                socks.append(connect('127.0.0.1', 5555))
                print ('connect count: ' + str(i))
                time.sleep(2)
        except IOError as e:
            print ('error: ' + str(e))
        print ('stop')
        while True:
            time.sleep(10)
    except KeyboardInterrupt:
        for s in socks:
            s.close()

```