當前位置: 首頁>>代碼示例>>Java>>正文


Java Inet4Address類代碼示例

本文整理匯總了Java中java.net.Inet4Address的典型用法代碼示例。如果您正苦於以下問題:Java Inet4Address類的具體用法?Java Inet4Address怎麽用?Java Inet4Address使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Inet4Address類屬於java.net包,在下文中一共展示了Inet4Address類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getEmbeddedIPv4ClientAddress

import java.net.Inet4Address; //導入依賴的package包/類
/**
 * Examines the Inet6Address to extract the embedded IPv4 client address if the InetAddress is an
 * IPv6 address of one of the specified address types that contain an embedded IPv4 address.
 *
 * <p>NOTE: ISATAP addresses are explicitly excluded from this method due to their trivial
 * spoofability. With other transition addresses spoofing involves (at least) infection of one's
 * BGP routing table.
 *
 * @param ip {@link Inet6Address} to be examined for embedded IPv4 client address
 * @return {@link Inet4Address} of embedded IPv4 client address
 * @throws IllegalArgumentException if the argument does not have a valid embedded IPv4 address
 */
public static Inet4Address getEmbeddedIPv4ClientAddress(Inet6Address ip) {
  if (isCompatIPv4Address(ip)) {
    return getCompatIPv4Address(ip);
  }

  if (is6to4Address(ip)) {
    return get6to4IPv4Address(ip);
  }

  if (isTeredoAddress(ip)) {
    return getTeredoInfo(ip).getClient();
  }

  throw formatIllegalArgumentException("'%s' has no embedded IPv4 address.", toAddrString(ip));
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:28,代碼來源:InetAddresses.java

示例2: testIsAuthenticationDialogSuppressed

import java.net.Inet4Address; //導入依賴的package包/類
public void testIsAuthenticationDialogSuppressed() throws Exception {
    final boolean[] suppressed = new boolean[1];
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            suppressed[0] = NetworkSettings.isAuthenticationDialogSuppressed();
            return super.getPasswordAuthentication();
        }
    });

    Callable<Void> callable = new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Authenticator.requestPasswordAuthentication("wher.ev.er", Inet4Address.getByName("1.2.3.4"), 1234, "http", null, "http");
            return null;
        }
    };
    NetworkSettings.suppressAuthenticationDialog(callable);
    assertTrue(suppressed[0]);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:23,代碼來源:NetworkSettingsTest.java

示例3: pickInetAddress

import java.net.Inet4Address; //導入依賴的package包/類
protected static InetAddress pickInetAddress(Iterable<InetAddress> sortedList, IpAddressUtils.IpTypePreference ipTypePref) {
    IpAddressUtils.IpTypePreference pref = getIpTypePreferenceResolved(ipTypePref);
    for (InetAddress ipAddress : sortedList) {
        if (pref == IpAddressUtils.IpTypePreference.ANY_IPV4_PREF  || pref == IpAddressUtils.IpTypePreference.ANY_IPV6_PREF) {
            return ipAddress;
        }
        if (ipAddress instanceof Inet4Address) {
            if (pref == IpAddressUtils.IpTypePreference.IPV4_ONLY) {
                return ipAddress;
            }
        }
        if (ipAddress instanceof Inet6Address) {
            if (pref == IpAddressUtils.IpTypePreference.IPV6_ONLY) {
                return ipAddress;
            }
        }
    }
    return null;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:20,代碼來源:IpAddressUtilsFilter.java

示例4: filterInetAddresses

import java.net.Inet4Address; //導入依賴的package包/類
protected static @NonNull List<InetAddress> filterInetAddresses(Iterable<InetAddress> list, IpAddressUtils.IpTypePreference ipTypePref) {
    IpAddressUtils.IpTypePreference pref = getIpTypePreferenceResolved(ipTypePref);
    List<InetAddress> newList = new ArrayList<>();
    if (list != null) {
        for (InetAddress ipAddress : list) {
            if (pref == IpAddressUtils.IpTypePreference.ANY_IPV4_PREF || pref == IpAddressUtils.IpTypePreference.ANY_IPV6_PREF) {
                newList.add(ipAddress);
            } else {
                if ((ipAddress instanceof Inet4Address) && (pref == IpAddressUtils.IpTypePreference.IPV4_ONLY)) {
                    newList.add(ipAddress);
                }
                if ((ipAddress instanceof Inet6Address) && (pref == IpAddressUtils.IpTypePreference.IPV6_ONLY)) {
                    newList.add(ipAddress);
                }
            }
        }
    }
    if (pref == IpAddressUtils.IpTypePreference.ANY_IPV4_PREF) {
        IpAddressUtils.sortIpAddressesShallow(newList,true);
    }
    if (pref == IpAddressUtils.IpTypePreference.ANY_IPV6_PREF) {
        IpAddressUtils.sortIpAddressesShallow(newList,false);
    }
    return newList;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:26,代碼來源:IpAddressUtilsFilter.java

示例5: GetHostIp

import java.net.Inet4Address; //導入依賴的package包/類
/**
 * 獲取ip地址方法
 * @return
 */
public static String GetHostIp() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface
                .getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
                    .hasMoreElements();) {
                InetAddress inetAddress = ipAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                    //if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {
                    return inetAddress.getHostAddress().toString();
                }

            }
        }
    } catch (SocketException ex) {
    } catch (Exception e) {
    }
    return null;
}
 
