C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 10-21-2008, 07:10 AM   #1
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 198
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
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Last edited by pobri19; 10-21-2008 at 07:30 AM.
pobri19 is offline   Reply With Quote
Old 10-21-2008, 07:39 AM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,831
MSDN says:
Quote:
Header Declared in Ws2tcpip.h.
So there you go.
tabstop is online now   Reply With Quote
Old 10-21-2008, 03:42 PM   #3
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 198
Tried that, didn't work. It gets the same errors as Winsock2.h
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Old 10-21-2008, 04:56 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,831
Found on msdn:
Quote:
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.
tabstop is online now   Reply With Quote
Old 10-21-2008, 05:31 PM   #5
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 198
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.
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Old 10-22-2008, 02:18 AM   #6
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 198
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?
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Old 10-22-2008, 05:29 AM   #7
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,059
Add #include <ws2tcpip.h> to your code
BobS0327 is offline   Reply With Quote
Old 10-22-2008, 07:21 AM   #8
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 198
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
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Old 10-22-2008, 10:05 AM   #9
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,059
Link with WS2_32.lib
BobS0327 is offline   Reply With Quote
Old 10-22-2008, 10:07 AM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,831
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:
Quote:
Library Use Ws2_32.lib.
tabstop is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Still confused why working set larger than virtual memory George2 Tech Board 4 01-13-2008 02:14 AM
trouble getting structure variables to work across functions cpudaman C++ Programming 13 12-14-2007 03:34 PM
Switch structure not working him61 C++ Programming 8 05-23-2007 05:35 PM
structure ...solution plz???? hegdeshashi C Programming 4 07-24-2006 09:57 AM
How to call a function several times, but with a different structure as argument? mabuhay C Programming 5 02-14-2006 09:04 AM


All times are GMT -6. The time now is 11:54 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