This commit is contained in:
ge
2026-04-28 23:18:57 +03:00
parent 7b17a4a33b
commit e54f013ef7
11 changed files with 142 additions and 50 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ fn main() {
// Resolve the fully qualified domain name for host.
// This programm is analog for `hostname -f` command.
hostname := os.hostname()!
ai := netio.translate_addr(node: hostname, flags: netio.ai_canonname)!
ai := netio.addr_info(node: hostname, flags: netio.ai_canonname)!
mut fqdn := ''
for a in ai {
// Not needed to iterate over all entries, return the first one per getaddrinfo(3).
+6 -2
View File
@@ -4,12 +4,13 @@ fn main() {
// Create new TCP socket.
mut socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
// Close socket on exit.
defer {
socket.close() or { panic(err) }
}
// Create the server socket address.
server_addr := netio.SocketAddr.ipv4([u8(127), 0, 0, 1]!, 1081)
server_addr := netio.SocketAddr.new_ipv4([u8(127), 0, 0, 1]!, 1081)
// Connect socket to the server address.
socket.connect(server_addr) or {
@@ -22,7 +23,10 @@ fn main() {
// Send message to the server.
msg := 'Hello from client!'
sent := socket.send(msg.bytes(), 0)!
sent := socket.send(msg.bytes(), 0) or {
eprintln('SEND: ${err}')
exit(1)
}
eprintln('Sent to the server: ${sent} bytes, data: ${msg}')
+8 -7
View File
@@ -3,11 +3,11 @@ import netio
fn main() {
// We want to bind a server socket to the all available local addresses,
// (both IPv4 and IPv6) so collect the address info entries for it.
ai := netio.translate_addr(
service: '1081' // The port number for listen.
sock_type: netio.sock_stream // Address must support TCP transport.
family: netio.af_inet6 // IPv6 support.
flags: netio.ai_passive // Passive mode for binding to any address (0.0.0.0, ::).
ai := netio.addr_info(
service: '1081' // The port number to listen.
socktype: netio.sock_stream // Address must support TCP transport.
family: netio.af_inet6 // IPv6 support.
flags: netio.ai_passive // Passive mode for binding to any address (0.0.0.0, ::).
)!
// Just initialize variables.
@@ -17,7 +17,7 @@ fn main() {
// Create socket and bind to the first available address.
for a in ai {
// Create a socket with advertised parameters.
socket = netio.Socket.new(a.family, a.sock_type, a.protocol)!
socket = netio.Socket.new(a.family, a.socktype, a.protocol)!
// Set SO_REUSEADDR enabled. It allows a server to bind to a port that
// is still in a `TIME-WAIT` state from a previous connection.
@@ -49,6 +49,7 @@ fn main() {
defer {
socket.close() or { panic(err) }
}
// Start listening for incoming connections on socket.
socket.listen(10) or {
eprintln('LISTEN: ${err}')
@@ -70,7 +71,7 @@ fn main() {
}
// Get remote host and port in numeric format.
remote_host, remote_port := netio.translate_name(remote_addr,
remote_host, remote_port := netio.name_info(remote_addr,
flags: netio.ni_numerichost | netio.ni_numericserv
)!