Java Code Examples for java.nio.channels.AsynchronousServerSocketChannel#getLocalAddress()
The following examples show how to use
java.nio.channels.AsynchronousServerSocketChannel#getLocalAddress() .
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: ServerSocketAccept.java From coroutines with Apache License 2.0 | 6 votes |
/*************************************** * Returns the channel to be used by this step. This first checks the * currently exexcuting coroutine in the continuation parameter for an * existing {@link #SERVER_SOCKET_CHANNEL} relation. If that doesn't exists * or if it contains a closed channel a new {@link * AsynchronousServerSocketChannel} will be opened and stored in the state * object. * * @param rContinuation The continuation to query for an existing channel * * @return The channel * * @throws IOException If opening the channel fails */ protected AsynchronousServerSocketChannel getServerSocketChannel( Continuation<?> rContinuation) throws IOException { Coroutine<?, ?> rCoroutine = rContinuation.getCurrentCoroutine(); AsynchronousServerSocketChannel rChannel = rCoroutine.get(SERVER_SOCKET_CHANNEL); if (rChannel == null || !rChannel.isOpen()) { rChannel = AsynchronousServerSocketChannel.open( getChannelGroup(rContinuation)); rCoroutine.set(SERVER_SOCKET_CHANNEL, rChannel) .annotate(MetaTypes.MANAGED); } if (rChannel.getLocalAddress() == null) { rChannel.bind(getSocketAddress(rContinuation)); } return rChannel; }
Example 2
Source File: NioBridge.java From java-async-util with Apache License 2.0 | 6 votes |
public static void main(final String[] args) throws IOException { final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open().bind(null); final CompletionStage<AsynchronousSocketChannel> acceptStage = accept(server); final SocketAddress addr = server.getLocalAddress(); final CompletionStage<AsynchronousSocketChannel> connectStage = connect(addr); // after connecting, write the integer 42 to the server final CompletionStage<Void> writeStage = connectStage.thenAccept(channel -> writeInt(channel, 42)); final CompletionStage<Void> readStage = acceptStage // after accepting, read an int from the socket .thenCompose(NioBridge::readInt) // print the result .thenAccept(System.out::println); // wait for the write and the read to complete writeStage.toCompletableFuture().join(); readStage.toCompletableFuture().join(); }
Example 3
Source File: Iteration.java From java-async-util with Apache License 2.0 | 6 votes |
public static void main(final String[] args) throws IOException { final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open().bind(null); final CompletionStage<AsynchronousSocketChannel> acceptStage = accept(server); final SocketAddress addr = server.getLocalAddress(); final CompletionStage<AsynchronousSocketChannel> connectStage = connect(addr); // after connecting, write 100 random integers, then write -1 final CompletionStage<Void> writeStage = connectStage.thenCompose(channel -> write100Randoms(channel)); final CompletionStage<List<Integer>> readStage = acceptStage.thenCompose(Iteration::readUntilStopped); // wait for the write and the read to complete, print read results writeStage.toCompletableFuture().join(); System.out.println(readStage.toCompletableFuture().join()); }
Example 4
Source File: Locks.java From java-async-util with Apache License 2.0 | 6 votes |
/** * Setup a server that will accept a connection from a single client, and then respond to every * request sent by the client by incrementing the request by one. * * @return the {@link SocketAddress} of the created server * @throws IOException */ static SocketAddress setupServer() throws IOException { final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open().bind(null); final SocketAddress addr = server.getLocalAddress(); NioBridge.accept(server).thenAccept(channel -> { AsyncIterator .generate(() -> NioBridge.readInt(channel)) .thenCompose(clientIntRequest -> NioBridge.writeInt(channel, clientIntRequest + 1)) .consume() .whenComplete((ignore, ex) -> { System.out.println("connection closed, " + ex.getMessage()); }); }); return addr; }
Example 5
Source File: MultiProducerIteration.java From java-async-util with Apache License 2.0 | 5 votes |
public static void main(final String[] args) throws IOException { final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open().bind(null); final SocketAddress addr = server.getLocalAddress(); // on the client side, concurrently connect to addr 4 times, and write 100 random integers on // each connection final CompletionStage<Void> writeStage = Combinators.allOf(IntStream .range(0, 4) .mapToObj(i -> connect(addr) .thenComposeAsync(channel -> Iteration.write100Randoms(channel))) .collect(Collectors.toList())) .thenApply(ig -> null); // on the server side, we'd like to accept 4 connections and route their messages into a single // place we can consume final AsyncIterator<AsynchronousSocketChannel> clientConnections = AsyncIterator // listen for next connection .generate(() -> accept(server)) // only will take 4 connections .take(4); final AsyncIterator<Integer> results = routeClientMessages(clientConnections); // do something with the results! - print each result as it comes from each client final CompletionStage<Void> printStage = results.forEach(i -> System.out.println(i)); // wait for both the clients and the server/printing to complete writeStage.thenAcceptBoth(printStage, (ig1, ig2) -> { System.out.println("completed successfully"); }); }