Obtaining your IP Address 
Author Message
 Obtaining your IP Address

I was wondering if anyone knows how to obtain the local IP address of a
machine. Is there a Winsock function to do this? (and I'm not talking about
using winipcfg, I want to be able to do what winipcfg does)

Thanks,
Ben



Wed, 10 Jan 2001 03:00:00 GMT  
 Obtaining your IP Address
Hi Benjamin,

    You may try this piece of code to obtain the IP address of your machine.

////////////////////////////////////////////////////////////////////////////
/////
    char Hostname[100];
   HOSTENT *pH;
   int **addr1;
   SOCKADDR_IN sock;
   CString addr;
 gethostname(Hostname,sizeof(Hostname));
 pH = gethostbyname(Hostname);
 addr1 = (int**)pH->h_addr_list;
 sock.sin_addr.s_addr = **addr1;
 addr = inet_ntoa(sock.sin_addr);//this is your ip address

////////////////////////////////////////////////////////////////////////////
///

Hope this helps you

Regards
Vijayaraghavan A
Aditi Technologies

http://www.aditi.com

Quote:

>I was wondering if anyone knows how to obtain the local IP address of a
>machine. Is there a Winsock function to do this? (and I'm not talking about
>using winipcfg, I want to be able to do what winipcfg does)

>Thanks,
>Ben




Thu, 11 Jan 2001 03:00:00 GMT  
 Obtaining your IP Address

Quote:

> I was wondering if anyone knows how to obtain the local IP address of a
> machine. Is there a Winsock function to do this? (and I'm not talking about
> using winipcfg, I want to be able to do what winipcfg does)

> Thanks,
> Ben


You want to look at WSAStartup and gethostname:

        HOSTENT * Host;
        WSADATA wsaData;
        char Str[128];

        WSAStartup( MAKEWORD(2,0), &wsaData );
        gethostname ( Str, sizeof(Str) );
        Host = gethostbyname ( Str );
        Str[0]='\0';
        for( int i=0; Host->h_addr_list[i]; i++ )
                sprintf( Str+strlen(Str), "%u.%u.%u.%u  ",
                Host->h_addr_list[i][0]&0xFF, Host->h_addr_list[i][1]&0xFF,
                Host->h_addr_list[i][2]&0xFF, Host->h_addr_list[i][3]&0xFF );
        MessageBox( Str );

Hope that helps,
Joe



Thu, 11 Jan 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Question:obtaining local ip address

2. Obtaining Inet IP Address when using a Router

3. Function to obtain local IP Address?

4. ### Obtain own IP-Address ###

5. Obtaining local IP address

6. Obtaining pid, ip address, and tds under NT.

7. Q: How to obtain the ip address of the local mechine.

8. How to obtain the IP address of your computer?

9. How to Obtain IP address programatically

10. Obtaining IP Address

11. getting ip address of the default route interface (public ip vs private ip)

12. Obtaining a PCI address vs a virtual address?

 

 
Powered by phpBB® Forum Software