開發者ID:wp521,項目名稱:MyFire,代碼行數:25,代碼來源:IpUtils.java

示例6: getIpAddress

import java.net.Inet4Address; //導入依賴的package包/類
public static String getIpAddress() {
    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 instanceof Inet4Address)) { // we get both ipv4 and ipv6, we want ipv4
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        Log.e(TAG,"getIpAddress", e);
    }

    return null;
}
 
開發者ID:archos-sa,項目名稱:aos-FileCoreLibrary,代碼行數:18,代碼來源:ArchosUtils.java

示例7: getTeredoInfo

import java.net.Inet4Address; //導入依賴的package包/類
/**
 * Returns the Teredo information embedded in a Teredo address.
 *
 * @param ip {@link Inet6Address} to be examined for embedded Teredo information
 * @return extracted {@code TeredoInfo}
 * @throws IllegalArgumentException if the argument is not a valid IPv6 Teredo address
 */
public static TeredoInfo getTeredoInfo(Inet6Address ip) {
  checkArgument(isTeredoAddress(ip), "Address '%s' is not a Teredo address.", toAddrString(ip));

  byte[] bytes = ip.getAddress();
  Inet4Address server = getInet4Address(Arrays.copyOfRange(bytes, 4, 8));

  int flags = ByteStreams.newDataInput(bytes, 8).readShort() & 0xffff;

  // Teredo obfuscates the mapped client port, per section 4 of the RFC.
  int port = ~ByteStreams.newDataInput(bytes, 10).readShort() & 0xffff;

  byte[] clientBytes = Arrays.copyOfRange(bytes, 12, 16);
  for (int i = 0; i < clientBytes.length; i++) {
    // Teredo obfuscates the mapped client IP, per section 4 of the RFC.
    clientBytes[i] = (byte) ~clientBytes[i];
  }
  Inet4Address client = getInet4Address(clientBytes);

  return new TeredoInfo(server, client, port, flags);
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:28,代碼來源:InetAddresses.java

示例8: importContact

import java.net.Inet4Address; //導入依賴的package包/類
@Override
public DHTPluginContact
importContact(
	InetSocketAddress				address,
	byte							version )
{
	if ( !isEnabled()){

		throw( new RuntimeException( "DHT isn't enabled" ));
	}

	InetAddress contact_address = address.getAddress();

	for ( DHTPluginImpl dht: dhts ){

		InetAddress dht_address = dht.getLocalAddress().getAddress().getAddress();

		if ( 	( contact_address instanceof Inet4Address && dht_address instanceof Inet4Address ) ||
				( contact_address instanceof Inet6Address && dht_address instanceof Inet6Address )){

			return( dht.importContact( address, version ));
		}
	}

	return( null );
}
 
開發者ID:BiglySoftware,項目名稱:BiglyBT,代碼行數:27,代碼來源:DHTPlugin.java

示例9: getIpv4Address

import java.net.Inet4Address; //導入依賴的package包/類
public static String getIpv4Address() {
    try {
        for (Enumeration<NetworkInterface> enNetI = NetworkInterface.getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
            NetworkInterface netI = enNetI.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = netI.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return "";
}
 
開發者ID:jiajieshen,項目名稱:AndroidDevSamples,代碼行數:17,代碼來源:NetworkUtil.java

示例10: getAddresses

import java.net.Inet4Address; //導入依賴的package包/類
/**
 * Returns a list of all the addresses on the system.
 * @param  inclLoopback
 *         if {@code true}, include the loopback addresses
 * @param  ipv4Only
 *         it {@code true}, only IPv4 addresses will be included
 */
static List<InetAddress> getAddresses(boolean inclLoopback,
                                      boolean ipv4Only)
    throws SocketException {
    ArrayList<InetAddress> list = new ArrayList<InetAddress>();
    Enumeration<NetworkInterface> nets =
             NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netInf : Collections.list(nets)) {
        Enumeration<InetAddress> addrs = netInf.getInetAddresses();
        for (InetAddress addr : Collections.list(addrs)) {
            if (!list.contains(addr) &&
                    (inclLoopback ? true : !addr.isLoopbackAddress()) &&
                    (ipv4Only ? (addr instanceof Inet4Address) : true)) {
                list.add(addr);
            }
        }
    }

    return list;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:27,代碼來源:Util.java

示例11: valueOf

import java.net.Inet4Address; //導入依賴的package包/類
/**
 * Converts an InetAddress into an IP address.
 *
 * @param inetAddress the InetAddress value to use
 * @return an IP address
 * @throws IllegalArgumentException if the argument is invalid
 */
public static IpAddress valueOf(InetAddress inetAddress) {
    byte[] bytes = inetAddress.getAddress();
    if (inetAddress instanceof Inet4Address) {
        return new IpAddress(Version.INET, bytes);
    }
    if (inetAddress instanceof Inet6Address) {
        return new IpAddress(Version.INET6, bytes);
    }
    // Use the number of bytes as a hint
    if (bytes.length == INET_BYTE_LENGTH) {
        return new IpAddress(Version.INET, bytes);
    }
    if (bytes.length == INET6_BYTE_LENGTH) {
        return new IpAddress(Version.INET6, bytes);
    }
    final String msg = "Unrecognized IP version address string: " +
        inetAddress.toString();
    throw new IllegalArgumentException(msg);
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:27,代碼來源:IpAddress.java

示例12: getLocalIpAddress

import java.net.Inet4Address; //導入依賴的package包/類
public static String getLocalIpAddress() {
    try {
        String localIpAddress = Collections.list(NetworkInterface.getNetworkInterfaces()).stream()
                .flatMap(networkInterface -> Collections.list(networkInterface.getInetAddresses()).stream())
                .filter(inetAddress -> inetAddress instanceof Inet4Address && inetAddress.getAddress()[0] != 127)
                .map(InetAddress::getHostAddress)
                .findFirst()
                .orElseThrow(AssertionError::new);

        logger.debug("localIpAddress: {}", localIpAddress); // TODO not even in debug.

        return localIpAddress;
    } catch (SocketException e) {
        // should never happen
        throw new AssertionError();
    }
}
 
開發者ID:membrane,項目名稱:kubernetes-client,代碼行數:18,代碼來源:Util.java

示例13: pingButtonActionPerformed

import java.net.Inet4Address; //導入依賴的package包/類
private void pingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pingButtonActionPerformed
       String host = hostTF.getText();
     try {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        InetAddress adr = Inet4Address.getByName(host);
        try{
            adr.isReachable(3000);
            JOptionPane.showMessageDialog(this, host+" is reachable, but it may not be the SpiNNaker!");
        }catch(IOException notReachable){
            JOptionPane.showMessageDialog(this, host+" is not reachable: "+notReachable.toString(), "Not reachable", JOptionPane.WARNING_MESSAGE);
        }
    } catch (UnknownHostException ex) {
              JOptionPane.showMessageDialog(this, host+" is unknown host: "+ex.toString(), "Host not found", JOptionPane.WARNING_MESSAGE);
    } finally {
        setCursor(Cursor.getDefaultCursor());
    }

}
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:19,代碼來源:SpiNNaker_InterfaceFactory.java

示例14: setup

import java.net.Inet4Address; //導入依賴的package包/類
@Before
public void setup() throws Exception {
    System.setProperty(ApplicationProperties.PROPERTIES_ROOT_PATH, new File(".").getAbsolutePath() + "/src/test/resources");
    PowerMockito.mockStatic(JwalaUtils.class);
    PowerMockito.when(JwalaUtils.getHostAddress("testServer")).thenReturn(Inet4Address.getLocalHost().getHostAddress());
    PowerMockito.when(JwalaUtils.getHostAddress("testServer2")).thenReturn(Inet4Address.getLocalHost().getHostAddress());

    // It is good practice to start with a clean sheet of paper before each test that is why resourceService is
    // initialized here. This makes sure that unrelated tests don't affect each other.
    MockitoAnnotations.initMocks(this);
    reset(Config.mockHistoryFacadeService);
    reset(Config.mockJvmPersistenceService);
    reset(Config.mockWebServerPersistenceService);
    reset(Config.mockGroupPesistenceService);
    reset(Config.mockAppPersistenceService);

    when(Config.mockJvmPersistenceService.findJvmByExactName(eq("someJvm"))).thenReturn(mock(Jvm.class));
    when(Config.mockRepositoryService.upload(anyString(), any(InputStream.class))).thenReturn("thePath");
}
 
開發者ID:cerner,項目名稱:jwala,代碼行數:20,代碼來源:ResourceServiceImplTest.java

示例15: Authority

import java.net.Inet4Address; //導入依賴的package包/類
/** */
private Authority(
    Optional<String> userName, Optional<String> password,
    Optional<Object> host, int portOrNegOne,
    IDNA.Info info) {
  if (host.isPresent()) {
    Object hostObj = host.get();
    Preconditions.checkArgument(
        hostObj instanceof InternetDomainName
        || hostObj instanceof Inet4Address
        || hostObj instanceof Inet6Address,
        "Invalid host", hostObj);
  }

  this.userName = userName;
  this.password = password;
  this.host = host;
  this.portOrNegOne = portOrNegOne;
  this.info = info;
}
 
開發者ID:OWASP,項目名稱:url-classifier,代碼行數:21,代碼來源:Authority.java


注:本文中的java.net.Inet4Address類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。