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...

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

Thanks for ur reply....

plz give some samples

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
        options=8<VLAN_MTU>
        ether 00:d0:43:7a:c0:6e
        inet 192.168.2.192 netmask 0xffffff00 broadcast 192.168.2.255
        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 127.0.0.1 netmask 0xff000000
pfsync0: flags=0<> metric 0 mtu 1460
        syncpeer: 224.0.0.240 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:

inet  192.168.2.192
        inet6  fe80::1%lo0
        inet6  ::1
        inet  127.0.0.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 192.168.2.192. you may have more than one of these; eg. one for your ethernet and another for 802.11n.

thank your sir,

its working fine.....

thanks a lot

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......

heyyy
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
request.
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.