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

Signature
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-)
Chris Uppal - 09 Mar 2007 02:48 GMT
> 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
Lew - 09 Mar 2007 05:26 GMT
IchBin wrote:
>> 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)
[quoted text clipped - 5 lines]
> 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
Chris Uppal - 09 Mar 2007 14:33 GMT
> 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
Lew - 09 Mar 2007 15:06 GMT
>> I worked at an
>> organization where all incoming HTTP traffic went through a
[quoted text clipped - 7 lines]
> 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
Chris Uppal - 09 Mar 2007 17:27 GMT
> > > I worked at an
> > > organization where all incoming HTTP traffic went through a
[quoted text clipped - 9 lines]
>
> No idea. The product that did the rewrites is called "Web Seal".
Ah, I see. Presumably this thing:
<http://publib.boulder.ibm.com/infocenter/tivihelp/v2r1/topic/com.ibm.itame2.do
c_5.1/am51_webseal_guide10.htm>
Does all the authentication, etc, itself so that the inside of the LAN is
treated as no more than a hidden back-end to the webserver it provides.
Makes sense now. Thanks.
-- chris
sivasu.india@gmail.com - 09 Mar 2007 05:43 GMT
> Is there a way, using java.net, to get the IP address of the client PC
> the java app is running.
[quoted text clipped - 7 lines]
> '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.
Gordon Beaton - 09 Mar 2007 07:01 GMT
> 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

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Nigel Wade - 09 Mar 2007 15:01 GMT
> 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() ?

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555