tcp server
This commit is contained in:
@@ -3,7 +3,18 @@ func main[] : I64
|
|||||||
if s < 0
|
if s < 0
|
||||||
panic("socket() failed")
|
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
|
if connect(s, sa, 16) < 0
|
||||||
panic("connect() failed")
|
panic("connect() failed")
|
||||||
free(sa)
|
free(sa)
|
||||||
|
|||||||
27
examples/tcp_server.zr
Normal file
27
examples/tcp_server.zr
Normal 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)
|
||||||
@@ -130,6 +130,9 @@ extern socket
|
|||||||
extern send
|
extern send
|
||||||
extern read
|
extern read
|
||||||
extern close
|
extern close
|
||||||
|
extern bind
|
||||||
|
extern listen
|
||||||
|
extern accept
|
||||||
|
|
||||||
section .text._builtin_deref
|
section .text._builtin_deref
|
||||||
_builtin_deref:
|
_builtin_deref:
|
||||||
@@ -251,28 +254,6 @@ _builtin_array_free:
|
|||||||
mov rdi, rbx
|
mov rdi, rbx
|
||||||
pop rbx
|
pop rbx
|
||||||
jmp free
|
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(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user