tcp client
This commit is contained in:
20
examples/tcp_client.zr
Normal file
20
examples/tcp_client.zr
Normal file
@@ -0,0 +1,20 @@
|
||||
func main[] : I64
|
||||
let s: I64 = socket(2, 1, 0)
|
||||
if s < 0
|
||||
panic("socket() failed")
|
||||
|
||||
let sa: Ptr = _builtin_sa_from_addr("23.192.228.80", 80)
|
||||
if connect(s, sa, 16) < 0
|
||||
panic("connect() failed")
|
||||
free(sa)
|
||||
|
||||
let req: String = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
|
||||
send(s, req, strlen(req), 0)
|
||||
|
||||
let resp: String = malloc(60000)
|
||||
let n: I64 = read(s, resp, 60000)
|
||||
String.set(resp, n, 0)
|
||||
print(resp)
|
||||
free(resp)
|
||||
|
||||
close(s)
|
||||
@@ -124,6 +124,12 @@ extern readdir
|
||||
extern closedir
|
||||
extern exit
|
||||
extern gettimeofday
|
||||
extern connect
|
||||
extern inet_addr
|
||||
extern socket
|
||||
extern send
|
||||
extern read
|
||||
extern close
|
||||
|
||||
section .text._builtin_deref
|
||||
_builtin_deref:
|
||||
@@ -245,6 +251,28 @@ _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(())
|
||||
|
||||
@@ -11,8 +11,8 @@ func print_i64[x: I64] : I64
|
||||
func String.nth[s: String, n: I64] : U8
|
||||
return _builtin_deref(s + n)
|
||||
|
||||
func String.set[s: String, n: I64] : I64
|
||||
_builtin_string_set(s, n)
|
||||
func String.set[s: String, n: I64, c: U8] : I64
|
||||
_builtin_string_set(s, n, c)
|
||||
|
||||
func String.is_whitespace[c: U8] : Bool
|
||||
return c == ' ' || c == 10 || c == 13 || c == 9
|
||||
|
||||
Reference in New Issue
Block a user