dogma:net
import { tcp_connect, tcp_send, tcp_recv, tcp_close } from "dogma:net"
The host must grant the dogma:net capability for these functions to be available. See the Embedding Guide.
Socket handles are i32 values returned by tcp_connect. Pass the handle to other functions to operate on the same connection.
Functions🔗
| Function | Signature | Description |
|---|---|---|
tcp_connect | (host: String, port: i32) -> i32 | Connect to TCP server, return socket handle |
tcp_send | (socket: i32, data: String) -> i32 | Send string, return bytes sent |
tcp_send_bytes | (socket: i32, data: [i32]) -> i32 | Send raw bytes, return bytes sent |
tcp_recv | (socket: i32, size: i32) -> String | Receive up to size bytes as string |
tcp_recv_bytes | (socket: i32, size: i32) -> [i32] | Receive up to size bytes as byte array |
tcp_close | (socket: i32) | Close socket |
tcp_set_timeout | (socket: i32, ms: i32) | Set read/write timeout in milliseconds |
Example🔗
import { tcp_connect, tcp_send, tcp_recv, tcp_close } from "dogma:net"
import { println } from "dogma:core"
fn main() {
let sock = tcp_connect("example.com", 80)
tcp_send(sock, "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n")
let response = tcp_recv(sock, 4096)
tcp_close(sock)
println(response)
}