本文整理汇总了Java中java.net.Inet4Address.getByAddress方法的典型用法代码示例。如果您正苦于以下问题:Java Inet4Address.getByAddress方法的具体用法?Java Inet4Address.getByAddress怎么用?Java Inet4Address.getByAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.Inet4Address
的用法示例。
在下文中一共展示了Inet4Address.getByAddress方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: localAddress
import java.net.Inet4Address; //导入方法依赖的package包/类
@Override
protected SocketAddress localAddress(DatagramChannel handle) throws Exception {
InetSocketAddress inetSocketAddress = (InetSocketAddress) handle.socket().getLocalSocketAddress();
InetAddress inetAddress = inetSocketAddress.getAddress();
if ((inetAddress instanceof Inet6Address) && (((Inet6Address) inetAddress).isIPv4CompatibleAddress())) {
// Ugly hack to workaround a problem on linux : the ANY address is always converted to IPV6
// even if the original address was an IPV4 address. We do store the two IPV4 and IPV6
// ANY address in the map.
byte[] ipV6Address = ((Inet6Address) inetAddress).getAddress();
byte[] ipV4Address = new byte[4];
for (int i = 0; i < 4; i++) {
ipV4Address[i] = ipV6Address[12 + i];
}
InetAddress inet4Adress = Inet4Address.getByAddress(ipV4Address);
return new InetSocketAddress(inet4Adress, inetSocketAddress.getPort());
} else {
return inetSocketAddress;
}
}
示例2: ipIntToInet4Address
import java.net.Inet4Address; //导入方法依赖的package包/类
public static InetAddress ipIntToInet4Address(int ip){
byte[] ipAddress=new byte[4];
writeInt(ipAddress, 0, ip);
try {
return Inet4Address.getByAddress(ipAddress);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
示例3: fromIpv4String
import java.net.Inet4Address; //导入方法依赖的package包/类
/**
* 从IPv4String转换为InetAddress.
*
* IpString如果确定ipv4, 使用本方法减少字符分析消耗 .
*
* 先字符串传换为byte[]再调getByAddress(byte[]),避免了调用getByName(ip)可能引起的DNS访问.
*/
public static Inet4Address fromIpv4String(String address) {
byte[] bytes = ip4StringToBytes(address);
if (bytes == null) {
return null;
} else {
try {
return (Inet4Address) Inet4Address.getByAddress(bytes);
} catch (UnknownHostException e) {
throw new AssertionError(e);
}
}
}
示例4: ipIntToInet4Address
import java.net.Inet4Address; //导入方法依赖的package包/类
public static InetAddress ipIntToInet4Address(int ip) {
byte[] ipAddress = new byte[4];
writeInt(ipAddress, 0, ip);
try {
return Inet4Address.getByAddress(ipAddress);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}