is getaddrinfo really necessary?

This is a discussion on is getaddrinfo really necessary? within the Networking/Device Communication forums, part of the General Programming Boards category; Looking at the examples from MSDN for windows sockets 2, they give this: Code: ... ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_INET; ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    is getaddrinfo really necessary?

    Looking at the examples from MSDN for windows sockets 2, they give this:
    Code:
    ...
        ZeroMemory(&hints, sizeof(hints));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
        hints.ai_flags = AI_PASSIVE;
    
        // Resolve the server address and port
        iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
        if ( iResult != 0 ) {
            printf("getaddrinfo failed: %d\n", iResult);
            WSACleanup();
            return 1;
        }
    
        // Create a SOCKET for connecting to server
        ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
        if (ListenSocket == INVALID_SOCKET) {
            printf("socket failed: %ld\n", WSAGetLastError());
            freeaddrinfo(result);
            WSACleanup();
            return 1;
        }
    
        // Setup the TCP listening socket
        iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            printf("bind failed: %d\n", WSAGetLastError());
            freeaddrinfo(result);
            closesocket(ListenSocket);
            WSACleanup();
            return 1;
        }
    
        freeaddrinfo(result);
    ...
    my question is, is the getaddrinfo( really necessary? I mean, couldn't you just pass those values in hints directly to sockets( and bind( ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    No, because "result" is filled out by getaddrinfo and required by subsequent functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    sorry, i guess i didn't phrase my question correctly.
    what i meant was, couldn't i just use something like:
    Code:
    ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCPIP);
    and
    Code:
    bind(ListenSocket, myaddr, sizeof(myaddr));
    where myaddr is a struct sockaddr_in that i have already initialized?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    getaddrinfo looks up the hostname into the internal format used by Winsock (according to MSDN).
    If you've already initialized that hostname, then sure, but otherwise no.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why create a wrapper fo getaddrinfo()
    By Overworked_PhD in forum Linux Programming
    Replies: 2
    Last Post: 11-10-2007, 01:37 AM
  2. getaddrinfo()
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 07-24-2006, 03:41 AM
  3. using gethostbyname , getaddrinfo & WSAAsyncGetHostByName
    By hanhao in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-04-2004, 01:07 AM
  4. Resolving Hostname Using getaddrinfo(...) :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-03-2002, 09:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21