1
2
3
4
5
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
24 Enumeration e2 = netface.getInetAddresses();
25 while (e2.hasMoreElements()) {
26 InetAddress ip = (InetAddress) e2.nextElement();
27
28 if (ip.getHostAddress().startsWith("1") || ip.getHostAddress().startsWith("2")) return ip.getHostAddress();
29
30 }
31 }
32 } catch (SocketException ex) {
33 ex.printStackTrace();
34 }
35 return "127.0.0.1";
36 }
37 }