C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 06-27-2008, 06:22 PM   #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( ?
sunjayc99 is offline   Reply With Quote
Old 06-28-2008, 07:33 AM   #2
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
No, because "result" is filled out by getaddrinfo and required by subsequent functions.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 06-28-2008, 03:03 PM   #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?
sunjayc99 is offline   Reply With Quote
Old 06-28-2008, 03:14 PM   #4
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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