本文整理汇总了Java中java.net.InetAddress.getAddress方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.getAddress方法的具体用法?Java InetAddress.getAddress怎么用?Java InetAddress.getAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.getAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createEngineId
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Generates an engine Id based on an InetAddress. Handles IPv4 and IPv6 addresses. The creation algorithm uses the passed IANA number.
* @param iana Your enterprise IANA number.
* @param addr The IP address the SNMPv3 Adaptor Server is listening to.
* @return The generated engine Id.
* @since 1.5
* @exception UnknownHostException if the provided <CODE>InetAddress </CODE> is null.
*/
public static SnmpEngineId createEngineId(int iana, InetAddress addr)
{
if(addr == null) throw new IllegalArgumentException("InetAddress is null.");
byte[] address = addr.getAddress();
byte[] engineid = new byte[5 + address.length];
engineid[0] = (byte) ( (iana & 0xFF000000) >> 24 );
engineid[0] |= 0x80;
engineid[1] = (byte) ( (iana & 0x00FF0000) >> 16 );
engineid[2] = (byte) ( (iana & 0x0000FF00) >> 8 );
engineid[3] = (byte) (iana & 0x000000FF);
if(address.length == 4)
engineid[4] = 0x01;
if(address.length == 16)
engineid[4] = 0x02;
for(int i = 0; i < address.length; i++) {
engineid[i + 5] = address[i];
}
return new SnmpEngineId(engineid);
}
示例2: _selectSuitableNetworkInterface
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* select a most suitable network interface according to the address
*
* @param address
* @return
*/
private SelectedInterface _selectSuitableNetworkInterface(InetAddress address) {
int similiarBytes = Integer.MIN_VALUE;
SelectedInterface selectedInterface = new SelectedInterface();
byte[] inputIpInBytes = address.getAddress();
for (PcapNetworkInterface currentInterface : _localPcapNetworkInterfaces) {
List<PcapAddress> addresses = currentInterface.getAddresses();
if (addresses != null) {
for (PcapAddress ipAddress : addresses) {
// make sure the address should be same type, all ipv4 or all ipv6
if (!_isSameTypeAddress(address, ipAddress.getAddress())) {
continue;
}
byte[] ipInBytes = ipAddress.getAddress().getAddress();
int currentSimiliarBytes = _similarBytes(inputIpInBytes, ipInBytes);
if (currentSimiliarBytes > similiarBytes) {
selectedInterface._selectedNetworkInterface = currentInterface;
selectedInterface._selectedIpAddress = ipAddress.getAddress();
similiarBytes = currentSimiliarBytes;
}
}
}
}
return selectedInterface;
}
示例3: Blowfish
import java.net.InetAddress; //导入方法依赖的package包/类
public Blowfish(String csKey, boolean bMixWithIpAdress)
{
try
{
if(bMixWithIpAdress)
{
InetAddress adr = InetAddress.getLocalHost();
byte ipAdress[] = adr.getAddress();
int nVal = 0;
for(int n=0; n<ipAdress.length; n++)
{
nVal *= 256;
nVal += ipAdress[n];
}
csKey += nVal;
}
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
m_csKey = csKey;
}
示例4: toAddrString
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Returns the string representation of an {@link InetAddress}.
*
* <p>For IPv4 addresses, this is identical to {@link InetAddress#getHostAddress()}, but for IPv6
* addresses, the output follows <a href="http://tools.ietf.org/html/rfc5952">RFC 5952</a> section
* 4. The main difference is that this method uses "::" for zero compression, while Java's version
* uses the uncompressed form.
*
* <p>This method uses hexadecimal for all IPv6 addresses, including IPv4-mapped IPv6 addresses
* such as "::c000:201". The output does not include a Scope ID.
*
* @param ip {@link InetAddress} to be converted to an address string
* @return {@code String} containing the text-formatted IP address
* @since 10.0
*/
public static String toAddrString(InetAddress ip) {
checkNotNull(ip);
if (ip instanceof Inet4Address) {
// For IPv4, Java's formatting is good enough.
return ip.getHostAddress();
}
checkArgument(ip instanceof Inet6Address);
byte[] bytes = ip.getAddress();
int[] hextets = new int[IPV6_PART_COUNT];
for (int i = 0; i < hextets.length; i++) {
hextets[i] = Ints.fromBytes((byte) 0, (byte) 0, bytes[2 * i], bytes[2 * i + 1]);
}
compressLongestRunOfZeroes(hextets);
return hextetsToIPv6String(hextets);
}
示例5: match
import java.net.InetAddress; //导入方法依赖的package包/类
@Override
public boolean match(Action action, InetAddress address, int port) {
if (action != action())
return false;
byte[] candidate = address.getAddress();
// same address type?
if (candidate.length != addressAsBytes.length)
return false;
// check bytes
for (int i=0; i<prefixByteCount; i++) {
if (candidate[i] != addressAsBytes[i])
return false;
}
// check remaining bits
if ((prefixByteCount < addressAsBytes.length) &&
((candidate[prefixByteCount] & mask) !=
(addressAsBytes[prefixByteCount] & mask)))
return false;
return super.match(action, address, port);
}
示例6: getIP3
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getIP3(){
try {
InetAddress addr = InetAddress.getLocalHost();
byte[] ipAddr = addr.getAddress();
addr = InetAddress.getByName(addr.getHostName());
ipAddr = addr.getAddress();
String ipAddrStr = "";
for (int i=0; i<ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i]&0xFF;
}
return ipAddrStr;
} catch (UnknownHostException e) {
}
return null;
}
示例7: getSubnetPrefix
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Given an IP address and a prefix network mask length, it returns the
* equivalent subnet prefix IP address Example: for ip = "172.28.30.254" and
* maskLen = 25 it will return "172.28.30.128"
*
* @param ip
* the IP address in InetAddress form
* @param maskLen
* the length of the prefix network mask
* @return the subnet prefix IP address in InetAddress form
*/
public static InetAddress getSubnetPrefix(final InetAddress ip, final int maskLen) {
int bytes = maskLen / 8;
int bits = maskLen % 8;
byte modifiedByte;
byte[] sn = ip.getAddress();
if (bits > 0) {
modifiedByte = (byte) (sn[bytes] >> 8 - bits);
sn[bytes] = (byte) (modifiedByte << 8 - bits);
bytes++;
}
for (; bytes < sn.length; bytes++) {
sn[bytes] = (byte) 0;
}
try {
return InetAddress.getByAddress(sn);
} catch (final UnknownHostException e) {
return null;
}
}
示例8: createClientIp
import java.net.InetAddress; //导入方法依赖的package包/类
private static int createClientIp()
{
long time = System.currentTimeMillis();
time &= 0xffffffffL;
int clientIp;
try
{
InetAddress inetadr = InetAddress.getLocalHost();
byte[] tmp = inetadr.getAddress();
clientIp = ((tmp[3] * 255 + tmp[2]) * 255 + tmp[1]) * 255 + tmp[0];
}
catch (Exception e)
{
// let's just fill it with something random:
clientIp = (int) (Math.random() * Integer.MAX_VALUE);
}
clientIp ^= (int) time;
return clientIp;
}
示例9: increment
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Returns a new InetAddress that is one more than the passed in address. This method works for
* both IPv4 and IPv6 addresses.
*
* @param address the InetAddress to increment
* @return a new InetAddress that is one more than the passed in address
* @throws IllegalArgumentException if InetAddress is at the end of its range
* @since 10.0
*/
public static InetAddress increment(InetAddress address) {
byte[] addr = address.getAddress();
int i = addr.length - 1;
while (i >= 0 && addr[i] == (byte) 0xff) {
addr[i] = 0;
i--;
}
checkArgument(i >= 0, "Incrementing %s would wrap.", address);
addr[i]++;
return bytesToInetAddress(addr);
}
示例10: Socks5Message
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Construct client request or server response.
*
* @param cmd
* - Request/Response code.
* @param ip
* - IP field.
* @paarm port - port field.
*/
public Socks5Message(int cmd, InetAddress ip, int port) {
super(cmd, ip, port);
if (ip == null) {
this.host = "0.0.0.0";
} else {
this.host = ip.getHostName();
}
this.version = SOCKS_VERSION;
byte[] addr;
if (ip == null) {
addr = new byte[4];
addr[0] = addr[1] = addr[2] = addr[3] = 0;
} else {
addr = ip.getAddress();
}
if (addr.length == 4) {
addrType = SOCKS_ATYP_IPV4;
} else {
addrType = SOCKS_ATYP_IPV6;
}
data = new byte[6 + addr.length];
data[0] = (byte) SOCKS_VERSION; // Version
data[1] = (byte) command; // Command
data[2] = (byte) 0; // Reserved byte
data[3] = (byte) addrType; // Address type
// Put Address
System.arraycopy(addr, 0, data, 4, addr.length);
// Put port
data[data.length - 2] = (byte) (port >> 8);
data[data.length - 1] = (byte) (port);
}
示例11: InetAddress_to_hex
import java.net.InetAddress; //导入方法依赖的package包/类
private static int InetAddress_to_hex(InetAddress a) {
int result = 0;
byte b[] = a.getAddress();
for (int i = 0; i < 4; i++)
result |= (b[i] & 0xff) << (8 * i);
return result;
}
示例12: isMaximum
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Returns true if the InetAddress is either 255.255.255.255 for IPv4 or
* ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff for IPv6.
*
* @return true if the InetAddress is either 255.255.255.255 for IPv4 or
* ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff for IPv6
* @since 10.0
*/
public static boolean isMaximum(InetAddress address) {
byte[] addr = address.getAddress();
for (int i = 0; i < addr.length; i++) {
if (addr[i] != (byte) 0xff) {
return false;
}
}
return true;
}
示例13: ip2long
import java.net.InetAddress; //导入方法依赖的package包/类
static long ip2long(final InetAddress ip) {
long l = 0;
final byte[] addr = ip.getAddress();
if (addr.length == 4) { // IPV4
for (int i = 0; i < 4; ++i) {
l += (((long) addr[i] & 0xFF) << 8 * (3 - i));
}
} else { // IPV6
return 0; // Have no idea how to deal with those
}
return l;
}
示例14: Socks4Message
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Most general constructor
*/
public Socks4Message(final int version, final int cmd,
final InetAddress ip, final int port, final String user) {
super(cmd, ip, port);
this.user = user;
this.version = version;
msgLength = user == null ? 8 : 9 + user.length();
msgBytes = new byte[msgLength];
msgBytes[0] = (byte) version;
msgBytes[1] = (byte) command;
msgBytes[2] = (byte) (port >> 8);
msgBytes[3] = (byte) port;
byte[] addr;
if (ip != null) {
addr = ip.getAddress();
} else {
addr = new byte[4];
addr[0] = addr[1] = addr[2] = addr[3] = 0;
}
System.arraycopy(addr, 0, msgBytes, 4, 4);
if (user != null) {
final byte[] buf = user.getBytes();
System.arraycopy(buf, 0, msgBytes, 8, buf.length);
msgBytes[msgBytes.length - 1] = 0;
}
}
示例15: isAny
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Returns true if the passed InetAddress contains all zero
*
* @param ip
* the IP address to test
* @return true if the address is all zero
*/
public static boolean isAny(final InetAddress ip) {
for (byte b : ip.getAddress()) {
if (b != 0) {
return false;
}
}
return true;
}