hi all,

I am working a carbon application using c++ in Mac machine.

I want to find the ip address of my machine using code.

How can i get this ip address?

Thanks in advance and urgent plz...

9 Years
Discussion Span
Last Post by samar ihab

gethostname will give you the name of the current host gethostbyname looks up the host with a particular name and will give you the address
> man 3 gethostname > man 3 gethostbyname


here is another way (which you may find easier). /sbin/ifconfig -a would give you an output of this kind:

fxp0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        ether 00:d0:43:7a:c0:6e
        inet netmask 0xffffff00 broadcast
        media: Ethernet autoselect (100baseTX <full-duplex>)
        status: active
plip0: flags=108810<POINTOPOINT,SIMPLEX,MULTICAST,NEEDSGIANT> metric 0 mtu 1500
pflog0: flags=141<UP,RUNNING,PROMISC> metric 0 mtu 33204
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
        inet6 ::1 prefixlen 128
        inet netmask 0xff000000
pfsync0: flags=0<> metric 0 mtu 1460
        syncpeer: maxupd: 128

ie. info on all the network interfaces.
all you have to do now is extract the ip addresses out of this information.

#include <cstdlib>
#include <iostream>
#include <fstream>

int main()
  system( "ifconfig -a | grep inet | "
          "sed 's/\\([ ]*[^ ]*\\)\\([ ]*[^ ]*\\).*$/\\1 \\2/' "
          " > address.txt" ) ;

the file address.txt will contain the addresses. on my machine:

        inet6  fe80::1%lo0
        inet6  ::1

the lines starting with inet are the ipv4 addresses, the ones with inet6 are the ipv6 addresses. read the addresses off the file using a std::ifstream.

note: if you want only externally visible ip addresses, discard addresses like the ipv4 address 127.0.0.x and the ipv6 address 0000:0000:0000:0000:0000:0000:0000:x ( ::x ). these are loopback addresses on lo0. also ignore any ipv6 address starting with fe80 (this is the ipv6 link-local prefix) or ipv4 addresses starting with 169.254.0. in the above example, the only address externally visible is the ipv4 address you may have more than one of these; eg. one for your ethernet and another for 802.11n.


this ip address store in text form if i wand to see in output window like in dev c++ dos promp please tell me what is the changes are required......


I am doing a project socket programming in C I do the connection and they talking to each other. I want to modify the code in the server I want to know how to Save the IP address of the registered client and map it to the name the client
provided when registering, and also I want to know how to Accept the invite requests and search for the callee IP address to forward the
In client code, I want to know how to Send an invite request with the callee name.
and aslo how to know Accept invite requests from the caller..

thank you please reply ASAP.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.