Skip to content

Instantly share code, notes, and snippets.

@komamitsu
Created September 27, 2022 02:31
Show Gist options
  • Save komamitsu/c0e83a3869216191c4f9931da7098929 to your computer and use it in GitHub Desktop.
Save komamitsu/c0e83a3869216191c4f9931da7098929 to your computer and use it in GitHub Desktop.
package org.komamitsu.unixdomainsockettest;
import java.net.StandardProtocolFamily;
import java.net.UnixDomainSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.*;
public class UnixDomainSocketTest {
private static class Server implements Callable<List<String>> {
private final UnixDomainSocketAddress socketAddress;
private final CountDownLatch serverReady;
public Server(UnixDomainSocketAddress socketAddress, CountDownLatch serverReady) {
this.socketAddress = socketAddress;
this.serverReady = serverReady;
}
@Override
public List<String> call() throws Exception {
System.out.println("Server: Starting");
List<String> result = new ArrayList<>();
try (ServerSocketChannel socketChannel = ServerSocketChannel.open(StandardProtocolFamily.UNIX)) {
socketChannel.bind(socketAddress);
serverReady.countDown();
try (SocketChannel clientSocket = socketChannel.accept()) {
ByteBuffer buffer = ByteBuffer.allocate(256);
buffer.clear();
while (clientSocket.read(buffer) >= 0) {
buffer.flip();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
String word = new String(bytes);
System.out.printf("Server: Received '%s'\n", word);
result.add(word);
buffer.clear();
}
}
}
System.out.println("Server: Finished");
return result;
}
}
private static class Client implements Callable<Void> {
private final UnixDomainSocketAddress socketAddress;
private final CountDownLatch serverReady;
private final List<String> words;
public Client(UnixDomainSocketAddress socketAddress, CountDownLatch serverReady, List<String> words) {
this.socketAddress = socketAddress;
this.serverReady = serverReady;
this.words = words;
}
@Override
public Void call() throws Exception {
serverReady.await();
System.out.println("Client: Starting");
try (SocketChannel socketChannel = SocketChannel.open(socketAddress)) {
ByteBuffer buffer = ByteBuffer.allocate(256);
for (String word : words) {
System.out.printf("Client: Sending '%s'\n", word);
buffer.put(word.getBytes());
buffer.flip();
int len = socketChannel.write(buffer);
System.out.printf("Client: Sent length=%d\n", len);
buffer.clear();
TimeUnit.SECONDS.sleep(1);
}
}
System.out.println("Client: Finished");
return null;
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
Path socketPath = Paths.get(System.getProperty("java.io.tmpdir"), String.format("komamitsu-test-%s", UUID.randomUUID()));
UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress.of(socketPath);
CountDownLatch serverReady = new CountDownLatch(1);
Server server = new Server(socketAddress, serverReady);
List<String> words = Arrays.stream(new String[]{"zero", "one", "two", "three", "four", "five", "six", "seven"}).toList();
Client client = new Client(socketAddress, serverReady, words);
ExecutorService executorService = Executors.newCachedThreadPool();
Future<List<String>> serverFuture = executorService.submit(server);
Future<Void> clientFuture = executorService.submit(client);
executorService.shutdown();
List<String> result = serverFuture.get();
clientFuture.get();
if (words.equals(result)) {
System.out.println("Success!");
}
else {
System.out.printf("Failed! Received: %s\n", result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment