[ACCEPTED]-socket return 'No such file or directory-sockets

Accepted answer
Score: 13

Your main problem is that you have the wrong 10 check for when socket() has an error. socket() will 9 return -1 on error, not 0 on success. You 8 may be getting a good socket value (2, 3, etc.) and 7 treating it like an error.

There is also 6 a second problem in the way you are parenthesizing 5 your code. When you write:

if (sockfd = socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol) != 0)

That is being 4 treated as:

if (sockfd = (socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol) != 0))

So sockfd will not be assigned 3 the return value of socket, but the value 2 of comparing it against 0. Fixing both 1 problems, you should be writing:

if ((sockfd = socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol)) == -1)
Score: 0

I believe you should be specifying the type 2 of socket you want. When you say:

add_info.ai_family = AF_UNSPEC;

you should 1 be saying:

add_info.ai_family = AF_INET;

More Related questions