Bind error while recreating socket

Somewhere in the kernel, there’s still some information about your previous socket hanging around. Tell the kernel that you are willing to re-use the port anyway:

int yes=1;
//char yes="1"; // use this under Solaris

if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
    perror("setsockopt");
    exit(1);
}

See the bind() section in beej’s Guide to Network Programming for a more detailed explanation.

Leave a Comment