Thread: addrinfo Structure Not Working

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    addrinfo Structure Not Working

    I'm trying to learn networking in C++, but whenever I compile this I get a bunch of 'undeclared identifiers' etc. If I compile it with <winsock2.h> I get about 86 errors, and when I click on them it takes me to the actual winsock2.h header file. I've included winsock32.lib and wsock32.lib in my linker options too.. Here's what I have so far:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <windows.h>
    #include <winsock.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    WSADATA wsaData;
    
    int main() {
    	struct addrinfo hints, *servinfo = NULL;
    	int wsaReturnVal = WSAStartup(MAKEWORD(1, 1), &wsaData);
    	if (wsaReturnVal) {
    		cout << "Error number: " << wsaReturnVal << endl;
    		exit(1);
    	}
    	memset(&hints, 0, sizeof(hints));
    	hints.ai_flags = AI_PASSIVE;
    	hints.ai_family = AF_UNSPEC;
    	hints.ai_socktype = SOCK_STREAM;
    
    	int status = getaddrinfo(NULL, "3490", &hints, &servinfo);
    	if (status == -1) {
    		cout << "getaddrinfo error." << endl;
    		exit(1);
    	}
    
    	freeaddrinfo(servinfo);
    
    	if (WSACleanup()) {
    		cout << "Cleanup Error." << endl;
    		exit(1);
    	}
    }
    The errors I get for this compile are:

    Error 1 error C2079: 'hints' uses undefined struct 'main::addrinfo' 13
    Error 2 error C2228: left of '.ai_flags' must have class/struct/union 20
    Error 3 error C2065: 'AI_PASSIVE' : undeclared identifier 20
    Error 4 error C2228: left of '.ai_family' must have class/struct/union 21
    Error 5 error C2228: left of '.ai_socktype' must have class/struct/union 22
    Error 6 error C3861: 'getaddrinfo': identifier not found 24
    Error 7 error C3861: 'freeaddrinfo': identifier not found 30

    Thanks.

    EDIT: Here's a screenshot of winsock2.h errors.. http://img56.imageshack.us/my.php?im...5112335jf3.jpg
    Last edited by pobri19; 10-21-2008 at 07:30 AM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    MSDN says:
    Header Declared in Ws2tcpip.h.
    So there you go.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Tried that, didn't work. It gets the same errors as Winsock2.h
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Found on msdn:
    The Winsock2.h header file internally includes core elements from the Windows.h header file, so there is not usually an #include line for the Windows.h header file in Winsock applications. If an #include line is needed for the Windows.h header file, this should be preceded with the #define WIN32_LEAN_AND_MEAN macro. For historical reasons, the Windows.h header defaults to including the Winsock.h header file for Windows Sockets 1.1. The declarations in the Winsock.h header file will conflict with the declarations in the Winsock2.h header file required by Windows Sockets 2.0. The WIN32_LEAN_AND_MEAN macro prevents the Winsock.h from being included by the Windows.h header.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Alright so just leave out the #include <windows.h> and put #include <winsock2.h> and I should be sweet? I'll try that when I get home and post if it worked. Thanks.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Alright that didn't work, I removed the Windows.h, and tried just using iostream and winsock.h, which produced the same undeclared identifiers errors, so I tried winsock2.h with iostream, which also produced the exact same errors. any ideas?
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Add #include <ws2tcpip.h> to your code

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Alright so now I have:

    Code:
    #include <iostream>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    These are the errors I'm getting...

    Code:
    Error	1	error LNK2001: unresolved external symbol __imp__freeaddrinfo@4	net.obj
    Error	2	error LNK2001: unresolved external symbol __imp__getaddrinfo@16	net.obj
    Error	3	fatal error LNK1120: 2 unresolved externals
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Link with WS2_32.lib

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are aware that you can search MSDN just as well as the rest of us? If you type in "getaddrinfo" and click on the result and go to the bottom you should see this:
    Library Use Ws2_32.lib.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  3. Switch structure not working
    By him61 in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2007, 05:35 PM
  4. structure ...solution plz????
    By hegdeshashi in forum C Programming
    Replies: 4
    Last Post: 07-24-2006, 09:57 AM
  5. Replies: 5
    Last Post: 02-14-2006, 09:04 AM