Created
May 25, 2021 12:42
Spin up APR server socket in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.tomcat.jni.Address; | |
import org.apache.tomcat.jni.Pool; | |
import org.apache.tomcat.jni.Socket; | |
import org.apache.tomcat.jni.Library; | |
import org.apache.tomcat.jni.OS; | |
public class AprTest { | |
public static void main(String... args) throws Exception { | |
String hostname = null; | |
if (args.length == 1) | |
hostname = args[0]; | |
Library.initialize(null); | |
String tcVersion = Library.versionString(); | |
String aprVersion = Library.aprVersionString(); | |
System.out.printf("libtcnative version: %s, libapr version %s%n", tcVersion, aprVersion); | |
long rootPool = Pool.create(0); | |
long serverSockPool = Pool.create(rootPool); | |
long sockAddress = Address.info(hostname, Socket.APR_UNSPEC, 8888, 0, rootPool); | |
System.out.printf("hostname: %s, family: %d, next: %d%n", Address.getInfo(sockAddress).hostname, Address.getInfo(sockAddress).family, Address.getInfo(sockAddress).next); | |
int saFamily = Address.getInfo(sockAddress).family; | |
long serverSock = Socket.create(saFamily, | |
Socket.SOCK_STREAM, | |
Socket.APR_PROTO_TCP, rootPool); | |
int ret = 0; | |
if (Library.APR_HAVE_IPV6 && saFamily == Socket.APR_INET6) { | |
ret = Socket.optSet(serverSock, Socket.APR_IPV6_V6ONLY, 0); | |
System.out.printf("setsockopt IPV6_V6ONLY status: %d%n", ret); | |
} | |
if (OS.IS_UNIX) { | |
Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1); | |
} | |
ret = Socket.bind(serverSock, sockAddress); | |
System.out.printf("socket bind status: %d%n", ret); | |
ret = Socket.listen(serverSock, 10); | |
System.out.printf("socket listen status: %d%n", ret); | |
long socket = Socket.accept(serverSock); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment