cromo La guida di Cromo
Scarica PDF EN

std.net

TCP connections and listeners, UDP sockets, TLS and the names of the hosts. A call waits until it is done, as in C, or until the timeout set on its socket with timeout(), and then it throws. In a task, the wait leaves the thread to the other tasks, so a server can give each connection a task of its own. An error of the network is an exception of type Error, whose message says what failed and why, as "cannot connect to example.com:80: connection refused".

cromo
net.Connection site = net.connectTls("example.com", 443)
site.timeout(10000)
site.writeText("GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
println("{}", string(site.read(200)))

The main types are Listener, Connection and Udp. A name is resolved on Linux from /etc/hosts, then by the servers of /etc/resolv.conf, and on Windows by the resolver of the system; IPv4 and IPv6 work on both. TLS comes from mbedTLS, inside the runtime: the executables need no other library.

The guide: Networking and HTTP.

Generata dalla documentazione nei sorgenti della libreria, che è in inglese.

#Tipi

#Listener

cromo
struct Listener

Waits for connections on an address and a port, made by listen or listenTls: accept() gives them one by one. The destructor closes it. It can be moved, as into a task, but not copied.

cromo
net.Listener listener = net.listen("0.0.0.0", 7000)
thread.Group clients
for {
    net.Connection client = listener.accept()
    clients.start([move client]() {
        client.write(client.read(1024))  // an echo
    })
}

#Listener.accept

cromo
Connection accept() const

The next connection, waiting for it; with a timeout, an exception if none arrives in time. An exception also when the listener is closed, or the system refuses the connection. With TLS, the handshake of the connection comes with its first read or write, in the task that serves it, so that a slow client does not keep the others waiting. It is const: several threads or tasks can accept from the same listener.

#Listener.timeout

cromo
void timeout(i64 milliseconds)

Sets how long accept() waits for a connection, in milliseconds; -1, the default, waits as long as it takes.

#Listener.port

cromo
i32 port() const

The port it listens on, also when 0 asked the system for a free one.

#Listener.close

cromo
void close()

Stops listening, and then accept() throws; the destructor does it too, and a second call does nothing. The connections already accepted stay open.

#Connection

cromo
struct Connection

A connection of TCP, from connect or accept(), encrypted when it comes from connectTls or listenTls, with the same read and write. Each call waits until it is done, or until the timeout; in a task, the wait leaves the thread to the other tasks. The destructor closes it. It can be moved, as into a task, but not copied.

cromo
net.Connection connection = net.connect("127.0.0.1", 7000)
connection.timeout(5000)
connection.writeText("hello")
list<u8> answer = connection.read(1024)  // empty if the other side has closed

#Connection.read

cromo
list<u8> read(i64 most)

Up to most bytes, as soon as some arrive, which can be fewer than those the other side wrote: an empty list when the other side has closed the connection. With TLS, also when it has ended without saying so. An exception on a timeout, on an error of the network or of TLS, as a handshake that fails, and after close().

#Connection.write

cromo
void write(list<u8> bytes)

Writes all the bytes, waiting while the system has no room for them; nothing is kept after it returns. An exception on a timeout, on an error of the network, as a connection that the other side has reset, and after close().

#Connection.writeText

cromo
void writeText(string text)

Writes the bytes of the text, UTF-8, as write does.

#Connection.startTls

cromo
void startTls(string host)

Starts TLS on a connection that connect gave, for the protocols that begin in clear and then ask for it, as SMTP and PostgreSQL: from then on read and write are encrypted. The certificate of the server must come from an authority that the system trusts, and name host, as for connectTls: otherwise an exception says why. On a connection that has TLS already it is a panic.

#Connection.timeout

cromo
void timeout(i64 milliseconds)

Sets how long each following read or write waits for the other side, in milliseconds, also in the handshake of TLS of a server; past it the call throws. -1, the default, waits as long as it takes.

#Connection.close

cromo
void close()

Closes the connection, and then read and write throw; the destructor does it too, and a second call does nothing. With TLS, it says close_notify to the other side first, if it can, ignoring the errors.

#Datagram

cromo
struct Datagram

A message of UDP, with the address it comes from.

  • list<u8> data: the bytes of the message
  • string host: the address of the sender, as "192.168.1.2" or "fe80::1"
  • i32 port: the port of the sender

#Udp

cromo
struct Udp

A socket of UDP, bound to an address and a port, made by udp: it sends and receives messages, each whole or not at all, with no connection and no promise that they arrive, nor in which order. The destructor closes it. It can be moved, not copied.

cromo
net.Udp socket = net.udp("0.0.0.0", 0)
socket.timeout(2000)
socket.send("192.168.1.20", 9000, "ping".bytes())
net.Datagram answer = socket.receive(1500)
println("{} bytes from {}:{}", answer.data.len(), answer.host, answer.port)

#Udp.send

cromo
void send(string host, i32 port, list<u8> bytes)

Sends the bytes as one message to the port of a host, a name or an address of the same version of IP as the socket. An exception if the host is not found or has no address of that version, on a timeout, or on an error of the network; none if the message is lost on the way.

#Udp.receive

cromo
Datagram receive(i64 most)

The next message, of at most most bytes, and who sent it; with a timeout, an exception if none arrives in time. Give room for the largest message: a longer one is cut to most bytes on Linux, and is an exception on Windows.

#Udp.timeout

cromo
void timeout(i64 milliseconds)

Sets how long send and receive wait, in milliseconds; past it they throw. -1, the default, waits as long as it takes.

#Udp.port

cromo
i32 port() const

The port it is bound to, also when 0 asked the system for a free one.

#Udp.close

cromo
void close()

Closes the socket, and then send and receive throw; the destructor does it too, and a second call does nothing.

#Ping

cromo
struct Ping

What ping found.

  • bool answered: whether the host answered before the timeout
  • string address: the address that was asked: "192.168.1.1"
  • time.Duration time: from the question to the answer; nothing without an answer

#Funzioni

#listen

cromo
Listener listen(string host, i32 port)

A listener on a name or an address, as "127.0.0.1" for this computer alone or "0.0.0.0" for all its addresses of IPv4 ("::" for IPv6); port 0 asks the system for a free one, which port() gives. A name with several addresses listens on the first. An exception if the name is not found or the port is taken, as "cannot use 0.0.0.0 port 8080: the address is in use".

#listenTls

cromo
Listener listenTls(string host, i32 port, string certificate, string key)

A listener as listen gives, whose connections encrypt with TLS: certificate is the text in PEM of the certificate of the server, followed by those of its chain, and key the text in PEM of its private key. An exception if they cannot be read. The handshake of each connection comes with its first read or write.

#connect

cromo
Connection connect(string host, i32 port)

A connection to the port of a name or an address, trying the addresses of the name in turn until one answers. An exception if the name is not found, or with the error of the last address if none answers, as "cannot connect to example.com:80: connection refused". It has no timeout of its own: it waits as long as the system does, and connectTimeout sets one.

#connectTimeout

cromo
Connection connectTimeout(string host, i32 port, i64 milliseconds)

A connection as connect gives, which waits at most milliseconds for the server to take it, for all the addresses of the name together: past them, an exception that says "timed out". A server that is off, or behind a firewall that says nothing, would otherwise wait as long as the system does, a minute or more. -1 waits as connect. The time to find the addresses of a name is not counted. The connection has no timeout for its reads and writes until timeout sets one.

#connectTls

cromo
Connection connectTls(string host, i32 port)

A connection with TLS to the port of a name or an address, as connect gives, after the handshake. The certificate of the server must come from an authority that the system trusts, and name the host: otherwise an exception says why, as "the certificate has expired". As connect, it has no timeout of its own, which connectTlsTimeout sets, in the handshake too.

#connectTlsTimeout

cromo
Connection connectTlsTimeout(string host, i32 port, i64 milliseconds)

A connection with TLS as connectTls gives, which waits at most milliseconds for the server to take the connection, and then as much for each read and write of the handshake. The connection keeps that timeout for its reads and writes, until timeout sets another. -1 waits as connectTls.

#udp

cromo
Udp udp(string host, i32 port)

A socket of UDP bound to the port of a name or an address, as "0.0.0.0" for all the addresses of IPv4 or "::" for those of IPv6; port 0 asks the system for a free one. The socket sends to the addresses of its version of IP. An exception if the name is not found or the port is taken.

#resolve

cromo
list<string> resolve(string host)

The addresses of a name, as text: "93.184.215.14", "2606:2800::1". An address written with numbers gives itself. An exception if the name is not found.

#ping

cromo
Ping ping(string host, i64 timeout)

Asks a host whether it is there, with an echo of ICMP as the command ping, and waits for its answer up to timeout milliseconds. host is a name or an address, of IPv4 or IPv6; a name is asked at its first address.

cromo
net.Ping router = net.ping("192.168.1.1", 1000)
if (router.answered) {
    println("{} answered in {} ms", router.address, router.time.milliseconds())
}

A host that does not answer is answered false, not an exception: it is off, out of reach, or it does not answer the pings. An exception if the name is not found, or if the system does not let the program send a ping: on Linux a program without the rights of root sends one when its group is in net.ipv4.ping_group_range, as it is on most systems. On Windows the wait is of the thread, also in a task.