The net
module provides functionalities for network communication, including DNS lookup, TCP and UDP address resolution, and various methods for establishing network connections.
Performs a DNS lookup for the given host.
host
: The hostname to lookup.
Returns an array of IP addresses associated with the given host.
Resolves a TCP address.
network
: Network type (e.g., "tcp", "tcp4", "tcp6").address
: The address to resolve.
Returns a string representation of the resolved TCP address.
Resolves a UDP address.
network
: Network type (e.g., "udp", "udp4", "udp6").address
: The address to resolve.
Returns a string representation of the resolved UDP address.
Establishes a generic network connection.
network
: Network type (e.g., "tcp", "udp").address
: The address to connect to.
Returns a connection object with methods for interaction.
Establishes a TCP network connection.
network
: Network type (e.g., "tcp", "tcp4", "tcp6").address
: The address to connect to.
Returns a connection object with methods for interaction.
Closes the network connection.
Reads data from the connection into the buffer.
buffer
: The buffer to read data into.
Returns the number of bytes read.
Writes data from the buffer to the connection.
buffer
: The buffer containing data to write.
Returns the number of bytes written.
Returns the local address of the connection.
Returns the remote address of the connection.
Sets the read and write deadlines for the connection.
t
: The deadline time.
Sets the read deadline for the connection.
t
: The read deadline time.
Sets the write deadline for the connection.
t
: The write deadline time.
import "net"
// DNS Lookup
addresses := net.dnslookup("example.com")
println(addresses)
// Resolve TCP Address
tcp_addr := net.resolve_tcp_addr("tcp", "example.com:80")
println(tcp_addr)
// Resolve UDP Address
udp_addr := net.resolve_udp_addr("udp", "example.com:80")
println(udp_addr)
// Dial a TCP Connection
conn := net.dialtcp("tcp", "example.com:80")
if !is_error(conn) {
println("Connected to: ", conn.remote_addr())
// Write to the connection
msg := bytes("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
n := conn.write(msg)
println("Bytes written: ", n)
// Read from the connection
buf := bytes(1024)
n = conn.read(buf)
println("Response: ", string(buf[:n]))
// Close the connection
conn.close()
}