Added some more server socket fun and examples. (0.27.0)
This commit is contained in:
parent
6d9b082945
commit
92d669a409
8 changed files with 125 additions and 4 deletions
53
examples/extra/pcsc2_example.fun
Executable file
53
examples/extra/pcsc2_example.fun
Executable file
|
|
@ -0,0 +1,53 @@
|
|||
#!/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
|
||||
*/
|
||||
|
||||
// Example using stdlib PCSC2 class.
|
||||
// Establish context, list readers, connect to first (if any),
|
||||
// send a sample SELECT MF APDU, print results, and clean up.
|
||||
|
||||
#include <io/pcsc2.fun>
|
||||
|
||||
pc = PCSC2()
|
||||
|
||||
ctx = pc.establish()
|
||||
print("ctx=" + to_string(ctx))
|
||||
|
||||
readers = pc.list_readers(ctx)
|
||||
print("readers=" + to_string(readers))
|
||||
|
||||
number h = 0
|
||||
if len(readers) > 0
|
||||
h = pc.connect(ctx, readers[1])
|
||||
print("handle=" + to_string(h))
|
||||
|
||||
if h != 0
|
||||
// APDU 1: SELECT applet by AID (should return 251 data bytes, constant)
|
||||
resp = pc.transmit_hex(h, "00a4040c0cD2760001354B414E4D30310000")
|
||||
print(to_string(resp))
|
||||
print("resp.data_hex=" + resp["data_hex"])
|
||||
print("resp.sw1=" + to_string(resp["sw1"]) + " sw2=" + to_string(resp["sw2"]) + " code=" + to_string(resp["code"]))
|
||||
number dlen1 = len(resp["data_hex"]) / 2
|
||||
print("SELECT AID data length=" + to_string(dlen1) + " (expected 251)")
|
||||
|
||||
// APDU 2: GET CHALLENGE 8 (should return 8 random bytes)
|
||||
resp = pc.transmit_hex(h, "0084000008")
|
||||
print(to_string(resp))
|
||||
print("resp.data_hex=" + resp["data_hex"])
|
||||
print("resp.sw1=" + to_string(resp["sw1"]) + " sw2=" + to_string(resp["sw2"]) + " code=" + to_string(resp["code"]))
|
||||
number dlen2 = len(resp["data_hex"]) / 2
|
||||
print("GET CHALLENGE data length=" + to_string(dlen2) + " (expected 8)")
|
||||
|
||||
_ = pc.disconnect(h)
|
||||
|
||||
_ = pc.release(ctx)
|
||||
print("done")
|
||||
44
examples/extra/pcsc_demo.fun
Executable file
44
examples/extra/pcsc_demo.fun
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
#!/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-02
|
||||
*/
|
||||
|
||||
// PCSC demo using stdlib PCSC class.
|
||||
// Establishes context, lists readers, optionally connects to the first,
|
||||
// transmits a sample APDU (SELECT MF), and cleans up.
|
||||
|
||||
#include <io/pcsc.fun>
|
||||
|
||||
pc = PCSC()
|
||||
|
||||
ctx = pc.establish()
|
||||
print("ctx=" + to_string(ctx))
|
||||
|
||||
readers = pc.list_readers(ctx)
|
||||
print("readers=" + to_string(readers))
|
||||
|
||||
number h = 0
|
||||
if len(readers) > 0
|
||||
// Connect to first reader
|
||||
h = pc.connect(ctx, readers[1])
|
||||
print("handle=" + to_string(h))
|
||||
|
||||
if h != 0
|
||||
// Sample APDU: SELECT MF (00 A4 00 00 02 3F 00)
|
||||
resp = pc.transmit_hex(h, "00A40000023F00")
|
||||
// Print hex data and SW
|
||||
print("resp.data_hex=" + resp["data_hex"])
|
||||
print("resp.sw1=" + to_string(resp["sw1"]) + " sw2=" + to_string(resp["sw2"]) + " code=" + to_string(resp["code"]))
|
||||
|
||||
_ = pc.disconnect(h)
|
||||
|
||||
_ = pc.release(ctx)
|
||||
print("done")
|
||||
37
examples/extra/tcp_echo_server.fun
Executable file
37
examples/extra/tcp_echo_server.fun
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
#!/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-13
|
||||
*/
|
||||
|
||||
// Simple TCP echo server using built-in socket ops.
|
||||
// Listens on localhost:12345 and echoes a single line/request back to each client.
|
||||
|
||||
port = 12345
|
||||
backlog = 16
|
||||
|
||||
listen_fd = tcp_listen(port, backlog)
|
||||
if (listen_fd <= 0)
|
||||
print("tcp_listen failed")
|
||||
exit(1)
|
||||
|
||||
print("Echo server listening on port " + to_string(port))
|
||||
|
||||
while (true)
|
||||
client = tcp_accept(listen_fd)
|
||||
if (client > 0)
|
||||
data = sock_recv(client, 4096)
|
||||
if (len(data) > 0)
|
||||
// Echo back exactly what we received
|
||||
sock_send(client, data)
|
||||
sock_close(client)
|
||||
|
||||
// Not reached in this example, but here for completeness
|
||||
sock_close(listen_fd)
|
||||
32
examples/extra/tcp_echo_server_class.fun
Executable file
32
examples/extra/tcp_echo_server_class.fun
Executable file
|
|
@ -0,0 +1,32 @@
|
|||
#!/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-13
|
||||
*/
|
||||
|
||||
// Echo server using the stdlib TcpServer class.
|
||||
// Make sure FUN_LIB_DIR points to the ./lib directory containing io/socket.fun.
|
||||
// Example:
|
||||
// FUN_LIB_DIR="$(pwd)/lib" ./build/fun ./examples/tcp_echo_server_class.fun
|
||||
|
||||
include <io/socket.fun>
|
||||
|
||||
print("Starting echo server at localhost:12345")
|
||||
|
||||
server = TcpServer(12345, 16)
|
||||
|
||||
if (server.listen() <= 0)
|
||||
print("Listen failed!")
|
||||
exit(1)
|
||||
else
|
||||
print("Echo server (class) listening on port 12345")
|
||||
|
||||
// Serve clients forever, echoing back what they send.
|
||||
server.serve_forever(4096)
|
||||
Loading…
Add table
Add a link
Reference in a new issue