Originally posted by danengel:
<STRONG>Hi,
does anybody know some short piece of C-code to get my IP-address? I've got some sample code from
no-ip, but it doesn't work.
Thanks,
Daniel</STRONG>
The difficulty in solving this is due to multi-link/multi-homing. In other words, you may have several local IP addresses (127.0.0.1, Your PPP IP address, and your ethernet IP address).
The best way that I could come up with to solve this in a generic way is to open a socket to a server that will be located over the connection that I am interested in. Then query that socket for the local IP address. If you are going through a Firewall you will get your local IP address and not your external IP address, but in reading between the lines of your post, I think this is OK.
Here is the code that will print your IP address that is being used to access the internet. Hope this helps...
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
<font color = brown>/************************************************** **************************************************
************************************************** **************************************************/</font>
<font color = green>int</font> main (<font color = green>int</font> argc, <font color = green>const</font> <font color = green>char</font> * argv[])
{
<font color = green>char</font> szIPAddress[<font color = blue>256</font>] = <font color = red>"Failed"</font>;
<font color = green>struct</font> hostent* he = gethostbyname( <font color = red>"www.apple.com"</font> );
<font color = green>if</font> ( he ) {
<font color = green>unsigned</font> <font color = green>long</font> nHostAddress = *(<font color = green>long</font>*)he->h_addr;
<font color = green>if</font> ( nHostAddress != <font color = blue>0</font> ) {
<font color = green>int</font> nRemoteSocket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
<font color = green>if</font> ( nRemoteSocket != <font color = blue>-1</font> ) {
<font color = green>struct</font> sockaddr_in sockAddress;
sockAddress.sin_family = PF_INET;
sockAddress.sin_addr.s_addr = nHostAddress;
sockAddress.sin_port = htons( <font color = blue>80</font> );
<font color = green>if</font> ( connect ( nRemoteSocket, (<font color = green>struct</font> sockaddr *) &sockAddress,
<font color = green>sizeof</font>( <font color = green>struct</font> sockaddr_in ) ) == <font color = blue>0</font> ) {
<font color = green>int</font> len = (<font color = green>int</font>) <font color = green>sizeof</font> (<font color = green>struct</font> sockaddr_in);
<font color = green>struct</font> sockaddr_in saddr;
<font color = green>if</font> ( getsockname( nRemoteSocket, (<font color = green>struct</font> sockaddr *) &saddr, &len ) >= <font color = blue>0</font> ) {
<font color = green>char</font>* pszAddress = (<font color = green>char</font>*)&saddr.sin_addr;
sprintf( szIPAddress, <font color = red>"%d.%d.%d.%d"</font>,
pszAddress[<font color = blue>0</font>] & 0xFF,
pszAddress[<font color = blue>1</font>] & 0xFF,
pszAddress[<font color = blue>2</font>] & 0xFF,
pszAddress[<font color = blue>3</font>] & 0xFF );
}
close( nRemoteSocket );
}
}
}
}
printf( <font color = red>"Local IP Address: %s"</font>, szIPAddress );
<font color = green>return</font> <font color = blue>0</font>;
}
</font>[/code]