participate


New To Java - Local network address?
<<   Back to Forum  |   Give us Feedback
This topic has 7 replies on 1 page.
cs02rm0
Posts:115
Registered: 5/28/03
Local network address?   
May 14, 2005 9:32 AM

 
Does anyone know how you get the local network address of a machine in Java? (eg. 192.168.0.1, not the loopback ip).

Thanks
 
andresurbina
Posts:184
Registered: 4/2/05
Re: Local network address?   
May 14, 2005 10:29 AM (reply 1 of 7)  (In reply to original post )

 
import java.net.*;
import java.io.*;
 
 
public class URLReader {
 
 
  public static void main(String[] args) throws Exception {
 
	
	System.out.println(getName());
	    
    }
    
  static String getName() throws Exception {
	InetAddress inet = 	InetAddress.getLocalHost();
	
	return inet.getHostAddress();
 
	}    
    
}
 
 
cs02rm0
Posts:115
Registered: 5/28/03
Re: Local network address?   
May 14, 2005 10:53 AM (reply 2 of 7)  (In reply to #1 )

 
Thanks, but that returns 127.0.0.1 for me.
 
sjasja
Posts:3,489
Registered: 2/15/99
Re: Local network address?   
May 14, 2005 11:52 AM (reply 3 of 7)  (In reply to #2 )

 
Try this:
    public static InetAddress[] localHostAddresses()
        throws UnknownHostException
    {
            InetAddress localhost = InetAddress.getLocalHost();
            InetAddress addresses[] = InetAddress.getAllByName(localhost.getHostName());
            return addresses;
    }

There is also another in www.javaalmanac.com in the java.net category.
 
cs02rm0
Posts:115
Registered: 5/28/03
Re: Local network address?   
May 14, 2005 12:20 PM (reply 4 of 7)  (In reply to #3 )

 
Thanks, but that just returns 127.0.0.1 for me too.

The ones in the Java Almanac seem to be the same, they just used byte arrays instead?
 
cs02rm0
Posts:115
Registered: 5/28/03
Re: Local network address?   
May 14, 2005 1:07 PM (reply 5 of 7)  (In reply to #4 )

 
It seems mapping the hostname for my computer in /etc/hosts to 192.168.x.x instead of 127.0.0.1 solves this.
 
Woogley
Posts:1,422
Registered: 29/04/02
Re: Local network address?   
May 14, 2005 1:46 PM (reply 6 of 7)  (In reply to #5 )

 
There's a pretty 'dirty' way to do this assuming you have a computer that is ALWAYS on the network. Let's pretend this computer is named 'bob' on the network. You could have 'bob' be running this server:
import java.io.*;
import java.net.*;
public class Bob {
	public Bob() throws IOException {
		ServerSocket server = new ServerSocket(6000);
		Socket client;
		PrintStream out;
		while (true) {
			client = server.accept();
			out = new PrintStream(client.getOutputStream());
			out.print(client.getInetAddress().getHostAddress());
			out.close();
			client.close();
		}
	}
	public static void main(String args[]) {
		try {
			new Bob();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}

And then on your side, you could use the getIP() method demonstrated in this program:
import java.io.*;
import java.net.*;
public class Example {
	public Example() {
		String address = "N/A";
		try {
			address = getIP();
		}
		catch (IOException e) {
			address = "N/A";
			e.printStackTrace();
		}
		System.out.println("Address: "+address);
	}
	protected String getIP() throws IOException {
		Socket s = new Socket("bob",6000);
		BufferedReader b = new BufferedReader(new InputStreamReader(s.getInputStream()));
		return b.readLine();
	}
	public static void main(String args[]) {
		new Example();
	}
}

This will work ONLY if 'bob' is a DIFFERENT computer than the one thats running the getIP(). If it's run on the same machine, getIP() will obviously return 127.0.01.
 
omegaNetwork
Posts:281
Registered: 12/23/03
Re: Local network address?   
May 15, 2005 8:40 AM (reply 7 of 7)  (In reply to #2 )

 
Thanks, but that returns 127.0.0.1 for me.

Well, it looks like you were running that code on a machine without Internet connection. But if you were on windows machine and connected, then the sample code given by andresurbina should work like charm for you.

All the best.
 
This topic has 7 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 129

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums