• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the IP Address of the local machine (not the server)?  RSS feed

 
Mike London
Ranch Hand
Posts: 1505
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried using code like below:
InetAddress address = InetAddress.getLocalHost();
localMachineIP = address.toString();

But this keeps returning the address of the server the application is coming from, NOT the user's PC running the code.
I've looked for the correct method, but can't seem to find it.
Do I need to use a "CGI" variable instead to get the user's IP address?
Thanks much in advance.
-- Mike
 
Mike London
Ranch Hand
Posts: 1505
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please never mind on my previous post.
<%=request.getRemoteHost()%>
Gets the correct remote host.
Thanks.
-- Mike
 
brad balmer
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a pure guess, but:
Socket sock = new Socket();
InetAddress iAdd = sock.getLocalAddress();
System.out.println("Address: " + iAdd.getHostAddress());
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!