tcp server

This commit is contained in:
2025-06-30 12:15:06 +02:00
parent a8ce309eac
commit 152e0189fe
3 changed files with 42 additions and 23 deletions

View File

@@ -3,7 +3,18 @@ func main[] : I64
if s < 0
panic("socket() failed")
let sa: Ptr = _builtin_sa_from_addr("23.192.228.80", 80)
let port: I64 = 80
let sa: Ptr = calloc(16)
String.set(sa, 0, 2)
String.set(sa, 1, 0)
String.set(sa, 2, Bit.rshift(port, 8) && 255)
String.set(sa, 3, port && 255)
// 23.192.228.80
String.set(sa, 4, 23)
String.set(sa, 5, 192)
String.set(sa, 6, 228)
String.set(sa, 7, 80)
if connect(s, sa, 16) < 0
panic("connect() failed")
free(sa)

27
examples/tcp_server.zr Normal file
View File

@@ -0,0 +1,27 @@
func main[] : I64
let s: I64 = socket(2, 1, 0)
if s < 0
panic("socket() failed")
let port: I64 = 8080
let sa: Ptr = calloc(16)
String.set(sa, 0, 2)
String.set(sa, 1, 0)
String.set(sa, 2, Bit.rshift(port, 8) && 255)
String.set(sa, 3, port && 255)
if bind(s, sa, 16) < 0
panic("bind() failed")
if listen(s, 1) < 0
panic("listen() failed")
let resp: String = malloc(60000)
while true
let c: I64 = accept(s, 0, 0)
if c < 0
panic("accept() failed")
let n: I64 = read(c, resp, 60000)
send(c, resp, n, 0)
close(c)