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
JavaUDSClient/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>JavaUDSClient</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,40 @@
package nu.zoom.checked.client;
import java.io.IOException;
import java.net.StandardProtocolFamily;
import java.net.UnixDomainSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
private void run() throws IOException {
try (var clientChannel = SocketChannel.open(StandardProtocolFamily.UNIX)) {
var serverPath = Path.of("/tmp").resolve("udsserver.sock") ;
var serverAddress = UnixDomainSocketAddress.of(serverPath);
clientChannel.connect(serverAddress);
var clientMessage = "quit från - " + UUID.randomUUID();
sendMessage(clientMessage, clientChannel);
}
}
private static void sendMessage(String clientMessage, SocketChannel clientChannel) throws IOException {
var messageBytes = clientMessage.getBytes(StandardCharsets.UTF_8);
var messageBuffer = ByteBuffer.allocate(500);
messageBuffer.put((byte) messageBytes.length);
messageBuffer.put(messageBytes);
messageBuffer.flip();
clientChannel.write(messageBuffer);
}
static class MessageToLongException extends Exception {}
}