Does sin_addr.s_addr = INADDR_ANY; need htonl at all?

前端 未结 7 1794
一整个雨季
一整个雨季 2020-12-06 03:56

I came across two threads:

Socket with recv-timeout: What is wrong with this code?

Reading / Writing to a socket using a FILE stream in c

one uses

7条回答
  •  被撕碎了的回忆
    2020-12-06 04:20

    Neither is right, in the sense that both INADDR_ANY and htonl are deprecated, and lead to complex, ugly code that only works with IPv4. Switch to using getaddrinfo for all of your socket address creation needs:

    struct addrinfo *ai, hints = { .ai_flags = AI_PASSIVE|AI_ADDRCONFIG };
    getaddrinfo(0, "1234", &hints, &ai);
    

    Replace "1234" with your port number or service name.

提交回复
热议问题