How to make a socket address reuseable using setsockopt()?

Did you just get an “address already in use” error while calling bind()?

Use the following snippet to get rid of it:

int enable = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
    perror("setsockopt(SO_REUSEADDR) failed");
Written on April 12, 2022