cplusplus.com
C++ : Forum : UNIX/Linux Programming : Host name to IP Address (vice-versa)
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


solved Host name to IP Address (vice-versa)

kevinchkin (448)
Hi Guys,

How can I convert hostname to IP address?
localhost ---> 127.0.0.1

Thanks in advance
kevinchkin (448)
Never mind found it. In case someone else is looking for it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string returnVal;
     char   temp[255];
 
      if ((gethostname(temp,sizeof(temp))) == -1) {
         perror("gethostname");
         exit(1);
     }
 
     const hostent* host_info = NULL ;
     host_info = gethostbyname(temp) ;
 
     if (host_info) {
         const in_addr* address = (in_addr*)host_info->h_addr_list[0] ;
         memset(temp,NULL,sizeof(temp)) ;
         strcpy(temp,inet_ntoa(*address)) ;
     }
 
     returnVal = temp;
    return returnVal ;

Topic archived. No new replies allowed.