아래 코드는 getaddrinfo 호출 후 받은 결과를 출력한다.
https://github.com/jinwookh/advanced-programming-in-unix/blob/master/chapter-16-network-ipc/getaddrinfo.c
Advanced Programming in the UNIX Environment 602p에 있는 코드를 그대로 복사했다.
./getaddrinfo localhost 80
host: localhost
flags: 0
family: inet6
type: datagram
protocol: UDP
host: localhost
flags: 0
family: inet6
type: stream
protocol: TCP
host: localhost
address: 127.0.0.1
port: 80
flags: 0
family: inet
type: datagram
protocol: UDP
host: localhost
address: 127.0.0.1
port: 80
flags: 0
family: inet
type: stream
protocol: TCPhost: localhost
flags: 0
family: inet6
type: datagram
protocol: UDP
host: localhost
flags: 0
family: inet6
type: stream
protocol: TCP
host: localhost
address: 127.0.0.1
port: 80
flags: 0
family: inet
type: datagram
protocol: UDP
host: localhost
address: 127.0.0.1
port: 80
flags: 0
family: inet
type: stream
protocol: TCP
localhost 80에 대해 4개의 addrinfo 객체가 반환됐다.
https://man7.org/linux/man-pages/man3/getaddrinfo.3.html
ai_family
This field specifies the desired address family for the
returned addresses. Valid values for this field include
AF_INETandAF_INET6. The value AF_UNSPEC indicates that
getaddrinfo() should return socket addresses for any
address family (either IPv4 or IPv6, for example) that can
be used withnode andservice.
ai_socktype
This field specifies the preferred socket type, for
example SOCK_STREAM or SOCK_DGRAM. Specifying 0 in this
field indicates that socket addresses of any type can be
returned bygetaddrinfo().
ai_protocol
This field specifies the protocol for the returned socket
addresses. Specifying 0 in this field indicates that
socket addresses with any protocol can be returned by
getaddrinfo().