Pages

Showing posts with label host. Show all posts
Showing posts with label host. Show all posts

September 5, 2013

Host name to IP address

What's the IP address of that host? Use this convenient C method to check it out.
Although it's written in C, it works also in Objective-C and so in all your iOS and Mac OS projects.


/** Converts a host name to IP address.

 Actual return data will be stored in the two parameters "addrp" and "familyp".

 @param name The host name to convert.
 @param addrp A pointer to a in_addr struct. This parameter will be overwritten with actual IP address.
 @param familyp Address family (must be AF_INET). This parameter could be overwitten with actual address family type.

 */
void host2addr(char *name, struct in_addr *addrp, short *familyp) {
    struct hostent *hp;
    if ((hp=gethostbyname(name))) {
        bcopy(hp->h_addr,(char *)addrp,hp->h_length);
        if (familyp) *familyp = hp->h_addrtype;
    } else if ((addrp->s_addr=inet_addr(name)) != -1) {
        if (familyp) *familyp = AF_INET;
    } else {
        fprintf(stderr, "Unknown host : %s\n",name);
        exit(1);
    }
}



Download the ready-for-use source file (.m) of Host name to IP address