Getting client IP address

Discussion in 'Java' started by IchBin, Mar 9, 2007.

  1. IchBin

    IchBin Guest

    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-)
    IchBin, Mar 9, 2007
    #1
    1. Advertising

  2. IchBin

    Chris Uppal Guest

    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)
    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
    Chris Uppal, Mar 9, 2007
    #2
    1. Advertising

  3. IchBin

    Lew Guest

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


    Chris Uppal wrote:
    > 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
    Lew, Mar 9, 2007
    #3
  4. IchBin

    Guest

    On Mar 9, 7:17 am, IchBin <> wrote:
    > 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.
    , Mar 9, 2007
    #4
  5. On Thu, 08 Mar 2007 21:17:19 -0500, IchBin wrote:
    > 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

    --
    [ 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
    Gordon Beaton, Mar 9, 2007
    #5
  6. IchBin

    Chris Uppal Guest

    Lew wrote:

    > 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
    Chris Uppal, Mar 9, 2007
    #6
  7. IchBin

    Nigel Wade Guest

    IchBin wrote:

    > 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() ?

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, Mar 9, 2007
    #7
  8. IchBin

    Lew Guest

    Chris Uppal wrote:
    > Lew wrote:
    >
    >> 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 ;-)


    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
    Lew, Mar 9, 2007
    #8
  9. IchBin

    Chris Uppal Guest

    Lew wrote:

    > > > 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 ;-)

    >
    > 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
    Chris Uppal, Mar 9, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    1
    Views:
    7,225
    avnrao
    May 4, 2004
  2. Greg Smith

    Getting the IP address of the client PC

    Greg Smith, Aug 24, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    518
    Jens Christian Mikkelsen
    Aug 24, 2004
  3. Replies:
    3
    Views:
    360
    senior
    Jan 8, 2007
  4. Waldy

    Getting Client IP Address

    Waldy, Dec 17, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    308
    Waldy
    Dec 17, 2008
  5. Stewart

    getting client ip address

    Stewart, Feb 12, 2007, in forum: Ruby
    Replies:
    3
    Views:
    221
    James Kasemi
    Feb 26, 2008
Loading...

Share This Page