The Artima Developer Community
Sponsored Link

Java Answers Forum
IP-adres

6 replies on 1 page. Most recent reply: May 14, 2002 11:47 AM by Stefan Parijs

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Stefan Parijs

Posts: 16
Nickname: stefan
Registered: May, 2002

IP-adres Posted: May 9, 2002 7:03 AM
Reply to this message Reply
Advertisement
How can I get the ip of my pc, using java?
Tnx on advance

Stefan

stefan_parijs@yahoo.com


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: IP-adres Posted: May 10, 2002 8:40 AM
Reply to this message Reply
Look at the java.net.InnetAddress class !

Normally doing a call to :
static InetAddress[] getAllByName(String host)

Determines all the IP addresses of a host, given the host's name. You should use the 127.0.0.1 or "localhost" as parameter

Thomas SMETS,
SCJP2 - Brussels

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: IP-adres Posted: May 10, 2002 8:13 PM
Reply to this message Reply

/** Returns the string value of the user's IP Address.
*/
public String getMyIPAddress(){
String myIpAddress = "";
try{
myIpAddress = InetAddress.getLocalHost().getHostAddress();
}catch(UnknownHostException unhe){
System.err.println("Exception: " + unhe.getMessage());
}catch(Exception e){
System.err.println("Exception: " + e.getMessage());
}
return myIpAddress;
}

Stefan Parijs

Posts: 16
Nickname: stefan
Registered: May, 2002

Re: IP-adres Posted: May 11, 2002 10:17 AM
Reply to this message Reply
Tnx, to both of you. It works :D

Vikram.G.S.

Posts: 16
Nickname: vidhi
Registered: Apr, 2002

Re: IP-adres Posted: May 14, 2002 7:28 AM
Reply to this message Reply
Try out this simple program.

import java.net.*;
public class InetDemo {
public static void main(String aerg[]) throws Exception {
System.out.println(InetAddress.getLocalHost());
}
}

This will disply ur PC's IP Address.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: IP-adres Posted: May 14, 2002 9:32 AM
Reply to this message Reply
One little remark :
There may be more than one interface to your PC
(when there is 2 ethernet cards for instance).
Please keep that in mind when coding !

Thomas,

Stefan Parijs

Posts: 16
Nickname: stefan
Registered: May, 2002

Re: IP-adres Posted: May 14, 2002 11:47 AM
Reply to this message Reply
TNX a lot for the good advice!

Stefan Parijs

Flat View: This topic has 6 replies on 1 page
Topic: Uploading file back to http server Previous Topic   Next Topic Topic: Please Help!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use