-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunix_socket_echo.fun
More file actions
executable file
·70 lines (60 loc) · 1.85 KB
/
unix_socket_echo.fun
File metadata and controls
executable file
·70 lines (60 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env fun
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-10-04
*/
/*
* UNIX domain socket echo demo.
* - On Unix-like systems: demonstrates unix_listen + tcp_accept + unix_connect + sock_send/recv.
* - On Windows: prints a friendly message and exits (since AF_UNIX is not available here).
*/
#include <io/socket.fun>
fun server_thread(listen_fd)
// Accept a single client, read message, echo back, close.
cfd = tcp_accept(to_number(listen_fd))
if (cfd <= 0)
// accept failed
return 0
msg = sock_recv(cfd, 4096)
// Simple echo prefix
_ = sock_send(cfd, join(["echo:", msg], ""))
_ = sock_close(cfd)
return 1
// Cross-platform guard: Windows sets OS=Windows_NT
os = env("OS")
if (os == "Windows_NT")
print("UNIX domain sockets are not supported on Windows in this build. Skipping demo.")
else
// Path for the UNIX socket
path = "/tmp/fun_unix_echo.sock"
// Start a UNIX listening socket
lfd = unix_listen(path, 1)
if (lfd <= 0)
print(join(["unix_listen failed at ", path], ""))
else
// Spawn the server thread which will accept exactly one client
tid = thread_spawn(server_thread, lfd)
// Small delay to ensure the server is accepting
sleep(50)
// Create a client using the UnixClient from stdlib
uc = UnixClient()
if (!uc.connect(path))
print("unix_connect failed")
else
// Send a message and read the reply
_ = uc.send("hello over AF_UNIX")
reply = uc.recv(4096)
print(reply)
_ = uc.close()
// Join the server thread and close the listening socket
_ = thread_join(tid)
_ = sock_close(lfd)
/* Expected output:
echo:hello over AF_UNIX
*/