cserverfile-descriptorshutdownposix-select

Should I call shutdown() on fds before exit from server?


When the server receives a termination signal it exits from the loop where the select() is monitoring the fds in the set (fd_set).

It is necessary look through fds and call shutdown(fd, SHUT_RDWR) if there are any of them still in the set? Or should I call close(fd)?


Solution

  • It's not necessary to call shutdown() before close(). When you close a socket, it's automatically shut down in both directions.

    You generally only need to use shutdown() if you need to keep the socket open for some reason. This might be done in a protocol where the end of the request is indicated by EOF; you call shutdown(fd, SHUT_WR) to send EOF, then read the response.