Socket programming issue with getaddrinfo()

Aug 21, 2009 at 10:10pm
I can't work out how to get the getaddrinfo() function to work. I keep getting the error:

error C2664: 'getaddrinfo' : cannot convert parameter 1 from 'char' to 'PCSTR'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

1
2
3
4
5
6
7

if(getaddrinfo(argv[1], port, &hints, &result) != 0) {
		std::cout << "Could not get address info!";
		WSACleanup();
		std::cin.get();
		return 1;
	}


The program compiles fine if this snippet isn't in it. Its where it says "port" that I seem to be getting the problem.
Aug 21, 2009 at 10:39pm
PCSTR is a char *, and it seems you are passing an actual character (and not a pointer).
Aug 22, 2009 at 12:07am
That shouldn't be a problem though because of the declaration of port:

char *port = "27015";

Sorry, should have said that before.
Last edited on Aug 22, 2009 at 12:42am
Aug 22, 2009 at 12:58pm
parameter 1. How is argv declared?
Aug 22, 2009 at 1:04pm
plz post your main functions head...

did u do it that way?

main(int argc, char **argv)
Aug 22, 2009 at 4:18pm
No i did it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma comment(lib, "wsock32.lib")

#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>

int main(int argc, char *argv)
{
	if(argc != 2) {
		std::cout << "Error! Usage: Client <hostname>";
		std::cin.get();
		return 1;
	}
	
	WSADATA wsaData;
	int iniResult = 0;
	char *port = "27015";

	iniResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if(iniResult != 0) {
		std::cout << "WSAStartup() failed to initialize!";
		std::cin.get();
		return 1;
	}
	
	struct addrinfo *result = NULL,
                *ptr = NULL,
                hints;

	SecureZeroMemory(&hints, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	if(getaddrinfo(argv[1], port, &hints, &result) != 0) {
		std::cout << "Could not get address info!";
		WSACleanup();
		std::cin.get();
		return 1;
	}

	
	std::cin.get();
	return 0;
}
Aug 22, 2009 at 5:40pm
as i wrote in my recent post: it hast to be char** argv not *argv , because it is an array of pointers to c-strings which u enter while starting the programm via the command line.

"client" --- "hostname" [arg3] [argx...[
/\ _______ /\ ________ /\
||________||_________||
argv[0] argv[1] argv[2] etc...
Last edited on Aug 22, 2009 at 5:41pm
Aug 22, 2009 at 6:38pm
that didn't wok though :S

I get the error:

1>Client.obj : error LNK2019: unresolved external symbol __imp__getaddrinfo@16 referenced in function _main
1 unresolved externals
Last edited on Aug 22, 2009 at 6:39pm
Aug 22, 2009 at 7:21pm
guess u forgot to import the library/header...


wtf is wsock32.lib?
 
#pragma comment(lib, "Ws2_32") 
Last edited on Aug 22, 2009 at 7:26pm
Aug 22, 2009 at 9:40pm
Ok using your pragma comment let the code compile fine so thanks!

Btw, could you show me how to include this library without having to use pragma comment because I only used this because I'm new to using VC++ and don't know how to do it the other way - this was just a way I found whilst searching for a solution.
Topic archived. No new replies allowed.