By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

getting ip address by system call

P: 4
want to write acode in java so that i can get the ip address of localhost or
remote host using system call.
i know that threre is inetAddress class exist to get it but do not want to use
this facility.i want to execute a system call directly for it.
Nov 25 '06 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
want to write acode in java so that i can get the ip address of localhost or
remote host using system call.
i know that threre is inetAddress class exist to get it but do not want to use
this facility.i want to execute a system call directly for it.
use methods from the InetAddress class
http://java.sun.com/j2se/1.4.2/docs/api/java/net/InetAddress.html

e.g.
Expand|Select|Wrap|Line Numbers
  1.  // get IP addresses
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. public class IPaddresses 
  6. {
  7. public static void main(String args[])
  8. {
  9.      try
  10.         {
  11.         System.out.println("local IP address " + InetAddress.getLocalHost().toString());
  12.         System.out.println("remote IP address " + InetAddress.getByName("www.google.com").toString());
  13.         }
  14.      catch (Exception se) {System.err.println("error " + se); }
  15. }
  16. }
  17.  
when run this codegave
local IP address winxp-300g/192.168.0.7
remote IP address www.google.com/216.239.59.99
Nov 25 '06 #2

P: 3
GJY
okay i have a question
how do you find the ip address of your host machine without giving the host name of the machine?
I have to make a general program that returns the ip of the machine it's running on, and i obv. can't supply the host name coz that's different for each m/c
Feb 10 '07 #3

Expert 100+
P: 1,510
okay i have a question
how do you find the ip address of your host machine without giving the host name of the machine?
I have to make a general program that returns the ip of the machine it's running on, and i obv. can't supply the host name coz that's different for each m/c
the call
Expand|Select|Wrap|Line Numbers
  1. InetAddress.getLocalHost()
should return the IP of the local machine - in my previous post it gave using toString()
local IP address winxp-300g/192.168.0.7
Feb 10 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.