MS 의 소스를 살펴보다가 평소에 알지 못하던 코드가 궁금하여(영어를 이해못했나?) 테스트 해봤습니다. 

    iResult = getaddrinfo(SeverAddress.c_str(), ServerPort, &hints, &result);

    if ( iResult != 0 ) 

{

        printf("getaddrinfo failed with error: %d\n", iResult);

        WSACleanup();

        return 1;

    }

// Attempt to connect to an address until one succeeds

for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) 

{

// Create a SOCKET for connecting to server

        socket_ = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);

        if (socket_ == INVALID_SOCKET)

{

m_dwLastError = WSAGetLastError();

WSACleanup();

return false;

}


// Connect to server.

iResult = ::connect(socket_, ptr->ai_addr, (int)ptr->ai_addrlen);

if (iResult == SOCKET_ERROR) 

{

closesocket(socket_);

socket_ = INVALID_SOCKET;

continue;

}

break;

} 


getaddrinfo 는 어떤식으로 동작할까나? MSDN 의 소스를 실행해보니

http://msdn.microsoft.com/en-us/library/windows/desktop/ms737530(v=vs.85).aspx

Calling getaddrinfo with following parameters: 

       nodename = www.google.com

        servname (or port) = HTTP


getaddrinfo returned success

getaddrinfo response 1

        Flags: 0x0

        Family: AF_INET (IPv4)

        IPv4 address 74.125.224.114

        Socket type: SOCK_STREAM (stream)

        Protocol: IPPROTO_TCP (TCP)

        Length of this sockaddr: 16

        Canonical name: (null)

getaddrinfo response 2

        Flags: 0x0

        Family: AF_INET (IPv4)

        IPv4 address 74.125.224.113

        Socket type: SOCK_STREAM (stream)

        Protocol: IPPROTO_TCP (TCP)

        Length of this sockaddr: 16

        Canonical name: (null)

getaddrinfo response 3

        Flags: 0x0

        Family: AF_INET (IPv4)

        IPv4 address 74.125.224.116

        Socket type: SOCK_STREAM (stream)

        Protocol: IPPROTO_TCP (TCP)

        Length of this sockaddr: 16

        Canonical name: (null)

getaddrinfo response 4

        Flags: 0x0

        Family: AF_INET (IPv4)

        IPv4 address 74.125.224.115

        Socket type: SOCK_STREAM (stream)

        Protocol: IPPROTO_TCP (TCP)

        Length of this sockaddr: 16

        Canonical name: (null)

getaddrinfo response 5

        Flags: 0x0

        Family: AF_INET (IPv4)

        IPv4 address 74.125.224.112

        Socket type: SOCK_STREAM (stream)

        Protocol: IPPROTO_TCP (TCP)

        Length of this sockaddr: 16

        Canonical name: (null)

Press any key to continue . . .


www.google.com 도메인이 지칭하는 아이피 주소들 중에 하나씩 접속을 시도해보기 위한 코드였습니다. 

참고로 www.google.com 의 domain 정보는

C:\>nslookup www.google.com
Server:  hq-dc01-b.nexon.net

Address:  10.1.100.10


Non-authoritative answer:

Name:    www.google.com

Addresses:  2607:f8b0:4005:802::1011

          74.125.224.113

          74.125.224.116

          74.125.224.115

          74.125.224.112

          74.125.224.114



+ Recent posts