Java Code Examples for java.net.Inet4Address.getByAddress()

The following are Jave code examples for showing how to use getByAddress() of the java.net.Inet4Address class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: neoscada   File: NioDatagramAcceptor.java   Source Code and License Vote up 6 votes
@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;
    }
}
 
Example 2
Project: onekey-proxy-android   File: CommonMethods.java   Source Code and License Vote up 5 votes
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;
	}
}
 
Example 3
Project: util   File: IPUtil.java   Source Code and License Vote up 5 votes
/**
 * 从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);
		}
	}
}
 
Example 4
Project: ss-android   File: CommonMethods.java   Source Code and License Vote up 5 votes
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;
    }
}