Java Code Examples for java.net.Inet4Address#getLoopbackAddress()

The following examples show how to use java.net.Inet4Address#getLoopbackAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XdsSdsClientServerTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private SimpleServiceGrpc.SimpleServiceBlockingStub getBlockingStub(
    final UpstreamTlsContext upstreamTlsContext, String overrideAuthority)
    throws URISyntaxException {
  URI expectedUri = new URI("sdstest://localhost:" + port);
  fakeNameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).build();
  NameResolverRegistry.getDefaultRegistry().register(fakeNameResolverFactory);
  XdsChannelBuilder channelBuilder =
      XdsChannelBuilder.forTarget("sdstest://localhost:" + port);
  if (overrideAuthority != null) {
    channelBuilder = channelBuilder.overrideAuthority(overrideAuthority);
  }
  InetSocketAddress socketAddress =
      new InetSocketAddress(Inet4Address.getLoopbackAddress(), port);
  Attributes attrs =
      (upstreamTlsContext != null)
          ? Attributes.newBuilder()
              .set(XdsAttributes.ATTR_UPSTREAM_TLS_CONTEXT, upstreamTlsContext)
              .build()
          : Attributes.EMPTY;
  fakeNameResolverFactory.setServers(
      ImmutableList.of(new EquivalentAddressGroup(socketAddress, attrs)));
  return SimpleServiceGrpc.newBlockingStub(cleanupRule.register(channelBuilder.build()));
}
 
Example 2
Source File: SingleClientUdpTracker.java    From bt with Apache License 2.0 5 votes vote down vote up
public SingleClientUdpTracker(int interval, int leechers, int seeders) {
    try {
        this.serverSocket = new DatagramSocket(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 0));
    } catch (SocketException e) {
        throw new RuntimeException("Failed to create server socket", e);
    }

    this.interval = interval;
    this.leechers = leechers;
    this.seeders = seeders;
}
 
Example 3
Source File: ClientServerTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setup() throws Exception {
    expr = TimeSeriesMetricExpression.valueOf("1 + 2");
    CompletableFuture<Integer> portFuture = new CompletableFuture<>();

    server = new CollectHistoryServer(history);
    final Thread serverThread = new Thread(() -> {
        OncRpcServerTransport transport = null;
        try {
            transport = new OncRpcUdpServerTransport(server, Inet4Address.getLoopbackAddress(), 0, server.info, 32768);
            transport.setCharacterEncoding("UTF-8");
            portFuture.complete(transport.getPort());
            server.run(new OncRpcServerTransport[]{transport});
        } catch (IOException | OncRpcException ex) {
            LOG.log(Level.SEVERE, "server " + server + " failed to start", ex);
            portFuture.completeExceptionally(ex);
        } finally {
            if (transport != null) transport.close();
        }
    });
    serverThread.setDaemon(true);
    serverThread.start();
    final int port = portFuture.get(15, TimeUnit.SECONDS);
    LOG.log(Level.INFO, "chosen port: {0}", port);

    LOG.log(Level.INFO, "started server {0}", server);
    client = new Client(Inet4Address.getLoopbackAddress(), port, OncRpcProtocols.ONCRPC_UDP);
    LOG.log(Level.INFO, "started client {0}", client);
}
 
Example 4
Source File: MicroServe.java    From jeveassets with GNU General Public License v2.0 5 votes vote down vote up
public void startServer() {
	try {
		ServerSocket serverSocket = new ServerSocket(2221, 50, Inet4Address.getLoopbackAddress());
		ConnectionListener connectionListener = new ConnectionListener(serverSocket, this);
		connectionListener.start();
		serverStarted = true;
	} catch (IOException ex) {
		LOG.error(ex.getMessage(), ex);
		serverStarted = false;
	}
}
 
Example 5
Source File: UdpTrackerConnection.java    From bt with Apache License 2.0 4 votes vote down vote up
public UdpTrackerConnection(SingleClientUdpTracker tracker) {
    InetSocketAddress localAddress = new InetSocketAddress(Inet4Address.getLoopbackAddress(), 0);
    this.worker = new UdpMessageWorker(localAddress, tracker.getServerAddress(), mock(IRuntimeLifecycleBinder.class));
    LOGGER.info("Established connection (local: {}, remote: {}", localAddress, tracker.getServerAddress());
}