There are a couple of things going on here. For the multiple results from getaddrinfo()
, that's because it returns an IPv4 address and a IPv6 address (you have both). As for the "address already in use", you need to set the SO_REUSEADDR
option. Put this between your calls to socket()
and bind()
:
int opval = 1;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));