Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How to get my IP address?

How to get my IP address?
Thread Tools
Mac Enthusiast
Join Date: Oct 2000
Status: Offline
Reply With Quote
Sep 5, 2001, 05:13 PM
 
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
     
Mac Elite
Join Date: May 2001
Location: Nowhereland
Status: Offline
Reply With Quote
Sep 5, 2001, 07:44 PM
 
use dyndns.org and dns update.....they work very well
_______void_______
     
Mac Enthusiast
Join Date: Oct 2000
Status: Offline
Reply With Quote
Sep 6, 2001, 05:30 AM
 
This is to get my IP seen from outside, but I'd like to extract it from my system. An app like System Prefs/Network or Internet Connect knows my IP as well, and it doesn't get it from outside.
     
Fresh-Faced Recruit
Join Date: Sep 2001
Status: Offline
Reply With Quote
Sep 7, 2001, 01:38 AM
 
Here is one quick way I thought of. Parse the results of ifconfig en0 (replace en0 w/ whatever interface you're interested in). I'm sure there is a syscall that does this, but hey what kind of advice do you expect for free
     
Forum Regular
Join Date: Mar 2001
Status: Offline
Reply With Quote
Sep 9, 2001, 12:14 AM
 
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 &lt;iostream&gt;

#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt;
#include &lt;unistd.h&gt;


<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-&gt;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 ) &gt;= <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]
     
Mac Enthusiast
Join Date: Oct 2000
Status: Offline
Reply With Quote
Sep 9, 2001, 06:12 AM
 
Thank you very much!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:56 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2