Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Get own IP from C++, KDE?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
mkb137
n00b
n00b


Joined: 13 May 2002
Posts: 33
Location: Calgary, Canada

PostPosted: Fri May 31, 2002 1:28 am    Post subject: Get own IP from C++, KDE? Reply with quote

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
View user's profile Send private message
craftyc
Guru
Guru


Joined: 23 May 2002
Posts: 443
Location: Behind You.

PostPosted: Fri May 31, 2002 1:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
mkb137
n00b
n00b


Joined: 13 May 2002
Posts: 33
Location: Calgary, Canada

PostPosted: Fri May 31, 2002 9:41 pm    Post subject: Reply with quote

No, I'm talking about from within a KDE C++ program.
Back to top
View user's profile Send private message
metalhedd
l33t
l33t


Joined: 30 May 2002
Posts: 692
Location: Ontario Canada

PostPosted: Sat Jun 15, 2002 12:40 am    Post subject: Reply with quote

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
View user's profile Send private message
trapni
Developer
Developer


Joined: 16 May 2002
Posts: 251
Location: Germany/Berlin

PostPosted: Sat Jun 15, 2002 6:31 pm    Post subject: Reply with quote

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 :P

Cheers,
Christian Parpart.
Back to top
View user's profile Send private message
delta407
Bodhisattva
Bodhisattva


Joined: 23 Apr 2002
Posts: 2876
Location: Chicago, IL

PostPosted: Sat Jun 15, 2002 6:35 pm    Post subject: Reply with quote

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
View user's profile Send private message
mkb137
n00b
n00b


Joined: 13 May 2002
Posts: 33
Location: Calgary, Canada

PostPosted: Sat Jun 15, 2002 11:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
jtanner
Tux's lil' helper
Tux's lil' helper


Joined: 23 May 2002
Posts: 121
Location: Atlanta, GA

PostPosted: Thu Jun 20, 2002 4:27 am    Post subject: Reply with quote

I believe the usual way is to call gethostname() followed by gethostbyname().

Jim
Back to top
View user's profile Send private message
mkb137
n00b
n00b


Joined: 13 May 2002
Posts: 33
Location: Calgary, Canada

PostPosted: Sun Jun 23, 2002 2:04 am    Post subject: Reply with quote

"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
View user's profile Send private message
jtanner
Tux's lil' helper
Tux's lil' helper


Joined: 23 May 2002
Posts: 121
Location: Atlanta, GA

PostPosted: Sun Jun 23, 2002 5:55 am    Post subject: Reply with quote

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:

Code:

hosts:        files dns


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
View user's profile Send private message
fmalabre
Guru
Guru


Joined: 19 Jun 2002
Posts: 376
Location: Chicago

PostPosted: Mon Jun 24, 2002 3:57 am    Post subject: Reply with quote

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
View user's profile Send private message
mkb137
n00b
n00b


Joined: 13 May 2002
Posts: 33
Location: Calgary, Canada

PostPosted: Tue Jun 25, 2002 1:28 am    Post subject: Reply with quote

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
View user's profile Send private message
fmalabre
Guru
Guru


Joined: 19 Jun 2002
Posts: 376
Location: Chicago

PostPosted: Tue Jun 25, 2002 3:51 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum