You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, February 14th 2005, 5:22pm

How to get IP Address of the machine ?

Hello,

How to get the IP address of the machine ?

I only succed to get the localhost address (127.0.0.1).

OS : Windows XP.

Thanks for your help.
Marvin

solomonhk

Beginner

  • "solomonhk" is male

Posts: 3

Location: 127.0.0.1

  • Send private message

2

Monday, February 14th 2005, 6:39pm

RE: How to get IP Address of the machine ?

Are you using ipconfig?
[fsck!]

guest

Unregistered

3

Monday, February 14th 2005, 8:23pm

RE: How to get IP Address of the machine ?

Just guessing, y ou probably want to access the first hop of the route, and not the machine IP. I say this because every machine has some form of localhost address, and many machines have more than one network interface (e.g., both a modem and a NIC running same time, or 2 NIC's). Depending on what interface is set up to handle the outbound IP, the local IP can differ for 2 different outbound IP's. The first hop of the route would tell you the local IP that is used for that outbound IP. The means of finding the first hop will vary across o/s's, and is not portable.

The alternative is to find all local interface IP's, their netmasks, and default route...followed by computing which one would handle a given outbound IP.

solomonhk

Beginner

  • "solomonhk" is male

Posts: 3

Location: 127.0.0.1

  • Send private message

4

Tuesday, February 15th 2005, 2:00pm

RE: How to get IP Address of the machine ?

guest: you do realize you said all that without answering his question?
[fsck!]

guest

Unregistered

5

Tuesday, February 15th 2005, 9:34pm

RE: How to get IP Address of the machine ?

[quote][i]Originally posted by solomonhk[/i]
guest: you do realize you said all that without answering his question?[/quote]

Yes. It gives him the information to answer it himself though. And his question wasn't really the question he wanted to ask because he didn't know that there is no single IP address for a machine...you can't just get the right IP and expect it to work on all machines unless you know the destination IP and all interfaces used to reach it.

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

6

Thursday, February 17th 2005, 5:17pm

Usually you need your local IP address to send it to a communication partner.
The most relyable way is to establish the connection and then look which IP address your socket got assigned to.

Cheers,
_
Qt/KDE Developer
Debian User

Guest

Unregistered

7

Thursday, February 17th 2005, 8:53pm

[quote][i]Originally posted by anda_skoa[/i]
Usually you need your local IP address to send it to a communication partner.
The most relyable way is to establish the connection and then look which IP address your socket got assigned to.

Cheers,
_[/quote]

Exactly....but what that does in the background is to let the kernel and system compute the right interface based on routing mechanism and then establish. Knowing netmasks, default route, and all interface IP's does the same thing, without actually attempting the connection. This is what the routing mechanism does, but doing it manually allows skipping an attempt to actually connect.

bisserke

Beginner

  • "bisserke" is male

Posts: 32

Location: Belgium

Occupation: Right now my job is eatin' these doughnuts, or maybe...

  • Send private message

8

Wednesday, March 23rd 2005, 2:11pm

!!!!! this only works WIN32 platforms !!!!!

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <winsock.h>
#include <qstringlist.h>

QStringList List;
	
char hostname[128];
if (gethostname(hostname, sizeof(hostname)) == SOCKET_ERROR) {
	QMessageBox::critical(NULL, "error", "error occured initializing.");
}

qDebug("found name %s\n", hostname);

struct hostent *phe = gethostbyname(hostname);
if (phe == 0) {
	QMessageBox::critical(NULL, "error", "error occured initializing.");
}

for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
	struct in_addr addr;
	memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
	
	printf("found ip %s\n", inet_ntoa(addr));
	List.append( inet_ntoa(addr) );
}


if you need the linux/unix version, let me know
Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon

This post has been edited 1 times, last edit by "bisserke" (Mar 23rd 2005, 2:12pm)


bisserke

Beginner

  • "bisserke" is male

Posts: 32

Location: Belgium

Occupation: Right now my job is eatin' these doughnuts, or maybe...

  • Send private message

10

Wednesday, March 23rd 2005, 4:38pm

No. it just returns all the IP addresses + aliases except for localhost.
I'm not sure if it returns the adresses of all the network cards. But try it out.
Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

11

Tuesday, April 12th 2005, 1:18pm

It needs to resolve addresses by their hostname... I'm currently trying to get my local IP in Python and the problem is simmilar, there are no functions that return the list of IP's for a current socket if it is bound to 0.0.0.0 X(

12

Tuesday, May 24th 2005, 5:51am

To bisserke

If you have a linux\unix version of your above code. for getting the IP address.
I would appreciate copy
Thank you
Navron

13

Tuesday, July 9th 2013, 7:59am

Hello,

How to get the IP address of the machine ?

I only succed to get the localhost address (127.0.0.1).

OS : Windows XP.
Thanks for your help.
Marvin
If you want to find an IP address of the machine for Windows XP,follow the below steps:
1.Start->Run->cmd prompt

2.In that,just type ipconfig and then click enter

For example: It will show ip address like 74.110.208.65 .


If you want to check your network IP address that is provided by your ISP,visit Ip-details.com .

This post has been edited 1 times, last edit by "Yuva" (Jul 9th 2013, 8:04am)