Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

636 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