当前位置: 首页>>代码示例>>Java>>正文


Java InetAddress.getLoopbackAddress方法代码示例

本文整理汇总了Java中java.net.InetAddress.getLoopbackAddress方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.getLoopbackAddress方法的具体用法?Java InetAddress.getLoopbackAddress怎么用?Java InetAddress.getLoopbackAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.InetAddress的用法示例。


在下文中一共展示了InetAddress.getLoopbackAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getRevealedLocalAddress

import java.net.InetAddress; //导入方法依赖的package包/类
private static SocketAddress getRevealedLocalAddress(SocketAddress sa,
                                                     SecurityManager sm)
{
    if (sm == null || sa == null)
        return sa;
    InetSocketAddress ia = (InetSocketAddress)sa;
    try{
        sm.checkConnect(ia.getAddress().getHostAddress(), -1);
        // Security check passed
    } catch (SecurityException e) {
        // Return loopback address
        return new InetSocketAddress(InetAddress.getLoopbackAddress(),
                                     ia.getPort());
    }
    return sa;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:SctpNet.java

示例2: getPrimaryAddress

import java.net.InetAddress; //导入方法依赖的package包/类
/**
 * For multihomed machines (i.e. anything with Docker Machine), use the 
 * routing table default route to find what should be the primary network
 * facing interface.
 * @return an InetAddress for the primary address
 */
public static InetAddress getPrimaryAddress()
{
    InetAddress ret;
    try {
        DatagramSocket s = new DatagramSocket();
        s.connect(InetAddress.getByAddress(new byte[]{1,1,1,1}), 0);
        ret = s.getLocalAddress();
        s.close();
        return ret;
    } catch (SocketException | UnknownHostException se) {
        log.info("get by route failed: " + se);
    } 
    
    try {
        return InetAddress.getLocalHost();
    } catch (UnknownHostException ex) {
        log.info("getLocalHost failed: " + ex);
    }
 
    return InetAddress.getLoopbackAddress();
}
 
开发者ID:drytoastman,项目名称:scorekeeperfrontend,代码行数:28,代码来源:Network.java

示例3: createTransportSvc

import java.net.InetAddress; //导入方法依赖的package包/类
@Before
public void createTransportSvc() {
    MockTcpTransport transport =
        new MockTcpTransport(Settings.EMPTY,
                                threadPool,
                                BigArrays.NON_RECYCLING_INSTANCE,
                                new NoneCircuitBreakerService(),
                                new NamedWriteableRegistry(Collections.emptyList()),
                                new NetworkService(Settings.EMPTY, Collections.emptyList())) {
            @Override
            public BoundTransportAddress boundAddress() {
                return new BoundTransportAddress(
                    new TransportAddress[]{new TransportAddress(InetAddress.getLoopbackAddress(), 9300)},
                    new TransportAddress(InetAddress.getLoopbackAddress(), 9300)
                );
            }
        };
    transportService = new MockTransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR,
            null);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:21,代码来源:FileBasedUnicastHostsProviderTests.java

示例4: testShouldInheritBuilderProperties

import java.net.InetAddress; //导入方法依赖的package包/类
@Test
public void testShouldInheritBuilderProperties() {
  long id = 12L;
  InetSocketAddress address = new InetSocketAddress(InetAddress.getLoopbackAddress(), 9042);
  String version = "1.2.19";
  String name = "node0";
  Map<String, Object> peerInfo = new HashMap<>();
  peerInfo.put("hello", "world");
  NodeSpec node =
      NodeSpec.builder()
          .withId(id)
          .withAddress(address)
          .withCassandraVersion(version)
          .withName(name)
          .withPeerInfo(peerInfo)
          .withPeerInfo("goodbye", "world")
          .build();

  Map<String, Object> expectedPeerInfo = new HashMap<>(peerInfo);
  expectedPeerInfo.put("goodbye", "world");

  assertThat(node.getId()).isEqualTo(id);
  assertThat(node.getAddress()).isEqualTo(address);
  assertThat(node.getCassandraVersion()).isEqualTo(version);
  assertThat(node.getName()).isEqualTo(name);
  assertThat(node.getPeerInfo()).isEqualTo(expectedPeerInfo);
}
 
开发者ID:datastax,项目名称:simulacron,代码行数:28,代码来源:NodeTest.java

示例5: main

import java.net.InetAddress; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    final String HOST = "dummyserver.java.net";
    InetAddress ia = null;
    ia = InetAddress.getByName(HOST);
    testInetAddress(ia, HOST);
    ia = InetAddress.getByName("255.255.255.0");
    testInetAddress(ia, null);
    ia = InetAddress.getByAddress(new byte[]{1,1,1,1});
    testInetAddress(ia, null);
    ia = InetAddress.getLocalHost();
    testInetAddress(ia, ia.getHostName());
    ia = InetAddress.getLoopbackAddress();
    testInetAddress(ia, ia.getHostName());
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:15,代码来源:getOriginalHostName.java

示例6: testPeer

import java.net.InetAddress; //导入方法依赖的package包/类
@Test
public void testPeer() {

    //Init
    InetAddress address = InetAddress.getLoopbackAddress();
    List<Capability> capabilities = new ArrayList<>();
    int port = 1010;
    String peerId = "1010";
    Peer peerCopy = new Peer(address, port, peerId);

    //Peer
    Peer peer = new Peer(address, port, peerId);

    //getAddress
    assertEquals("127.0.0.1", peer.getAddress().getHostAddress());

    //getPort
    assertEquals(port, peer.getPort());

    //getPeerId
    assertEquals(peerId, peer.getPeerId());

    //getCapabilities
    assertEquals(capabilities, peer.getCapabilities());

    //getEncoded
    assertEquals("CC847F0000018203F2821010C0", Hex.toHexString(peer.getEncoded()).toUpperCase());

    //toString
    assertEquals("[ip=" + address.getHostAddress() + " port=" + Integer.toString(port) + " peerId=" + peerId + "]", peer.toString());

    //equals
    assertEquals(true, peer.equals(peerCopy));
    assertEquals(false, peer.equals(null));

    //hashCode
    assertEquals(-1218913009, peer.hashCode());
}
 
开发者ID:rsksmart,项目名称:rskj,代码行数:39,代码来源:PeerTest.java

示例7: testBadBoundAddressArray

import java.net.InetAddress; //导入方法依赖的package包/类
public void testBadBoundAddressArray() {
    try {
        TransportAddress[] badArray = randomBoolean() ? null : new TransportAddress[0];
        new BoundTransportAddress(badArray, new TransportAddress(InetAddress.getLoopbackAddress(), 80));
        fail("expected an exception to be thrown due to no bound address");
    } catch (IllegalArgumentException e) {
        //expected
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:10,代码来源:BoundTransportAddressTests.java

示例8: lookupAllHostAddr

import java.net.InetAddress; //导入方法依赖的package包/类
@Override
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
    if (firstCall) {
        firstCall = false;
        // throw unchecked exception first time round
        throw new IllegalStateException();
    }

    // return any valid address
    return new InetAddress[] { InetAddress.getLoopbackAddress() };
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:12,代码来源:ThrowingNameService.java

示例9: create

import java.net.InetAddress; //导入方法依赖的package包/类
/**
 * Creates an ExecutionControl instance based on a JDI
 * {@code ListeningConnector} or {@code LaunchingConnector}.
 *
 * Initialize JDI and use it to launch the remote JVM. Set-up a socket for
 * commands and results. This socket also transports the user
 * input/output/error.
 *
 * @param env the context passed by
 * {@link jdk.jshell.spi.ExecutionControl#start(jdk.jshell.spi.ExecutionEnv) }
 * @param remoteAgent the remote agent to launch
 * @param isLaunch does JDI do the launch? That is, LaunchingConnector,
 * otherwise we start explicitly and use ListeningConnector
 * @param host explicit hostname to use, if null use discovered
 * hostname, applies to listening only (!isLaunch)
 * @return the channel
 * @throws IOException if there are errors in set-up
 */
static ExecutionControl create(ExecutionEnv env, String remoteAgent,
        boolean isLaunch, String host, int timeout) throws IOException {
    try (final ServerSocket listener = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) {
        // timeout on I/O-socket
        listener.setSoTimeout(timeout);
        int port = listener.getLocalPort();

        // Set-up the JDI connection
        JdiInitiator jdii = new JdiInitiator(port,
                env.extraRemoteVMOptions(), remoteAgent, isLaunch, host,
                timeout, Collections.emptyMap());
        VirtualMachine vm = jdii.vm();
        Process process = jdii.process();

        List<Consumer<String>> deathListeners = new ArrayList<>();
        Util.detectJdiExitEvent(vm, s -> {
            for (Consumer<String> h : deathListeners) {
                h.accept(s);
            }
        });

        // Set-up the commands/reslts on the socket.  Piggy-back snippet
        // output.
        Socket socket = listener.accept();
        // out before in -- match remote creation so we don't hang
        OutputStream out = socket.getOutputStream();
        Map<String, OutputStream> outputs = new HashMap<>();
        outputs.put("out", env.userOut());
        outputs.put("err", env.userErr());
        Map<String, InputStream> input = new HashMap<>();
        input.put("in", env.userIn());
        return remoteInputOutput(socket.getInputStream(), out, outputs, input,
                (objIn, objOut) -> new JdiDefaultExecutionControl(env,
                                    objOut, objIn, vm, process, remoteAgent, deathListeners));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:55,代码来源:JdiDefaultExecutionControl.java

示例10: main

import java.net.InetAddress; //导入方法依赖的package包/类
public static void main(String[] args) {
    InterfaceAddress ia1;
    InterfaceAddress ia2;
    InetAddress loopbackAddr = InetAddress.getLoopbackAddress();
    InetAddress broadcast1 = null;
    InetAddress broadcast2 = null;

    try {
        broadcast1 = InetAddress.getByName("255.255.255.0");
        broadcast2 = InetAddress.getByName("255.255.0.0");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    ia1 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);
    ia2 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);

    compare(ia1, ia2, true);

    ia2 = createInterfaceAddress(loopbackAddr, broadcast1, (short)45);
    compare(ia1, ia2, false);

    ia2 = createInterfaceAddress((InetAddress)null, broadcast1, (short)45);
    compare(ia1, ia2, false);

    ia1 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);
    ia2 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);
    compare(ia1, ia2, true);

    ia1.equals(null);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:Equals.java

示例11: check

import java.net.InetAddress; //导入方法依赖的package包/类
public boolean check() {
    try (
            Socket socket = new Socket(InetAddress.getLoopbackAddress(), port);
            DataOutputStream os = new DataOutputStream(socket.getOutputStream());
            DataInputStream is = new DataInputStream(socket.getInputStream())
    ) {
        int number = random.nextInt();
        os.writeInt(number);
        os.flush();
        return is.readInt() == number;
    } catch (IOException e) {
        d("[c-check] io exception", e);
        return false;
    }
}
 
开发者ID:brevent,项目名称:Brevent,代码行数:16,代码来源:SimpleSock.java

示例12: checkPortSync

import java.net.InetAddress; //导入方法依赖的package包/类
@WorkerThread
public static boolean checkPortSync() throws IOException {
    try (
            Socket socket = new Socket(InetAddress.getLoopbackAddress(), BreventProtocol.PORT);
            DataOutputStream os = new DataOutputStream(socket.getOutputStream());
            DataInputStream is = new DataInputStream(socket.getInputStream())
    ) {
        os.writeShort(0);
        os.flush();
        return BreventProtocol.readFrom(is) == BreventOK.INSTANCE;
    }
}
 
开发者ID:brevent,项目名称:Brevent,代码行数:13,代码来源:BreventProtocol.java

示例13: rpcHost

import java.net.InetAddress; //导入方法依赖的package包/类
public InetAddress rpcHost() {
    if (!configFromFiles.hasPath("rpc.host")) {
        return InetAddress.getLoopbackAddress();
    }
    String host = configFromFiles.getString("rpc.host");
    try {
        return InetAddress.getByName(host);
    } catch (UnknownHostException e) {
        logger.warn("Unable to bind to {}. Using loopback instead", e);
        return InetAddress.getLoopbackAddress();
    }
}
 
开发者ID:rsksmart,项目名称:rskj,代码行数:13,代码来源:RskSystemProperties.java

示例14: main

import java.net.InetAddress; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {

        KDC.saveConfig(OneKDC.KRB5_CONF, new OneKDC(null),
                "noaddresses = false",
                "extra_addresses = 10.0.0.10, 10.0.0.11 10.0.0.12");
        Config.refresh();

        KerberosTicket ticket =
                Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false)
                        .s().getPrivateCredentials(KerberosTicket.class)
                        .iterator().next();

        InetAddress loopback = InetAddress.getLoopbackAddress();
        InetAddress extra1 = InetAddress.getByName("10.0.0.10");
        InetAddress extra2 = InetAddress.getByName("10.0.0.11");
        InetAddress extra3 = InetAddress.getByName("10.0.0.12");

        boolean loopbackFound = false;
        boolean extra1Found = false;
        boolean extra2Found = false;
        boolean extra3Found = false;
        boolean networkFound = false;

        for (InetAddress ia: ticket.getClientAddresses()) {
            System.out.println(ia);
            if (ia.equals(loopback)) {
                loopbackFound = true;
                System.out.println("  loopback found");
            } else if (ia.equals(extra1)) {
                extra1Found = true;
                System.out.println("  extra1 found");
            } else if (ia.equals(extra2)) {
                extra2Found = true;
                System.out.println("  extra2 found");
            } else if (ia.equals(extra3)) {
                extra3Found = true;
                System.out.println("  extra3 found");
            } else if (ia instanceof Inet4Address) {
                networkFound = true;
                System.out.println("  another address (" + ia +
                        "), assumed real network");
            }
        }

        if (!loopbackFound || !networkFound
                || !extra1Found || !extra2Found || !extra3Found ) {
            throw new Exception();
        }
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:50,代码来源:Addresses.java

示例15: getOtherInstance

import java.net.InetAddress; //导入方法依赖的package包/类
private static Socket getOtherInstance() {
    File socketFile = FileSystemService.getUserConfigFile("midas.lock");
    if(!socketFile.exists()) {
        return null;
    } else {
        int port;
        try {
            label130: {
                BufferedReader e = new BufferedReader(new InputStreamReader(new FileInputStream(socketFile), StandardCharsets.UTF_8));
                Throwable var3 = null;

                Object var5;
                try {
                    String portStr = e.readLine();
                    if(portStr != null) {
                        port = Integer.parseInt(portStr);
                        break label130;
                    }

                    log("Faild to retrieve port from socket file \'" + socketFile + "\'. File seems to be empty.");
                    var5 = null;
                } catch (Throwable var18) {
                    var3 = var18;
                    throw var18;
                } finally {
                    if(e != null) {
                        if(var3 != null) {
                            try {
                                e.close();
                            } catch (Throwable var16) {
                                var3.addSuppressed(var16);
                            }
                        } else {
                            e.close();
                        }
                    }

                }

                return (Socket)var5;
            }
        } catch (Exception var20) {
            log("Failed to read socket file \'" + socketFile + "\': " + var20.getMessage());
            return null;
        }

        log("Checking for running instance on port " + port + ".");

        try {
            return new Socket(InetAddress.getLoopbackAddress(), port);
        } catch (IOException var17) {
            log("Found lock file but no other instance running. Assuming unclean shutdown of previous launch.");
            return null;
        }
    }
}
 
开发者ID:transwarpio,项目名称:rapidminer,代码行数:57,代码来源:JVMOptionBuilder.java


注:本文中的java.net.InetAddress.getLoopbackAddress方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。