link error for gai_strerror and inet_ntop

This is a discussion on link error for gai_strerror and inet_ntop within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, the code aim is too display ip address of name passed in command line but the code is unable ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    link error for gai_strerror and inet_ntop

    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???

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Indentation. You are missing a whole helluva lot of indentation.

    Other than crappy coding, it works for me. (Gentoo Linux, 2.6.31 - with modifications to remove the WinSock stuff, of course)

    ipver should be a const char *, and you need indentation. What fails on your system? (What symptom do you see that makes you think it doesn't work?)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Straight from your first thread:

    Quote Originally Posted by fronty
    Have you tried library MSDN tells to use?

    And frigging indent your code. Nobody can read that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 05:23 AM
  3. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 08:13 PM
  4. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21