本文へ移動
mie リファレンス
REPL Playground

std:net リファレンス

@import 'std:net' で取得する TCP クライアントモジュール。connect / connect_tls で Conn ハンドルを得て read / read_exact / write / close する。

connect

connect(host, port)
connect: { String, Int | Future(Result(Conn)) }

平文 TCP で host:port へ接続し Conn ハンドルを得る。

>>> c := net.connect("example.com", 80)!
>>> c.close()!

connect_tls

connect_tls(host, port)
connect_tls: { String, Int | Future(Result(Conn)) }

TLS で host:port へ接続する (システムルート CA・ホスト名検証)。

>>> c := net.connect_tls("example.com", 443)!
>>> c.close()!

Conn メソッド

close

c.close()
close: { Future(Result(Unit)) }

切断して封印する。以降のメソッド呼び出しは object.Error。

>>> c := net.connect("example.com", 80)!
>>> c.close()!

read

c.read(n)
read: { Int | Future(Result(Bytes)) }

最大 n bytes 読む。Ok の空 Bytes は EOF (相手が閉じた)。

>>> c := net.connect("example.com", 80)!
>>> data := c.read(1024)!
>>> print data

read_exact

c.read_exact(n)
read_exact: { Int | Future(Result(Bytes)) }

ちょうど n bytes 読む。揃う前に EOF なら Err(eof)。

>>> c := net.connect("example.com", 80)!
>>> head := c.read_exact(4)!
>>> print head

set_timeout

c.set_timeout(ms)
set_timeout: { Int | Unit }

以降の read / write の期限をミリ秒で設定する (0 で解除)。同期・即値 ()。

>>> c := net.connect("example.com", 80)!
>>> c.set_timeout(5000)!

write

c.write(bytes)
write: { Bytes | Future(Result(Unit)) }

bytes を全量書く。

>>> c := net.connect("example.com", 80)!
>>> payload := fs.read_bytes("req.bin")!
>>> c.write(payload)!