Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getaddrinfo error: ai_socktype not supported

struct addrinfo *myAddrinfo, *curMyAddrinfo, hint;
memset(&hint, 0, sizeof(struct addrinfo));
hint.ai_family = AF_INET;
hint.ai_protocol = AI_PASSIVE;
hint.ai_socktype = SOCK_STREAM;

const int code = getaddrinfo(NULL, SERVER_PORT, &hint, &myAddrinfo);
if ((code) != 0) {
    printf("getaddrinfo error occours: %s ",
            gai_strerror(code));
    return 1;
}

this gives the error: "ai_socktype not supported" if i comment out the hint.ai_protocol = AI_PASSIVE; it will get through, but i am wondering why it happens?

thanks for your time

like image 260
Nissan911 Avatar asked Jan 26 '26 22:01

Nissan911


2 Answers

It's worth just adding here, as this is the top result when searching for "ai_socktype not supported" an alternative reason for it could be that hints is not zeroed on the stack; for that you need

memset(&hints, 0, sizeof hints);

Nissan's code of course did have that already

like image 103
Mark Avatar answered Jan 29 '26 12:01

Mark


That's because AI_PASSIVE is referred to ai_flags field, (not ai_protocol). Try :

hint.ai_flags = AI_PASSIVE;

And have a look at addrinfo structure.

like image 39
Heisenbug Avatar answered Jan 29 '26 11:01

Heisenbug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!