### Install Build Dependencies Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/graalvm.md Install necessary system-level dependencies for building native images. ```bash ## Add dependencies if necessary, e.g.: sudo apt-get install gcc zlib1g-dev ``` -------------------------------- ### Run Demo Script Syntax and Examples Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/demo.md Illustrates the syntax and provides examples for running specific junixsocket demos using the run-demo.sh script. This includes launching server, client, RMI, MySQL, PostgreSQL, and HTTP server demos, with options for specifying jars and Java system properties. ```bash $ ./run-demo.sh Syntax: ./run-demo.sh [-m] [-j jar]+ [-- [java opts]*] Example: # Runs the demo server ./run-demo.sh org.newsclub.net.unix.demo.SimpleTestServer # Runs the demo client ./run-demo.sh org.newsclub.net.unix.demo.SimpleTestClient # Runs the demo RMI server ./run-demo.sh org.newsclub.net.unix.demo.rmi.SimpleRMIServer # Runs the demo RMI client ./run-demo.sh org.newsclub.net.unix.demo.rmi.SimpleRMIClient # Runs the demo server. Replace "(demo)" with the desired demo. ./run-demo.sh -- -Ddemo=(demo) org.newsclub.net.unix.demo.server.AFUNIXSocketServerDemo # Runs the demo client. Replace "(demo)" with the desired demo, and "(socket)" with the socket to connect to. ./run-demo.sh -- -Ddemo=(demo) -Dsocket=(socket) org.newsclub.net.unix.demo.client.DemoClient # Runs the MySQL demo ./run-demo.sh -j (path-to-mysql-connector-jar) -- -DmysqlSocket=/tmp/mysql.sock org.newsclub.net.mysql.demo.AFUNIXDatabaseSocketFactoryDemo # Runs the PostgreSQL demo ./run-demo.sh -j (path-to-postgresql-jar) -- -DsocketPath=/tmp/.s.PGSQL.5432 org.newsclub.net.unix.demo.jdbc.PostgresDemo # Runs the HTTP Server ./run-demo.sh -j (path-to-nanohttpd-jar) -- org.newsclub.net.unix.demo.nanohttpd.NanoHttpdServerDemo Other flags: -m Use the Java module-path instead of the classpath (Java 9 or higher) -j Add the given jar to the beginning of the classpath/modulepath -- Separate the run-demo flags from the Java JVM flags ``` -------------------------------- ### Install crossclang SDK for Xcode Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/xcode.md Executes the installation script to link the crossclang SDK into the Xcode toolchain directory. ```bash ./junixsocket-native/crossclang/Xcode-Support/install ``` -------------------------------- ### Install Development Tools on Alpine Linux Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Installs essential development tools including git, Maven, clang, gcc, binutils, bash, and C/Linux headers on Alpine Linux. This is a prerequisite for compiling C code. ```bash sudo apk add git maven clang gcc binutils bash musl-dev libc-dev linux-headers ``` -------------------------------- ### Run Junixsocket Demos Script Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/demo.md Use the run-demos.sh script to launch various junixsocket examples. The script accepts a classname as an argument and can be configured with Java options. ```bash ./run-demos.sh ``` -------------------------------- ### Install LLVM and LLD on macOS Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md On macOS, the Xcode version of clang is insufficient for cross-compilation. Install llvm and lld using Homebrew. ```bash brew install llvm lld ``` -------------------------------- ### Enable musl Compatibility on Alpine Linux Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/graalvm.md Install gcompat to ensure the native binary runs on Alpine Linux systems. ```bash sudo apk add gcompat ``` -------------------------------- ### Configure GraalVM Environment Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/graalvm.md Set the JAVA_HOME and PATH environment variables to point to your GraalVM installation. ```bash # export JAVA_HOME=/Library/Java/JavaVirtualMachines/graalvm-ce-java17-22.2.0/Contents/Home # export PATH=$JAVA_HOME/bin:$PATH ``` -------------------------------- ### Build and Test junixsocket Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Compiles, tests, and installs junixsocket using Maven. The second command shows how to disable the retrolambda plugin if JDK 8 is not installed. ```bash mvn clean install ``` ```bash mvn clean install -Dretrolambda=false ``` -------------------------------- ### Simple C test program Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md A basic C program that prints 'Hello world, crossclang style!' to the console. This can be used to test cross-compilation setups. ```c #include int main(int argc, char *argv[]) { printf("Hello world, crossclang style!\n"); return 0; } ``` -------------------------------- ### Websocket to Unix Socket with websocat Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Forward data between a WebSocket and a Unix domain socket using websocat. This example shows a WebSocket listening on a local address and forwarding to a Unix socket. ```bash websocat ws-l:127.0.0.1:8088 unix:the_socket ``` -------------------------------- ### Unix Socket Listen to Websocket with websocat Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Forward data from a Unix domain socket to a WebSocket using websocat. This example listens on a Unix socket and forwards to a WebSocket server. ```bash websocat --unlink unix-l:the_socket ws://127.0.0.1:8089 ``` -------------------------------- ### Get Standard FileDescriptors Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Access the standard input, output, and error FileDescriptors provided by the Java runtime. ```java // Standard file descriptors FileDescriptor stdin = FileDescriptor.in; FileDescriptor stdout = FileDescriptor.out; FileDescriptor stderr = FileDescriptor.err; ``` -------------------------------- ### Configure JDK 8 Toolchain for Maven Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Specifies the JDK 8 installation path in Maven's toolchains.xml file. This is required for building junixsocket with Java 7 compatibility via the retrolambda plugin. ```xml jdk 1.8 /Library/Java/JavaVirtualMachines/1.8.0.jdk/Contents/Home/ ``` -------------------------------- ### Get FileDescriptor from RandomAccessFile Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Retrieve the FileDescriptor from a RandomAccessFile. This provides access to the file's underlying system handle. ```java RandomAccessFile raf = ...; FileDescriptor fd = in.getFD(); ``` -------------------------------- ### Get FileDescriptor from FileInputStream Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Obtain the FileDescriptor associated with a FileInputStream. This allows access to the underlying file handle. ```java FileInputStream in = ...; FileDescriptor fd = in.getFD(); ``` -------------------------------- ### Get FileDescriptor from AFSocket Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Retrieve the FileDescriptor from an AFSocket instance. This is useful when working with junixsocket's advanced socket features. ```java AFSocket socket = ...; FileDescriptor fd = socket.getFileDescriptor(); ``` -------------------------------- ### Build Everything (Current Architecture) Source: https://github.com/kohlschutter/junixsocket/blob/main/BUILDING.md Use this command to perform a full build for your current system architecture. It compiles all modules and includes the native library. ```bash mvn clean install ``` -------------------------------- ### Prepare Target SDK for Cross-compilation Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md Use the `prepare-target-sdk` script to automatically generate an SDK from a target machine. This script should be run on all target platforms, including the development machine. The generated SDKs are stored in `junixsocket-native/crosslang/target-sdks/` and should be copied to a shared directory on the development machine. ```bash junixsocket-native/crossclang/bin/prepare-target-sdk ``` -------------------------------- ### Build and Run Native Image Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/graalvm.md Commands to build the native image using Maven and execute the resulting binary. ```bash # Build the platform-native executable: cd junixsocket/junixsocket-selftest-native-image # (Also specify -Dmysql to include junixsocket-mysql tests) mvn -Dnative clean package # Run the platform-native executable: ./target/junixsocket-selftest-native-image-X.Y.Z ``` -------------------------------- ### Create a Listening Unix Socket with netcat Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Use 'nc -lU /path/to/socket' to create a listening Unix domain stream socket. ```bash nc -lU /path/to/socket ``` -------------------------------- ### Connect to a Unix Socket with netcat Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Use 'nc -U /path/to/socket' to connect to an existing Unix domain stream socket. ```bash nc -U /path/to/socket ``` -------------------------------- ### Build junixsocket demo with jlink and jpackage Source: https://github.com/kohlschutter/junixsocket/blob/main/junixsocket-demo-jpackagejlink/README.md Execute the Maven build process with specific profiles enabled to generate jlink and jpackage artifacts. ```bash cd junixsocket-demo-jpackagejlink mvn clean verify -Djpackage -Djlink ``` -------------------------------- ### Build junixsocket-native via command-line Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/xcode.md Uses xcodebuild to compile the project with the crossclang toolchain from the terminal. ```bash xcodebuild -project junixsocket-native/junixsocket-native.xcodeproj -configuration Release \ -target "All Architectures" -toolchain crossclang USE_HEADERMAP=NO \ CURRENT_ARCH=undefined_arch arch=undefined_arch ARCHS=arm64 CODE_SIGNING_ALLOWED=NO \ clean build ``` -------------------------------- ### Build Quickly (Ignoring Quality) Source: https://github.com/kohlschutter/junixsocket/blob/main/BUILDING.md This command prioritizes build speed over code quality checks. It disables Retrolambda and excludes the native library, suitable for rapid local builds. ```bash mvn clean install -Dretrolambda=false -rf :junixsocket-common -Dignorant ``` -------------------------------- ### Create a new AF_UNIX Server Socket Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/quickstart.md Use this to create and bind a new AF_UNIX domain server socket to a specified file path. The server socket will listen for incoming connections on this path. ```java File socketFile = new File("/path/to/your/socket"); AFUNIXServerSocket server = AFUNIXServerSocket.newInstance(); server.bind(AFUNIXSocketAddress.of(socketFile)); ``` -------------------------------- ### Build Quickly (Ignoring Tests) Source: https://github.com/kohlschutter/junixsocket/blob/main/BUILDING.md This command is optimized for speed by skipping all test executions. It also disables Retrolambda, excludes the native library, and ignores code quality checks. ```bash mvn clean install -Dretrolambda=false -rf :junixsocket-common -Dignorant -DskipTests ``` -------------------------------- ### Connect to an AF_UNIX Socket Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/quickstart.md Use this to connect to an existing AF_UNIX domain socket. Ensure the socket file exists before attempting to connect. ```java File socketFile = new File("/path/to/your/socket"); AFUNIXSocket sock = AFUNIXSocket.newInstance(); sock.connect(AFUNIXSocketAddress.of(socketFile)); ``` -------------------------------- ### List Listening Unix Sockets with netstat Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Use 'netstat --unix -l' to display listening Unix domain sockets. ```bash netstat --unix -l ``` -------------------------------- ### Work with AF_UNIX SocketChannels Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/quickstart.md This snippet demonstrates opening an AFUNIXSocketChannel, connecting it to a socket address, and then accessing the underlying Socket API. It highlights the interchangeability between SocketChannel and Socket APIs with junixsocket. ```java AFUNIXSelectorProvider provider = AFUNIXSelectorProvider.provider(); AFUNIXSocketChannel sc = provider.openSocketChannel(); sc.connect(AFUNIXSocketAddress.of(new File("/tmp/test.sock"))); // (work with SocketChannel API) AFUNIXSocket socket = sc.socket(); // this always succeeds [1] // (work with Socket API) AFUNIXSocket sock = ...; sock.getChannel(); // this always succeeds as well [1] AbstractSelector selector = provider.openSelector(); // work with Selector API ``` -------------------------------- ### Instantiate AFSocketServerConnector Source: https://github.com/kohlschutter/junixsocket/blob/main/junixsocket-jetty/README.md Configure a server connector for junixsocket by providing the server instance and socket address. ```java AFSocketServerConnector connector = new AFSocketServerConnector(server, acceptors, selectors, new HttpConnectionFactory()); AFSocketAddr afAddr = ...; // e.g., AFUNIXSocketAddress.of(new File("/tmp/sock)); connector.setListenSocketAddress(afAddr); // (optional) try to automatically stop server if another instance reuses our address connector.setMayStopServer(true); server.addConnector(connector); ``` -------------------------------- ### Configure Sonatype snapshot repository Source: https://github.com/kohlschutter/junixsocket/blob/main/README.md Enable the Sonatype snapshot repository in the project POM file to access snapshot builds. ```xml sonatype.snapshots Sonatype snapshot repository https://oss.sonatype.org/content/repositories/snapshots/ default true ``` -------------------------------- ### List Unix Sockets via /proc/net/unix (Linux) Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md On Linux, the '/proc/net/unix' file provides a list of all active Unix domain sockets. ```bash cat /proc/net/unix ``` -------------------------------- ### Build junixsocket selftest with GraalVM Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/graalvm.md Shell script to configure the environment and execute the selftest build process for GraalVM native-image compatibility. ```bash # Make sure GraalVM is enabled, e.g.: export JAVA_HOME=/Library/Java/JavaVirtualMachines/graalvm-ce-java17-22.2.0/Contents/Home export PATH=$JAVA_HOME/bin:$PATH # Run selftest with native-image-gent, build and run native-image version of selftest cd junixsocket/junixsocket-native-graalvm bin/build-selftest ``` -------------------------------- ### List Unix Sockets with lsof Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Use 'lsof -U' to list all Unix domain sockets. Add '+E' for peer process information on Linux. ```bash lsof -U ``` ```bash lsof +E -U ``` ```bash sudo lsof -a -U -u root ``` -------------------------------- ### Build junixsocket with SNAPSHOT Dependencies Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Builds junixsocket using SNAPSHOT versions of its dependencies. This is typically needed for development versions. ```bash mvn clean install -Duse-snapshots ``` -------------------------------- ### Using crossclang from Command-line Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md The `crossclang` script simplifies configuring the compiler and linker for a specific target environment. It forwards arguments to `clang` and automatically configures sysroot, include paths, library paths, and the appropriate linker based on the `-target` argument. ```bash junixsocket-native/crossclang/bin/clang ``` -------------------------------- ### Instantiate AFSocketClientConnector Source: https://github.com/kohlschutter/junixsocket/blob/main/junixsocket-jetty/README.md Create a client connector using a specific socket address. ```java ClientConnector clientConnector = AFSocketClientConnector.withSocketAddress(afAddr); ``` ```java AFSocketClientConnector.withSocketAddress(AFUNIXSocketAddress.of(new File("/tmp/socket"))); ``` -------------------------------- ### Inspect UUID via otool Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/peercreds.md Use the otool command-line utility to inspect the UUID for a binary on macOS. ```bash otool -l | grep uuid ``` -------------------------------- ### Compile C code for the current architecture Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md Use crossclang to compile a C program for the current architecture. The '-target current' flag automatically detects the host system. ```bash # Compile (and link) for the current architecture, no matter what it is crossclang/bin/clang -o test test.c -target current ``` -------------------------------- ### List Unix Sockets with ss (Linux) Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Use 'ss -x' to list Unix domain sockets on Linux. Add 'p' to show peer processes if UNIX_DIAG is enabled in the kernel. ```bash ss -x ``` ```bash ss -xp ``` -------------------------------- ### Build Excluding Native Library Source: https://github.com/kohlschutter/junixsocket/blob/main/BUILDING.md Recommended for most use cases, this command builds all modules except for the native junixsocket library. This is useful if you don't need the native component or are on an incompatible platform. ```bash mvn clean install -rf :junixsocket-common ``` -------------------------------- ### Build junixsocket-common, Skipping junixsocket-native-custom Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Builds the junixsocket-common module while skipping the junixsocket-native-custom dependency. This is a workaround for issues related to the native-custom module during testing. ```bash mvn clean install -Dnative-custom.skip -rf :junixsocket-common ``` -------------------------------- ### Build junixsocket with GCC as Linker Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Builds junixsocket while explicitly specifying GCC as the compiler and linker. Use this if clang is unavailable or causing issues. ```bash mvn clean install -Djunixsocket.native.default.linkerName=gcc ``` -------------------------------- ### Build Full Release Version of junixsocket Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Builds the full release version of junixsocket, including all cross-compile destinations. Requires adherence to separate release instructions. ```bash mvn clean install -Dstrict -Drelease ``` -------------------------------- ### Build junixsocket with SNAPSHOT and Specific Module Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Builds junixsocket using SNAPSHOT dependencies and specifically targets the junixsocket-native-custom module. This is a workaround for missing native artifact access during SNAPSHOT builds. ```bash mvn clean install -Duse-snapshot -rf :junixsocket-native-custom ``` -------------------------------- ### List Connected Unix Sockets with netstat Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Use 'netstat --unix' to display connected Unix domain sockets. ```bash netstat --unix ``` -------------------------------- ### Check Peer Credential Support Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/peercreds.md Verify if the current platform supports peer credentials before attempting to use them. ```java AFUNIXSocket.supports(AFUNIXSocketCapability.CAPABILITY_PEER_CREDENTIALS) ``` -------------------------------- ### Compile C code using default clang Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md Use crossclang with the default clang compiler for the current architecture. The '-target default' flag uses the system's default clang. ```bash # Compile (and link) using the default clang for the current architecture crossclang/bin/clang -o test test.c -target default ``` -------------------------------- ### Build junixsocket and Skip Tests Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Builds junixsocket but skips running the tests. This can be useful for faster builds or when tests are known to be problematic. ```bash mvn clean install -DskipTests ``` -------------------------------- ### Compile C code for ARMv5 Linux Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md Use crossclang to compile a C program for ARMv5 Linux. Specify the target architecture and ABI. ```bash # Compile (and link) for ARMv5 Linux crossclang/bin/clang -o test-linuxarmv5 test.c -target armv5tel-unknown-linux-gnueabi ``` -------------------------------- ### Build junixsocket with Ignored Code Quality Checks Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Builds junixsocket with code quality checks turned off to significantly shorten build times. Use with caution as it may hide potential issues. ```bash mvn clean install -Dignorant ``` -------------------------------- ### Cross-compiling with Release Profile Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md Cross-compilation is enabled by default when using the release profile (`-Drelease`), unless explicitly disabled with `-Dcross=false`. ```bash mvn clean install -Drelease ``` -------------------------------- ### Run Junixsocket Selftest Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/demo.md Execute the selftest jar to verify junixsocket functionality on the current platform. Ensure the version number in the jar name is correct. ```bash java -jar junixsocket-selftest-X.Y.Z-jar-with-dependencies.jar ``` -------------------------------- ### Check File Descriptor Support Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Verify if the current platform supports sending and receiving file descriptors over AF_UNIX sockets using junixsocket. ```java AFSocket.supports(AFSocketCapability.CAPABILITY_FILE_DESCRIPTORS); ``` -------------------------------- ### GPG public key information Source: https://github.com/kohlschutter/junixsocket/blob/main/README.md Displays the GPG public key details used for signing Maven artifacts. ```text ed25519 2024-12-04 [SC] [expires: 2027-12-04] F2F098DD0383FE75CD5C6D3A0321BEE8AA36B734 ``` -------------------------------- ### Compile C code for x86_64 Linux Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md Use crossclang to compile a C program for x86_64 Linux. Ensure the target architecture and operating system are correctly specified. ```bash # Compile (and link) for x86_64 Linux crossclang/bin/clang -o test-linux64 test.c -target x86_64-pc-linux-gnu ``` -------------------------------- ### Build Excluding Native Library and Java 7 Support Source: https://github.com/kohlschutter/junixsocket/blob/main/BUILDING.md This command builds the project excluding the native library and disabling support for older Java 7 features via Retrolambda. Use this if you are only targeting newer Java versions. ```bash mvn clean install -Dretrolambda=false -rf :junixsocket-common ``` -------------------------------- ### Build junixsocket with Strict Code Quality Checks Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/building.md Builds junixsocket with stricter code quality checks enabled, which may reveal more potential errors. ```bash mvn clean install -Dstrict ``` -------------------------------- ### Receive File Descriptors from AFUNIXSocket Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Configure an AFUNIXSocket to receive ancillary file descriptors and retrieve them after reading data. A reasonable ancillary receive buffer size should be set. ```java AFUNIXSocket socket = ... // set ancillary receive buffer to a reasonable size (disabled by default!) socket.setAncillaryReceiveBufferSize(1024); InputStream in = socket.getInputStream(); // do you regular socket IO here in.read(...) // If there were any file descriptors sent as ancillary messages, check them right after a call to read FileDescriptor[] descriptors = socket.getReceivedFileDescriptors(); if (descriptors != null) { for (FileDescriptor fd : descriptors) { FileInputStream fin = new FileInputStream(fd); // do something with the stream } } ``` -------------------------------- ### Send File Descriptors via AFUNIXSocket Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Send one or more FileDescriptors as ancillary messages along with regular data over an AFUNIXSocket. Ensure at least one byte of data is sent to accompany the ancillary messages. ```java AFUNIXSocket socket = ... FileInputStream fin = new FileInputStream(file); socket.setOutboundFileDescriptors(fin.getFD()); // you can also send more than one FD at the same time, just make sure they're all part of the same call to setOutboundFileDescriptors. // Ancillary messages are sent _along_ regular in-band messages, so we have to send something here. os.write("Some message".getBytes("UTF-8")); ``` -------------------------------- ### Check FileDescriptor as Redirect Capability Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Verify if the environment supports using FileDescriptor as a redirect for ProcessBuilder. This check is crucial as the feature is platform-dependent and relies on Java SDK internals. ```java if (AFSocket.supports(AFSocketCapability.CAPABILITY_FD_AS_REDIRECT)) {...} ``` -------------------------------- ### Send FileDescriptor via ProcessBuilder.Redirect Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Pass a FileDescriptor as standard input to a process launched with ProcessBuilder by casting it to ProcessBuilder.Redirect. This functionality requires Java 9+ and is not supported on Windows. ```java FileDescriptor fd = ...; ProcessBuilder pb = ...; pb.redirectInput(FileDescriptorCast.using(fd).as(ProcessBuilder.Redirect.class)); Process p = pb.start(); ``` -------------------------------- ### Compile C code for x86_64 macOS Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md Use crossclang to compile a C program for x86_64 macOS. Specify the target architecture and macOS version. ```bash # Compile (and link) for x86_64 macOS crossclang/bin/clang -o test-mac64 test.c -target x86_64-apple-darwin18.2.0 ``` -------------------------------- ### Update snapshot dependencies Source: https://github.com/kohlschutter/junixsocket/blob/main/README.md Commands to refresh snapshot dependencies for Maven and Gradle projects. ```bash mvn -U dependency:resolve ``` ```bash ./gradlew refreshVersions ``` -------------------------------- ### Compile JNI code for junixsocket Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md Compile JNI code for junixsocket using crossclang. This command compiles C source files, specifies the target platform, includes necessary directories, and creates a shared library. ```bash cd junixsocket/junixsocket-native crossclang/bin/clang src/main/c/*.c \ -target (target-platform) \ -Isrc/main/c -Isrc/main/c/jni \ -shared (additional flags) \ -olibjunixtest.so ``` -------------------------------- ### Cross-compile junixsocket using Maven Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/crosscomp.md Cross-compile junixsocket for x86_64 Linux and macOS by setting the `-Dcross=true` flag when building the parent project. This flag is also enabled by default with the release profile unless explicitly disabled. ```bash cd junixsocket mvn clean install -Dcross=true ``` -------------------------------- ### Use RemoteFileInput with junixsocket-rmi Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Simplify inter-process communication of streams using junixsocket-rmi by wrapping FileInputStream with RemoteFileInput. The receiving side can then convert it back to a FileInputStream. ```java FileInputStream fin = ...; RemoteFileInput rfi = new RemoteFileInput(socketFactory, fin); // rfi can now be used for inter-process communication via RMI: someRMIService.someMethod(rfi); // on the receiving side: public void someMethod(RemoteFileInput rfi) throws IOException { FileInputStream fin = rfi.asFileInputStream(); // ... fin.close(); // closes the stream for this process only (different file handle). } ``` -------------------------------- ### Cast FileDescriptor to a Desired Class Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Instantiate FileDescriptorCast with a FileDescriptor and specify the target class to obtain an instance of that type. Ensure the FileDescriptor is valid before casting. ```java FileDescriptor fd = ...; // NOTE: check `fd.valid()` or an `IOException` may be thrown. Class desiredClass = ...; T instance = FileDescriptorCast.using(fd).as(desiredClass); ``` -------------------------------- ### Forwarding with socat Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Use socat to forward between different socket types, including Unix domain sockets and standard input/output. Supports various address types like UNIX-CONNECT and ABSTRACT. ```bash socat - UNIX-CONNECT:/path/to/socket ``` -------------------------------- ### Retrieve Peer Credentials Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/peercreds.md Obtain credentials from a connected AFUNIXSocket. Note that availability of specific properties depends on the operating system. ```java AFUNIXSocket socket = ... // Once the socket is connected, you can obtain the credentials: AFUNIXSocketCredentials credentials = socket.getPeerCredentials(); credentials.getPid(); // Returns the PID (Process ID), -1 means "could not retrieve". credentials.getUid(); // Returns the UID (User ID), 0 means "root", -1 means "could not retrieve". credentials.getGids(); // Returns the GIDs (Group IDs), the first one is the "primary" GID, null means "could not retrieve". credentials.getGid(); // Returns the primary GIDs (Group ID), same as the first entry in getGids(); -1 means "could not retrieve". credentials.getUUID(); // Returns the process binary's unique ID, null means "could not retrieve" ``` -------------------------------- ### Cast FileDescriptor to FileChannel Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Obtain a FileChannel from a FileDescriptor using FileDescriptorCast. Requires using custom FileChannelSupplier subclasses for specific access modes (ReadOnly, WriteOnly, ReadWrite). ```java FileChannel fc = FileDescriptorCast.using(fd).as(FileChannelSupplier.ReadOnly.class).get(); ``` -------------------------------- ### Authenticate RMI Connections Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/peercreds.md Use RemotePeerInfo to retrieve credentials for RMI clients or servers to verify authorization or trust. ```java // Returns the peer credentials of a client calling our RMI method // Helps answering the question "is the caller authorized"? AFUNIXSocketCredentials credentials = RemotePeerInfo.remotePeerCredentials(); // Returns the peer credentials of a server providing the given remote object // Helps answering the question "is the service trusted"? AFUNIXSocketCredentials credentials = RemotePeerInfo.remotePeerCredentials(someRemoteObject); ``` -------------------------------- ### Check FileDescriptor Availability and Cast Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Verify if a FileDescriptor can be cast to a specific class using isAvailable and then perform the cast. Throws IllegalStateException if casting is not possible, listing available types. ```java FileDescriptorCast fdc = FileDescriptorCast.using(fd); if (fdc.isAvailable(Socket.class)) { Socket socket = fdc.as(Socket.class); } else { throw new IllegalStateException("Cannot cast to Socket, only to: " + fdc.availableTypes()); } ``` -------------------------------- ### Convert Native fd Integer to FileDescriptor Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Unsafely convert a system-native file descriptor integer value to a Java FileDescriptor object. This operation may not be available on all platforms and can be disabled via system properties. ```java FileDescriptorCast.unsafeUsing(fdVal).as(FileDescriptor.class); ``` -------------------------------- ### HTTP Request over Unix Socket with curl Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Send HTTP requests to a service exposing its API over a Unix domain socket using curl. This is commonly used for services like Docker. ```bash curl --unix-socket /var/run/docker.sock http://localhost/images/json ``` -------------------------------- ### Datagram Sockets with netcat Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/unixsockets.md Some versions of netcat support datagram sockets when the '-u' parameter is added. ```bash nc -u -U /path/to/socket ``` -------------------------------- ### Cast FileDescriptor to InputStream Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Use FileDescriptorCast to obtain an InputStream from a FileDescriptor for reading. ```java InputStream in = FileDescriptorCast.using(fd).as(InputStream.class); ``` -------------------------------- ### Add Maven dependency for junixsocket-core Source: https://github.com/kohlschutter/junixsocket/blob/main/README.md Include the core functionality in a Maven project. Note that version 2.4.0 and later require the pom type. ```xml com.kohlschutter.junixsocket junixsocket-core 2.10.1 pom ``` -------------------------------- ### Cast FileDescriptor to Socket Types Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Cast a FileDescriptor to various Socket types, including Socket, AFSocket, and AFUNIXSocket. ```java Socket sock = FileDescriptorCast.using(fd).as(Socket.class); // or: AFSocket sock = FileDescriptorCast.using(fd).as(AFSocket.class); // or: AFUNIXSocket sock = FileDescriptorCast.using(fd).as(AFUNIXSocket.class); // etc. ``` -------------------------------- ### Cast FileDescriptor to Integer Source: https://github.com/kohlschutter/junixsocket/blob/main/src/site/markdown/filedescriptors.md Access the native file descriptor value as an integer. This method is not supported on all platforms, particularly Windows. ```java int fdVal = FileDescriptorCast.using(fd).as(Integer.class); // won't work for all types on Windows ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.