本文整理汇总了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));
}
示例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]);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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 );
}
示例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 "";
}
示例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;
}
示例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);
}
示例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();
}
}
示例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());
}
}
示例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");
}
示例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;
}