Getting client IP address

I

IchBin

Is there a way, using java.net, to get the IP address of the client PC
the java app is running.

Fooling around wit it and I only get the localhost address 127.0.0.1

--
Thanks in Advance... http://weconsultants.prophp.org
IchBin, Pocono Lake, Pa, USA http://ichbinquotations.awardspace.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
C

Chris Uppal

IchBin said:
Is there a way, using java.net, to get the IP address of the client PC
the java app is running.

Not in general no. The client machine itself may not know what its IP address
is. For instance none of my machines here (which are behind a NATing router)
have any idea how they appear to the rest of the Internet.

If you want the address they have on the local LAN, then that's probably doable
(I can't remember how offhand). That information may or may not be useful
and/or reliable depending on what you want to do with it. Note that machines
may have more than one physical network connection, and hence more than one
local IP address.

-- chris
 
L

Lew

Chris said:
Not in general no. The client machine itself may not know what its IP address
is. For instance none of my machines here (which are behind a NATing router)
have any idea how they appear to the rest of the Internet.

If you want the address they have on the local LAN, then that's probably doable
(I can't remember how offhand). That information may or may not be useful
and/or reliable depending on what you want to do with it. Note that machines
may have more than one physical network connection, and hence more than one
local IP address.

The host side may mask source IP addresses also. I worked at an organization
where all incoming HTTP traffic went through a proxy/firewall layer that
rewrote request headers as they entered the network. All applications saw the
proxy host as the originating IP address for all incoming HTTP requests.

-- Lew
 
S

sivasu.india

Is there a way, using java.net, to get the IP address of the client PC
the java app is running.

Fooling around wit it and I only get the localhost address 127.0.0.1

--
Thanks in Advance... http://weconsultants.prophp.org
IchBin, Pocono Lake, Pa, USA http://ichbinquotations.awardspace.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

You can get the IP Address in the following way:

InetAddress addr = InetAddress.getLocalHost();
// Get IP Address
byte[] ipAddr = addr.getAddress();

// Get hostname
String hostname = addr.getHostName();
just try it out.
 
G

Gordon Beaton

Is there a way, using java.net, to get the IP address of the client PC
the java app is running.

Fooling around wit it and I only get the localhost address 127.0.0.1

Nothing wrong with that result, depending on what you're trying to
acheive.

For more choices, iterate over the available NetworkInterfaces and
choose one you like.

/gordon
 
C

Chris Uppal

Lew said:
I worked at an
organization where all incoming HTTP traffic went through a
proxy/firewall layer that
rewrote request headers as they entered the network. All applications saw
the proxy host as the originating IP address for all incoming HTTP
requests.

Odd. Do you happen to know /why/ they did that ? I've been trying to think
of any reason, but without success so far (I can think of reasons /not/ to do
it, but even postulating exceptionally bloody-minded IT staff, that doesn't
seem to help ;-)

-- chris
 
N

Nigel Wade

IchBin said:
Is there a way, using java.net, to get the IP address of the client PC
the java app is running.

Fooling around wit it and I only get the localhost address 127.0.0.1

When you say "client PC", do you mean the PC on which the code is running, or
are you talking about a Socket on a server and wanting the IP address of the
client connected to that socket?

For the former, the way to do that is to iterate over the network interfaces and
get their IP addresses. One of those interfaces will be the "loopback"
interface, which always has the IP address 127.0.0.1.
http://java.sun.com/docs/books/tutorial/networking/nifs/listing.html

For the latter you can get the client address from the socket. Offhand I can't
remember the exact incantation, Socket.getRemoteAddress() ?
 
L

Lew

Chris said:
Odd. Do you happen to know /why/ they did that ? I've been trying to think
of any reason, but without success so far (I can think of reasons /not/ to do
it, but even postulating exceptionally bloody-minded IT staff, that doesn't
seem to help ;-)

No idea. The product that did the rewrites is called "Web Seal".

It messed up projects that tried to use the source IP, let me tell you.

-- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top