Hi,
the code aim is too display ip address of name passed in command line but the code is unable to link up
I am using dev-c++ and i have added library wsock32 and ws_s32
still it give link error for gai_strerror and inet_ntop
here is the code:
Code:
01 /*
02 ** showip.c -- show IP addresses for a host given on the command line
03 */
04 #include <stdio.h>
05 #include <string.h>
06 #include <windows.h>
07 #include <winsock2.h>
08 #include <ws2tcpip.h>
09
10
11
12
13 int main(int argc, char *argv[])
14 {struct addrinfo hints, *res, *p;
15 int status;
16 char ipstr[INET6_ADDRSTRLEN];
17 WSADATA wsaData; // if this doesn't work
18 //WSAData wsaData; // then try this instead
19 // MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
20 if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
21 fprintf(stderr, "WSAStartup failed.\n");
22 exit(1);
23 }
24 if (argc != 2) {
25 fprintf(stderr,"usage: showip hostname\n");
26 return 1;
27 }
28 memset(&hints, 0, sizeof hints);
29 hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
30 hints.ai_socktype = SOCK_STREAM;
31 if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
32 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
33 return 2;
34 }
35 printf("IP addresses for %s:\n\n", argv[1]);
36 for(p = res;p != NULL; p = p->ai_next) {
37 void *addr;
38 char *ipver;
39 // get the pointer to the address itself,
40 // different fields in IPv4 and IPv6:
41 if (p->ai_family == AF_INET) { // IPv4
42 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
43 addr = &(ipv4->sin_addr);
44 ipver = "IPv4";
45 } else { // IPv6
46 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
47 addr = &(ipv6->sin6_addr);
48 ipver = "IPv6";
49 }
50 // convert the IP to a string and print it:
51 inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
52 printf(" %s: %s\n", ipver, ipstr);
53 }
54 freeaddrinfo(res); // free the linked list
55 WSACleanup();
56 return 0;
57 }
Any idea what i am missing???