[Webinar] Streamline your web hosting managementRegister Today


convert actual address"www" to IP

Posted on 2004-10-29
Medium Priority
Last Modified: 2006-11-17
How can i convert a simple address like www.in.gr to IP address x.x.x.x format?
A little tut through the needed functions like inet_ntoa would be cool, because i can't figure this out by myself.
Question by:skiritis13
  • 2

Accepted Solution

gugario earned 60 total points
ID: 12444188
Here you go, I ran into that problem recently.. what you want to use is gethostbyname().. some of these includes might be overkill, but that's all I used in my program.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netdb.h>

struct in_addr* resolve_addr(char *host)
        struct hostent *h;
        static struct in_addr addr;
        //if host name empty, return null
        if (host == NULL || strlen(host) == 0)
                return NULL;

        //check to see if value entered was an IP
        addr.s_addr = inet_addr(host);
        if (addr.s_addr != -1)
                return &addr;

        //check to see if value entered was a host name
        h = gethostbyname(host);
        if (h!= NULL)
                return (struct in_addr *) *h->h_addr_list;

        //if neither worked, return NULL
        return NULL;

Author Comment

ID: 12452107
What i was really looking for is this in "for windows" format because currently i can't run linux.
Anyway i made it win-ed my self( the code is the same, i just changed the headers ) but gethostbyname keeps returning null.I run into the msdn and it was very clear to use getaddrinfo in the place of gethostbyname. Any point on how to use getaddrinfo and the struct addrinfo or should i stick with the above code block?
Again: in the above gethostbyname keeps returning NULL.

Author Comment

ID: 12452139
Anyway i just made it work.Thanx for the code!

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question