From 2095935c2488d1a46c7dd0c9d2325e41715b8ad9 Mon Sep 17 00:00:00 2001 From: rc_05 Date: Fri, 24 May 2024 23:03:55 +0200 Subject: First commit --- examples/Client.hx | 19 +++++++++++++++++++ examples/Server.hx | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 examples/Client.hx create mode 100644 examples/Server.hx (limited to 'examples') diff --git a/examples/Client.hx b/examples/Client.hx new file mode 100644 index 0000000..675fe94 --- /dev/null +++ b/examples/Client.hx @@ -0,0 +1,19 @@ +package examples; + +import haxe.io.Bytes; +import unix.UnixSocket; + +class Client { + static function main() { + var server = new UnixSocket("haxe.sock"); + server.init(); + server.connect(); + + server.writeBytes(Bytes.ofString("Ping")); + + var receivedData = server.readBytes("Pong".length); + trace(receivedData); + + server.close(); + } +} diff --git a/examples/Server.hx b/examples/Server.hx new file mode 100644 index 0000000..87e00a4 --- /dev/null +++ b/examples/Server.hx @@ -0,0 +1,29 @@ +package examples; + +import haxe.io.Bytes; +import sys.FileSystem; +import unix.UnixSocket; + +class Server { + static function main() { + var server = new UnixSocket("haxe.sock"); + + server.init(); + server.bind(); + server.listen(5); + + while (true) { + trace('Waiting for clients to connect...'); + var client = server.accept(); + trace('Accepted connection from client with fd ${client.fileDescriptor}'); + var receivedData = client.readBytes("Ping".length).toString(); + trace('$receivedData'); + client.writeBytes(Bytes.ofString("Pong")); + client.close(); + } + + if (FileSystem.exists("haxe.sock")) { + FileSystem.deleteFile("haxe.sock"); + } + } +} -- cgit v1.2.3