I'm using lwip to receive data from a remote host. This works fine, but sometimes the connection goes down, so lwip_read returns -1 (error 113, Software caused connection abort). If this happens, I'm closing the socket I read.
After this happens sometimes, accept returns -1 (error 23, Too many open files in system).
Now I'm wondering, what to do to avoid the error of accept. What else but closing the socket can I do?
In order to make clearer what I'm doing, I striped down the logic into this pseudo code:
Code: Select all
for (;;)
{
fd_set read_fds;
memcpy(&read_fds, ¤t_fds, sizeof(read_fds));
int triggered_nfds = select(highest_socket + 1, &read_fds, NULL, NULL, &timeout);
if (triggered_nfds > 0) // more than zero sockets have data
{
if (FD_ISSET(listen_socket, &read_fds)) // check if listener socket has data
{
int socket = accept(listen_socket, (struct sockaddr *)NULL, (socklen_t *)NULL);
if (socket < 0)
{
// log error
break;
}
else
{
// set:
/* 10 second timeout */
/* enable sending keepalive probes for socket */
/* 180 sec idle before start sending probes */
/* 30 sec between probes */
/* Drop connection after 4 probes without response */
FD_SET(socket, ¤t_fds);
// save connection
}
triggered_nfds--;
}
for (each accepted connection)
{
if (FD_ISSET(connection_socket, &read_fds))
{
int recv_size = lwip_read(connection_socket, buffer, buffer_size);
if(recv_size < 0)
{
// log error
FD_CLR(connection_socket, ¤t_fds); // clear socket in buffer
lwip_close(connection_socket);
}else{
// do sth with buffer
}
triggered_nfds--;
}
}
}
Torsten