本文整理汇总了Java中java.net.InetAddress.getByAddress方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.getByAddress方法的具体用法?Java InetAddress.getByAddress怎么用?Java InetAddress.getByAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.getByAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
}
示例2: TransportAddress
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Read from a stream and use the {@code hostString} when creating the InetAddress if the input comes from a version prior
* {@link Version#V_5_0_3_UNRELEASED} as the hostString was not serialized
*/
public TransportAddress(StreamInput in, @Nullable String hostString) throws IOException {
if (in.getVersion().before(Version.V_6_0_0_alpha1_UNRELEASED)) { // bwc layer for 5.x where we had more than one transport address
final short i = in.readShort();
if(i != 1) { // we fail hard to ensure nobody tries to use some custom transport address impl even if that is difficult to add
throw new AssertionError("illegal transport ID from node of version: " + in.getVersion() + " got: " + i + " expected: 1");
}
}
final int len = in.readByte();
final byte[] a = new byte[len]; // 4 bytes (IPv4) or 16 bytes (IPv6)
in.readFully(a);
final InetAddress inetAddress;
if (in.getVersion().onOrAfter(Version.V_5_0_3_UNRELEASED)) {
String host = in.readString(); // the host string was serialized so we can ignore the passed in version
inetAddress = InetAddress.getByAddress(host, a);
} else {
// prior to this version, we did not serialize the host string so we used the passed in version
inetAddress = InetAddress.getByAddress(hostString, a);
}
int port = in.readInt();
this.address = new InetSocketAddress(inetAddress, port);
}
示例3: toInetAddress
import java.net.InetAddress; //导入方法依赖的package包/类
protected static final InetAddress toInetAddress(byte[] from) {
try {
return InetAddress.getByAddress(from);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unable to convert byte array to InetAddress!");
}
}
示例4: roundTrip
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Round trip test code for both IPv4 and IPv6. {@link InetAddress} contains the {@code getByAddress} and
* {@code getbyName} methods for both IPv4 and IPv6, unless you also specify a {@code scopeid}, which this does not
* test.
*
* @param bytes 4 (32-bit for IPv4) or 16 bytes (128-bit for IPv6)
* @throws Exception if any error occurs while interacting with the network address
*/
private void roundTrip(byte[] bytes) throws Exception {
Random random = random();
for (int i = 0; i < 10000; i++) {
random.nextBytes(bytes);
InetAddress expected = InetAddress.getByAddress(bytes);
String formatted = NetworkAddress.format(expected);
InetAddress actual = InetAddress.getByName(formatted);
assertEquals(expected, actual);
}
}
示例5: usingIPV4With9BitsMask
import java.net.InetAddress; //导入方法依赖的package包/类
@Test
public void usingIPV4With9BitsMask() throws UnknownHostException {
InetAddress address = generateIPAddressV4();
byte[] bytes = address.getAddress();
bytes[2] ^= 1;
InetAddress address2 = InetAddress.getByAddress(bytes);
bytes[2] ^= 2;
InetAddress address3 = InetAddress.getByAddress(bytes);
InetAddressBlock mask = new InetAddressBlock(address, 9);
Assert.assertTrue(mask.contains(address2));
Assert.assertFalse(mask.contains(address3));
}
示例6: toString
import java.net.InetAddress; //导入方法依赖的package包/类
@Override
public synchronized String toString() {
try {
return "[" + InetAddress.getByAddress(ipAddress) + " (" + visitStates.size() + ")]";
}
catch (UnknownHostException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
示例7: decodeInternal
import java.net.InetAddress; //导入方法依赖的package包/类
@Override
InetAddress decodeInternal(ByteBuffer input) {
if (input == null || input.remaining() == 0) return null;
try {
return InetAddress.getByAddress(Bytes.getArray(input));
} catch (UnknownHostException e) {
throw new InvalidTypeException(
"Invalid bytes for inet value, got " + input.remaining() + " bytes", e);
}
}
示例8: shouldHandleInet
import java.net.InetAddress; //导入方法依赖的package包/类
@Test
public void shouldHandleInet() throws Exception {
Codec<InetAddress> inetCodec = mapper.codecFor(primitive(INET));
assertThat(inetCodec).isSameAs(mapper.inet);
InetAddress ipv6 =
InetAddress.getByAddress(
new byte[] {127, 0, 0, 1, 127, 0, 0, 2, 127, 0, 0, 3, 122, 127, 125, 124});
InetAddress ipv4 = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
// ipv4
encodeAndDecode(inetCodec, ipv4, "0x7f000001");
// ipv6
encodeAndDecode(inetCodec, ipv6, "0x7f0000017f0000027f0000037a7f7d7c");
encodeAndDecode(inetCodec, null, null);
encodeObjectAndDecode(inetCodec, ipv4, "0x7f000001", ipv4);
encodeObjectAndDecode(inetCodec, "127.0.0.1", "0x7f000001", ipv4);
encodeObjectAndDecode(inetCodec, "notanip.>>>", null, null);
encodeObjectAndDecode(inetCodec, null, null, null);
encodeObjectAndDecode(inetCodec, Optional.of(12), null, null);
try {
// invalid number of bytes.
inetCodec.decode(Bytes.fromHexString("0x7f00000102"));
fail("Shouldn't have been able to decode 5 byte address");
} catch (InvalidTypeException e) {
assertThat(e.getCause()).isInstanceOf(UnknownHostException.class);
}
}
示例9: setDestination
import java.net.InetAddress; //导入方法依赖的package包/类
public void setDestination(Member destination) throws UnknownHostException {
this.destination = destination;
this.address = InetAddress.getByAddress(destination.getHost());
this.port = destination.getPort();
this.udpPort = destination.getUdpPort();
}
示例10: parseSocketAddress
import java.net.InetAddress; //导入方法依赖的package包/类
private InetSocketAddress parseSocketAddress(BdfList descriptor)
throws FormatException {
byte[] address = descriptor.getRaw(1);
int port = descriptor.getLong(2).intValue();
if (port < 1 || port > MAX_16_BIT_UNSIGNED) throw new FormatException();
try {
InetAddress addr = InetAddress.getByAddress(address);
return new InetSocketAddress(addr, port);
} catch (UnknownHostException e) {
// Invalid address length
throw new FormatException();
}
}
示例11: long2inet
import java.net.InetAddress; //导入方法依赖的package包/类
private static InetAddress long2inet(long addr) {
try {
byte[] b = new byte[4];
for (int i = b.length - 1; i >= 0; i--) {
b[i] = (byte) (addr & 0xFF);
addr = addr >> 8;
}
return InetAddress.getByAddress(b);
} catch (UnknownHostException ignore) {
return null;
}
}
示例12: HS110DiscoveryService
import java.net.InetAddress; //导入方法依赖的package包/类
public HS110DiscoveryService() throws UnknownHostException {
super(HS110BindingConstants.SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIMEOUT_SECONDS, false);
byte[] addr = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
broadcast = InetAddress.getByAddress(addr);
discoverPacket = new DatagramPacket(discoverbuffer, discoverbuffer.length, broadcast, 9999);
}
示例13: main
import java.net.InetAddress; //导入方法依赖的package包/类
public static void main(String[] args)
throws Exception {
OneKDC kdc = new OneKDC(null);
kdc.writeJAASConf();
KDC.saveConfig(OneKDC.KRB5_CONF, kdc,
"noaddresses = false",
"default_keytab_name = " + OneKDC.KTAB);
Config.refresh();
Context c = Context.fromJAAS("client");
Context s = Context.fromJAAS("server");
c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);
s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
InetAddress initiator = InetAddress.getLocalHost();
InetAddress acceptor = InetAddress.getLocalHost();
switch (args[0]) {
case "1":
// no initiator host address available, should be OK
break;
case "2":
// correct initiator host address, still fine
c.x().setChannelBinding(
new ChannelBinding(initiator, acceptor, null));
s.x().setChannelBinding(
new ChannelBinding(initiator, acceptor, null));
break;
case "3":
// incorrect initiator host address, fail
initiator = InetAddress.getByAddress(new byte[]{1,1,1,1});
c.x().setChannelBinding(
new ChannelBinding(initiator, acceptor, null));
s.x().setChannelBinding(
new ChannelBinding(initiator, acceptor, null));
break;
}
Context.handshake(c, s);
}
示例14: gotChild
import java.net.InetAddress; //导入方法依赖的package包/类
@Override
public void gotChild( Asset currentObject, Object child ) {
if( child instanceof DefaultXmlObject ) {
DefaultXmlObject c = (DefaultXmlObject)child;
if( c.getName().toLowerCase() == "tag" ) {
String tagName = c.getAttributes().get( "name" ).toLowerCase();
String tagValue = c.getValue();
try {
switch( tagName ) {
case "bios-uuid":
currentObject.setBiosUuid( UUID.fromString( tagValue ) );
break;
case "host-fqdn":
currentObject.setHostFqdn( tagValue );
break;
case "hostname":
currentObject.setHostName( tagValue );
break;
case "host-ip":
String[] ipChunks = tagValue.split( "\\." );
if( ipChunks.length == 4 ) {
byte[] hostIp = new byte[4];
for( int i = 0; i < 4; i++ )
hostIp[i] = (byte)Integer.parseInt( ipChunks[i] );
InetAddress address = InetAddress.getByAddress( tagValue, hostIp );
if( address instanceof Inet4Address )
currentObject.setHostIpV4( (Inet4Address)address );
}
break;
case "host-uuid":
currentObject.setId( UUID.fromString( tagValue ) );
break;
case "host_start":
currentObject.setLastHostScanStart( DateHelper.parseIso8601Date( tagValue ) );
break;
case "host_end":
currentObject.setLastHostScanEnd( DateHelper.parseIso8601Date( tagValue ) );
break;
case "lastauthenticatedresults":
currentObject.setLastAuthenticatedResult( DateHelper.parseIso8601Date( tagValue ) );
break;
case "local-checks-proto":
currentObject.setLastAuthenticatedScanProto( tagValue );
break;
case "mac-macaddress":
String[] macs = tagValue.split( "\n" );
for( String mac : macs ) {
mac = mac.trim();
if( mac.length() > 0 ) // the last mac is typically a sequence of spaces
currentObject.addMacAddress( MacAddressHelper.parse( mac ) );
}
break;
case "mcafee-epo-guid":
currentObject.setMcAfeeApoGuid( UUID.fromString( tagValue ) );
break;
case "netbios-name":
currentObject.setNetbiosName( tagValue );
break;
case "operating-system":
currentObject.setOperatingSystem( tagValue );
break;
case "system-type":
currentObject.setSystemType( tagValue );
break;
}
} catch( Exception e ) {
logger.warn( String.format( "Exception when parsing Host Properties tag \'%s\' with value \'%s\'", tagName, tagValue ), e );
}
}
}
}
示例15: installBlockRule
import java.net.InetAddress; //导入方法依赖的package包/类
public void installBlockRule(TargetAthenaValue ipSrc, TargetAthenaValue ipDst, String deviceUri) {
///////////////////////////////Debug///////////////////////////////////////
Integer sip = (Integer) ipSrc.getTargetAthenaValue();
Integer dip = (Integer) ipDst.getTargetAthenaValue();
byte[] sbytes = BigInteger.valueOf(sip).toByteArray();
byte[] dbytes = BigInteger.valueOf(sip).toByteArray();
InetAddress saddress = null;
InetAddress daddress = null;
try {
saddress = InetAddress.getByAddress(sbytes);
daddress = InetAddress.getByAddress(dbytes);
} catch (UnknownHostException e) {
e.printStackTrace();
}
DeviceId deviceId = DeviceId.deviceId(deviceUri);
TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Ip4Prefix matchIp4SrcPrefix =
Ip4Prefix.valueOf(((Integer) ipSrc.getTargetAthenaValue()).intValue(),
Ip4Prefix.MAX_MASK_LENGTH);
Ip4Prefix matchIp4DstPrefix =
Ip4Prefix.valueOf(((Integer) ipDst.getTargetAthenaValue()).intValue(),
Ip4Prefix.MAX_MASK_LENGTH);
selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
.matchIPSrc(matchIp4SrcPrefix)
.matchIPDst(matchIp4DstPrefix);
TrafficTreatment treatment = DefaultTrafficTreatment.builder()
.build();
ForwardingObjective forwardingObjective = DefaultForwardingObjective.builder()
.withSelector(selectorBuilder.build())
.withTreatment(treatment)
.withPriority(flowPriority)
.withFlag(ForwardingObjective.Flag.VERSATILE)
.fromApp(appId)
.makeTemporary(flowTimeout)
.add();
log.info("Block!!" + saddress.getHostAddress() + daddress.getHostAddress() + deviceUri);
flowObjectiveService.forward(deviceId,
forwardingObjective);
}