Basic sockets (0.21.0)
This commit is contained in:
parent
94602f0855
commit
d546632cd4
19 changed files with 667 additions and 4 deletions
0
examples/datetime_basic.fun
Normal file → Executable file
0
examples/datetime_basic.fun
Normal file → Executable file
0
examples/regex_demo.fun
Normal file → Executable file
0
examples/regex_demo.fun
Normal file → Executable file
0
examples/regex_procedural.fun
Normal file → Executable file
0
examples/regex_procedural.fun
Normal file → Executable file
34
examples/tcp_http_get.fun
Executable file
34
examples/tcp_http_get.fun
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/*
|
||||
* Simple TCP client in Fun: fetches HTTP/1.0 from example.org
|
||||
* Requires network access. Demonstrates tcp_connect, sock_send, sock_recv, sock_close.
|
||||
*/
|
||||
|
||||
host = "example.org"
|
||||
port = 80
|
||||
|
||||
fd = tcp_connect(host, port)
|
||||
if (fd <= 0)
|
||||
print("connect failed")
|
||||
else
|
||||
req = join([
|
||||
"GET / HTTP/1.0\r\n",
|
||||
"Host: ", host, "\r\n",
|
||||
"User-Agent: fun/0.20\r\n",
|
||||
"Connection: close\r\n\r\n"
|
||||
], "")
|
||||
sent = sock_send(fd, req)
|
||||
// Read up to 8192 bytes (first chunk)
|
||||
data = sock_recv(fd, 8192)
|
||||
print(data)
|
||||
sock_close(fd)
|
||||
30
examples/tcp_http_get_class.fun
Executable file
30
examples/tcp_http_get_class.fun
Executable file
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
#include <io/socket.fun>
|
||||
|
||||
// Simple HTTP GET using TcpClient class from stdlib
|
||||
|
||||
c = TcpClient()
|
||||
if (!c.connect("example.org", 80))
|
||||
print("connect failed")
|
||||
else
|
||||
req = join([
|
||||
"GET / HTTP/1.0\r\n",
|
||||
"Host: example.org\r\n",
|
||||
"User-Agent: fun/0.21\r\n",
|
||||
"Connection: close\r\n",
|
||||
"\r\n"
|
||||
], "")
|
||||
c.send(req)
|
||||
resp = c.recv_all(8192)
|
||||
print(resp)
|
||||
c.close()
|
||||
Loading…
Add table
Add a link
Reference in a new issue