下面列出了java.nio.channels.AsynchronousServerSocketChannel#getLocalAddress() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/***************************************
* 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;
}
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();
}
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());
}
/**
* 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;
}
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");
});
}