Added lot more example code. (0.39.8).
This commit is contained in:
parent
3b70d6c92a
commit
564999709c
32 changed files with 1353 additions and 53 deletions
40
examples/net/http_static_server.fun
Executable file
40
examples/net/http_static_server.fun
Executable file
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-25
|
||||
*/
|
||||
|
||||
#include <io/socket.fun>
|
||||
|
||||
port = 8088
|
||||
srv = TcpServer(port, 10)
|
||||
if (srv.listen() <= 0)
|
||||
print("HTTP static server: listen failed on :" + to_string(port))
|
||||
return 0
|
||||
print("HTTP static server on :" + to_string(port))
|
||||
while true
|
||||
fd = srv.accept()
|
||||
if (fd > 0)
|
||||
_ = sock_recv(fd, 4096) // ignore request
|
||||
body = "<html><body><h1>Hello from Fun static server</h1></body></html>"
|
||||
b = to_string(body)
|
||||
resp = "HTTP/1.1 200 OK\r\n"
|
||||
resp = resp + "Content-Type: text/html; charset=utf-8\r\n"
|
||||
resp = resp + "Content-Length: " + to_string(len(b)) + "\r\n"
|
||||
resp = resp + "Connection: close\r\n\r\n" + b
|
||||
sock_send(fd, resp)
|
||||
sock_close(fd)
|
||||
|
||||
/* Expected output (on start):
|
||||
HTTP static server on :8088
|
||||
|
||||
Then open http://127.0.0.1:8088/ in a browser; it will render:
|
||||
<html><body><h1>Hello from Fun static server</h1></body></html>
|
||||
*/
|
||||
27
examples/net/tcp_echo_client.fun
Executable file
27
examples/net/tcp_echo_client.fun
Executable file
|
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-25
|
||||
*/
|
||||
|
||||
#include <io/socket.fun>
|
||||
|
||||
fd = sock_connect("127.0.0.1", 9090)
|
||||
if (fd <= 0)
|
||||
print("connect failed")
|
||||
return 0
|
||||
sock_send(fd, "ping\n")
|
||||
resp = sock_recv(fd, 4096)
|
||||
print("response: " + resp)
|
||||
sock_close(fd)
|
||||
|
||||
/* Expected output (with tcp_echo_server.fun running):
|
||||
response: ping
|
||||
*/
|
||||
33
examples/net/tcp_echo_server.fun
Executable file
33
examples/net/tcp_echo_server.fun
Executable file
|
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-25
|
||||
*/
|
||||
|
||||
#include <io/socket.fun>
|
||||
|
||||
port = 9090
|
||||
srv = TcpServer(port, 10)
|
||||
if (srv.listen() <= 0)
|
||||
print("listen failed on :" + to_string(port))
|
||||
return 0
|
||||
print("Echo server on :" + to_string(port))
|
||||
while true
|
||||
fd = srv.accept()
|
||||
if (fd > 0)
|
||||
msg = sock_recv(fd, 4096)
|
||||
if (len(msg) > 0) sock_send(fd, msg)
|
||||
sock_close(fd)
|
||||
|
||||
/* Expected output (on start):
|
||||
Echo server on :9090
|
||||
|
||||
Then, from another shell: `nc 127.0.0.1 9090` and type "ping" — the server echoes it back.
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue