private Core() { boolean f = false; try { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = (NetworkInterface) en.nextElement(); for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { String ipAddress = inetAddress.getHostAddress().toString(); this.ip = inetAddress; f = true; break; } } if (f) break; } } catch (SocketException ex) { ex.printStackTrace(); System.exit(1); } this.teams = new HashMap<String, Team>(); this.judges = new HashMap<String, Judge>(); this.problems = new HashMap<String, ProblemInfo>(); this.scheduler = new Scheduler(); this.timer = new ContestTimer(300 * 60); try { readConfigure(); } catch (FileNotFoundException e) { e.printStackTrace(); System.exit(1); } this.scoreBoardHttpServer = new ScoreBoardHttpServer(this.scoreBoardPort); }
public AppFrame() { super(); GlobalData.oFrame = this; this.setSize(this.width, this.height); this.toolkit = Toolkit.getDefaultToolkit(); Dimension w = toolkit.getScreenSize(); int fx = (int) w.getWidth(); int fy = (int) w.getHeight(); int wx = (fx - this.width) / 2; int wy = (fy - this.getHeight()) / 2; setLocation(wx, wy); this.tracker = new MediaTracker(this); String sHost = ""; try { localAddr = InetAddress.getLocalHost(); if (localAddr.isLoopbackAddress()) { localAddr = LinuxInetAddress.getLocalHost(); } sHost = localAddr.getHostAddress(); } catch (UnknownHostException ex) { sHost = "你的IP地址错误"; } // this.textLines[0] = "服务器正在运行."; this.textLines[1] = ""; this.textLines[2] = "你的IP地址: " + sHost; this.textLines[3] = ""; this.textLines[4] = "请打开你的手机客户端"; this.textLines[5] = ""; this.textLines[6] = "输入屏幕上显示的IP地址."; // try { URL fileURL = this.getClass().getProtectionDomain().getCodeSource().getLocation(); String sBase = fileURL.toString(); if ("jar".equals(sBase.substring(sBase.length() - 3, sBase.length()))) { jar = new JarFile(new File(fileURL.toURI())); } else { basePath = System.getProperty("user.dir") + "\\res\\"; } } catch (Exception ex) { this.textLines[1] = "exception: " + ex.toString(); } }
/** * Returns an InetAddress instance that represents the localhost, and that a socket can bind upon * or distribute to peers as a contact address. * * @param intendedDestination the destination that we'd like to use the localhost address with. * @return an InetAddress instance representing the local host, and that a socket can bind upon or * distribute to peers as a contact address. */ public synchronized InetAddress getLocalHost(InetAddress intendedDestination) { // no point in making sure that the localHostFinderSocket is initialized. // better let it through a NullPointerException. InetAddress localHost = null; localHostFinderSocket.connect(intendedDestination, this.RANDOM_ADDR_DISC_PORT); localHost = localHostFinderSocket.getLocalAddress(); localHostFinderSocket.disconnect(); // windows socket implementations return the any address so we need to // find something else here ... InetAddress.getLocalHost seems to work // better on windows so lets hope it'll do the trick. if (localHost.isAnyLocalAddress()) { try { // all that's inside the if is an ugly IPv6 hack // (good ol' IPv6 - always causing more problems than it solves.) if (intendedDestination instanceof Inet6Address) { // return the first globally routable ipv6 address we find // on the machine (and hope it's a good one) Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = (NetworkInterface) interfaces.nextElement(); Enumeration addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = (InetAddress) addresses.nextElement(); if (address instanceof Inet6Address) { if (!address.isAnyLocalAddress() && !address.isLinkLocalAddress() && !address.isSiteLocalAddress() && !address.isLoopbackAddress()) { return address; } } } } } else localHost = InetAddress.getLocalHost(); /** @todo test on windows for ipv6 cases */ } catch (Exception ex) { // sigh ... ok return 0.0.0.0 logger.warn("Failed to get localhost ", ex); } } return localHost; }
public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); // if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && // inetAddress.isSiteLocalAddress() ) { if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { String ipAddr = inetAddress.getHostAddress(); return ipAddr; } } } } catch (SocketException ex) { Log.d(TAG, ex.toString()); } return null; }
/** @return */ public static String getIPv4Address() { String ipv4address = null; try { final List<NetworkInterface> networkinterfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (final NetworkInterface networkinterface : networkinterfaces) { final List<InetAddress> addresses = Collections.list(networkinterface.getInetAddresses()); for (final InetAddress address : addresses) { if ((address == null) || address.isLoopbackAddress()) { continue; } if (address instanceof Inet4Address) { ipv4address = address.getHostAddress().toString(); break; } } } } catch (Exception x) { DBG.m(x); } return ipv4address; }
/** * Get IP address from first non-localhost interface * * @param ipv4 true=return ipv4, false=return ipv6 * @return address or empty string */ public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }