Telnet
The @std/net/telnet package provides a simple Telnet client.
import telnet from "@std/net/telnet"
const conn = telnet.Telnet("127.0.0.1", 2323)defer conn.close()
conn.write("ping\n")
const out = conn.read(2000)
println(out)Import
Section titled “Import”import telnet from "@std/net/telnet"Exports
Section titled “Exports”| Name | Kind | Description |
|---|---|---|
Telnet | struct | Telnet connection type. |
telnet.Telnet
Section titled “telnet.Telnet”Creates and connects a Telnet client.
Arguments:
| Name | Type | Description |
|---|---|---|
host | string | Hostname or IP address. |
port | int | TCP port. |
Returns: Telnet
import telnet from "@std/net/telnet"
const conn = telnet.Telnet("127.0.0.1", 2323)defer conn.close()conn.write
Section titled “conn.write”Writes text data to the Telnet connection.
Arguments:
| Name | Type | Description |
|---|---|---|
data | string | Data to send. |
Returns: void
conn.write("ping\n")conn.read
Section titled “conn.read”Reads one line from the Telnet connection.
Arguments:
| Name | Type | Description |
|---|---|---|
timeoutMs | int | Read timeout in milliseconds. |
Returns: string
const out = conn.read(2000)
if out != "" { println("Telnet got:", out)}read waits until a newline is received or until the timeout/deadline is reached.
conn.close
Section titled “conn.close”Closes the Telnet connection.
Returns: void
conn.close()Prefer defer so the connection is closed automatically when the current scope exits.
const conn = telnet.Telnet("127.0.0.1", 2323)defer conn.close()Complete Example
Section titled “Complete Example”import telnet from "@std/net/telnet"
const conn = telnet.Telnet("127.0.0.1", 2323)defer conn.close()
while true { const out = conn.read(2000)
if out != "" { println("Telnet got:", out) }
conn.write("ping\n") sleep(1000)}Function Syntax Note
Section titled “Function Syntax Note”Use -> only when describing a function value type.
let reader: fn(int) -> stringWhen writing a function implementation, put the return type after the parameter list.
fn readOnce(timeoutMs: int) string { return conn.read(timeoutMs)}For a function that returns nothing, use void.
fn closeConn() void { conn.close()}