TCP is also doable in Hare, but given the nature of TCP it is a bit more involved to set up and manage.
use fmt;
use net;
use net::tcp;
use net::ip;
use strings;
use io;
export fn main() void = {
// Parse an address we want to connect to.
const addr = ip::parse("0.0.0.0")!;
// Create a tcp connection to that address and port.
const conn = tcp::connect(addr, 9000)!;
// Since tcp is persisted as a stream we need to make sure we
// close our connection when finished to free up the resources.
defer net::close(conn)!;
// Create a message to send over the connection.
const msg = strings::toutf8("Hello, from TCP Hare client!");
// Again, since this tcp connection is stream-based we can use
// the io modile to read and write data to it.
const sz = io::write(conn, msg)!;
fmt::printf("Send {} bytes", sz)!;
};
# First, start a tcp listener with netcat
$ nc -l -p 9000
Hello, from TCP Hare client!
# Second, run the example to connect to that tcp listener
$ hare run tcp-client.ha
Send 28 bytes
Back to table of contents