Solved

convert actual address"www" to IP

Posted on 2004-10-29
198 Views
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.
Thanx.
0
Question by:skiritis13
    3 Comments
     
    LVL 1

    Accepted Solution

    by:
    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;
    }
    0
     

    Author Comment

    by:skiritis13
    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.
    0
     

    Author Comment

    by:skiritis13
    Anyway i just made it work.Thanx for the code!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now