View Javadoc

1   /*
2    * Created on Mar 29, 2004
3    *
4    * To change the template for this generated file go to
5    * Window>Preferences>Java>Code Generation>Code and Comments
6    */
7   package org.neo.swarm.network;
8   import java.net.InetAddress;
9   import java.net.NetworkInterface;
10  import java.net.SocketException;
11  import java.util.Enumeration;
12  /***
13   * This object does..
14   * 
15   * @author navery
16   */
17  public class NetUtils {
18  	public static String getMyIPAddress() {
19  		try {
20  		Enumeration e = NetworkInterface.getNetworkInterfaces();
21  		while (e.hasMoreElements()) {
22  			NetworkInterface netface = (NetworkInterface) e.nextElement();
23  			//System.out.println("Net interface: " + netface.getName());
24  			Enumeration e2 = netface.getInetAddresses();
25  			while (e2.hasMoreElements()) {
26  				InetAddress ip = (InetAddress) e2.nextElement();
27  				// ignore mac addresses and assume the first non mac address is it....
28  				if (ip.getHostAddress().startsWith("1") || ip.getHostAddress().startsWith("2")) return ip.getHostAddress();
29  				//System.out.println("IP address: " + ip.toString());
30  			}
31  		}
32  		} catch (SocketException ex) {
33  			ex.printStackTrace();
34  		}
35  		return "127.0.0.1";
36  	}
37  }