Hi,
I'm trying to launch the code from Beej's "Guide to Network Programming".
I tried to google, but I didn't find a solution. Other people were able to fix SIMILAR problems with adding some header files or a lib, but it didn't work for me.
The code looks like this(there are unnecessary headers, I know, but they are added just to rule out the possible header issue
) :
Code:
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <Ws2tcpip.h>
int main(int argc, char *argv[])
{
WSADATA wsaData; // if this doesn't work
//WSAData wsaData; // then try this instead
// MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: showip hostname\n");
return 1;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", argv[1]);
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res); // free the linked list
WSACleanup();
return 0;
}
Now I get inet_ntop undeclared(first use this in function).
I've tried to replace it with inetNtop, and PCTSTR WSAAPI InetNtop. I still get the same thing.
I've added 'libws2_32.a' and 'libsock32.a' under project->project options->parameters->add library or object in Dev-C++.
I know the InetNtop should be avoided in the first place, but I'm trying to understand network programming before I start with the alternatives.
Any idea whats wrong?