diff --git a/examples/tcp_client.zr b/examples/tcp_client.zr index 3a33c8b..d6a013f 100644 --- a/examples/tcp_client.zr +++ b/examples/tcp_client.zr @@ -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) diff --git a/examples/tcp_server.zr b/examples/tcp_server.zr new file mode 100644 index 0000000..ded97d6 --- /dev/null +++ b/examples/tcp_server.zr @@ -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) \ No newline at end of file diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index 88bd50b..7372539 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -130,6 +130,9 @@ extern socket extern send extern read extern close +extern bind +extern listen +extern accept section .text._builtin_deref _builtin_deref: @@ -251,28 +254,6 @@ _builtin_array_free: mov rdi, rbx pop rbx jmp free - -_builtin_sa_from_addr: - push r15 - push r14 - push rbx - mov rbx, rsi - mov r14, rdi - push 16 - pop rdi - call malloc - mov r15, rax - mov dword [rax], 2 - rol bx, 8 - mov word [rax+2], bx - mov rdi, r14 - call inet_addr - mov dword [r15+4], eax - mov rax, r15 - pop rbx - pop r14 - pop r15 - ret " ); Ok(())