View previous topic :: View next topic |
Author |
Message |
mkb137 n00b
Joined: 13 May 2002 Posts: 33 Location: Calgary, Canada
|
Posted: Fri May 31, 2002 1:28 am Post subject: Get own IP from C++, KDE? |
|
|
Does anyone know a way of finding my own IP address from within a KDE application? Either by C++ or KDE libraries. |
|
Back to top |
|
|
craftyc Guru
Joined: 23 May 2002 Posts: 443 Location: Behind You.
|
Posted: Fri May 31, 2002 1:46 pm Post subject: |
|
|
I don't know wether this is what you're looking for, but, open konsole and type /sbin/ifconfig -a. This will list all your NICs and display the IP address of each one (plus a few other things). _________________ Postcount ++ |
|
Back to top |
|
|
mkb137 n00b
Joined: 13 May 2002 Posts: 33 Location: Calgary, Canada
|
Posted: Fri May 31, 2002 9:41 pm Post subject: |
|
|
No, I'm talking about from within a KDE C++ program. |
|
Back to top |
|
|
metalhedd l33t
Joined: 30 May 2002 Posts: 692 Location: Ontario Canada
|
Posted: Sat Jun 15, 2002 12:40 am Post subject: |
|
|
I would think that it would be stored in a file somewhere, maybe in /etc or /proc or something. if not i think qt will look up the ip if you supply the machine name (which you can be sure is in /etc/hostname) however it might return 127.0.0.1 I'm not sure. all of this is just guesses since i've never programmed ANYTHING under linux. but i'm getting ready to start so i've been doing a litle bit of reading. _ |
|
Back to top |
|
|
trapni Retired Dev
Joined: 16 May 2002 Posts: 251 Location: Germany/Berlin
|
Posted: Sat Jun 15, 2002 6:31 pm Post subject: |
|
|
mkb137 wrote: | No, I'm talking about from within a KDE C++ program. | Check out the header files in /usr/include/net/*.
You'll probbably have to look up first wich interfaces are provided by the running system. Them you may look up for the IP addresses provided for each interface. That's it.
Exactly that's what ifconfig, ip, route etc are doing do Look at their sources as a good example
Cheers,
Christian Parpart. |
|
Back to top |
|
|
delta407 Bodhisattva
Joined: 23 Apr 2002 Posts: 2876 Location: Chicago, IL
|
Posted: Sat Jun 15, 2002 6:35 pm Post subject: |
|
|
Finding the IP may be less than useful, given that a large number of people are NATed or connecting to the net via proxy... what do you need it for? |
|
Back to top |
|
|
mkb137 n00b
Joined: 13 May 2002 Posts: 33 Location: Calgary, Canada
|
Posted: Sat Jun 15, 2002 11:53 pm Post subject: |
|
|
I needed it to do file transfer for the program KMess (http://kmess.sourceforge.net). Eventually I got the IP from ExtendedSocket's localAddress call, which only seems to work for a connected socket. |
|
Back to top |
|
|
jtanner Tux's lil' helper
Joined: 23 May 2002 Posts: 121 Location: Atlanta, GA
|
Posted: Thu Jun 20, 2002 4:27 am Post subject: |
|
|
I believe the usual way is to call gethostname() followed by gethostbyname().
Jim |
|
Back to top |
|
|
mkb137 n00b
Joined: 13 May 2002 Posts: 33 Location: Calgary, Canada
|
Posted: Sun Jun 23, 2002 2:04 am Post subject: |
|
|
"gethostname" will just pull the name you gave yourself in etc/hosts, won't it? Then, I believe, gethostbyname will do a lookup on the name. I tried that way and it ended up looking up "MikeHome" (the terribly original name by which I call my computer) and then failing to obtain an IP (as you would expect). |
|
Back to top |
|
|
jtanner Tux's lil' helper
Joined: 23 May 2002 Posts: 121 Location: Atlanta, GA
|
Posted: Sun Jun 23, 2002 5:55 am Post subject: |
|
|
I believe that gethostname returns the canonical name of the host, which is usually the first one in /etc/hosts.
Why would I expect it to fail when looking up a hostname? If it's in /etc/hosts, gethostbyname should find it. Check /etc/nsswitch.conf and make sure it has the following entry:
This tells the resolver routines to look in /etc/hosts before checking dns (note this doesn't affect nslookup).
Anyway, this is the portable way to do it. Otherwise, what trapni said is also a good idea if you don't mind being linux-only. Check the source code for ifconfig and do the same thing.
Jim |
|
Back to top |
|
|
fmalabre Guru
Joined: 19 Jun 2002 Posts: 376 Location: Chicago
|
Posted: Mon Jun 24, 2002 3:57 am Post subject: |
|
|
That's funny to put the Canadian flag for the English language on the web site...
I was wondering if I will get english or french when clicking on it... |
|
Back to top |
|
|
mkb137 n00b
Joined: 13 May 2002 Posts: 33 Location: Calgary, Canada
|
Posted: Tue Jun 25, 2002 1:28 am Post subject: |
|
|
Yeah, I know. I realized I should change that almost as soon as I put it up. All I was thinking at the time is to remove the US flag that the German guy who made the site had put there. I also feel that clicking on that I should get alternating English/French text or something.
Since then I've been too lazy to do some sort of modified Maple Leaf/Union Jack thing. |
|
Back to top |
|
|
fmalabre Guru
Joined: 19 Jun 2002 Posts: 376 Location: Chicago
|
Posted: Tue Jun 25, 2002 3:51 am Post subject: |
|
|
Eventually, I could do the french translation if it can help.
By the way, I'm french...
I don't use MSN so far, but your client looks very nice |
|
Back to top |
|
|
|