Initial commit

This commit is contained in:
Johan Maasing 2024-11-27 10:55:10 +01:00
parent 81a0e108ab
commit 3a44c2b3ae
5 changed files with 204 additions and 0 deletions

17
JavaUDSServer/pom.xml Normal file
View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>se.rutdev.proxmig</groupId>
<artifactId>JavaUDSServer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View file

@ -0,0 +1,49 @@
package nu.zoom.checked.server;
import java.io.IOException;
import java.net.StandardProtocolFamily;
import java.net.UnixDomainSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
private void run() throws IOException {
var path = Path.of("/tmp").resolve("udsserver.sock") ;
Files.deleteIfExists(path);
var socketAddress = UnixDomainSocketAddress.of(path);
ServerSocketChannel serverChannel = ServerSocketChannel
.open(StandardProtocolFamily.UNIX);
serverChannel.bind(socketAddress);
boolean keepRunning = true;
while (keepRunning) {
try (var clientChannel = serverChannel.accept()) {
if (clientChannel != null) {
var buffer = ByteBuffer.allocate(500);
clientChannel.read(buffer);
buffer.flip();
var length = Byte.toUnsignedInt(buffer.get());
if (length < 400 && length > 0) {
var rawMessage = new byte[length];
buffer.get(rawMessage);
var message = new String(rawMessage, StandardCharsets.UTF_8);
System.out.println(message);
if (message.startsWith("q")) {
keepRunning = false;
}
}
}
}
}
serverChannel.close();
Files.deleteIfExists(path);
}
}