upd17
This commit is contained in:
Executable
BIN
Binary file not shown.
@@ -1,5 +1,16 @@
|
|||||||
import netio
|
import netio
|
||||||
|
|
||||||
|
// This program starts a server that listens for TCP connections on port 1081.
|
||||||
|
// The program listens on all addresses available in the operating system,
|
||||||
|
// including IPv4 and IPv6.
|
||||||
|
//
|
||||||
|
// Run the program and try connecting using the telnet utility:
|
||||||
|
//
|
||||||
|
// telnet 127.0.0.1 1081 # IPv4
|
||||||
|
// telnet ::1 1081 # IPv6
|
||||||
|
//
|
||||||
|
// This program fails if operation system does not support IPv6 or IPv6 is disabled.
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// We want to bind a server socket to the all available local addresses,
|
// 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.
|
// (both IPv4 and IPv6) so collect the address info entries for it.
|
||||||
@@ -58,6 +69,7 @@ fn main() {
|
|||||||
|
|
||||||
println('Listening on ${listen_addr}...')
|
println('Listening on ${listen_addr}...')
|
||||||
|
|
||||||
|
for {
|
||||||
// Accept the connection from remote. This is a blocking call.
|
// Accept the connection from remote. This is a blocking call.
|
||||||
// conn will store the new socket connected to the remote.
|
// conn will store the new socket connected to the remote.
|
||||||
conn, remote_addr := socket.accept() or {
|
conn, remote_addr := socket.accept() or {
|
||||||
@@ -75,7 +87,7 @@ fn main() {
|
|||||||
flags: netio.ni_numerichost | netio.ni_numericserv
|
flags: netio.ni_numerichost | netio.ni_numericserv
|
||||||
)!
|
)!
|
||||||
|
|
||||||
eprintln('Accpeted connection. Remote address: ${remote_host}, remote port: ${remote_port}')
|
eprintln('Accepted connection. Remote address: ${remote_host}, remote port: ${remote_port}')
|
||||||
|
|
||||||
// Read 512 bytes of data from socket.
|
// Read 512 bytes of data from socket.
|
||||||
mut buf := []u8{len: 512} // Initialize the buffer to store message.
|
mut buf := []u8{len: 512} // Initialize the buffer to store message.
|
||||||
@@ -89,12 +101,5 @@ fn main() {
|
|||||||
msg := unsafe { tos_clone(buf.data) }
|
msg := unsafe { tos_clone(buf.data) }
|
||||||
|
|
||||||
eprintln('Received from client: ${read} bytes, data: ${msg}')
|
eprintln('Received from client: ${read} bytes, data: ${msg}')
|
||||||
|
|
||||||
// Send reply to the client.
|
|
||||||
sent := conn.send(msg.bytes(), 0) or {
|
|
||||||
eprintln('SEND: ${err}')
|
|
||||||
exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln('Sent to the client: ${sent} bytes, data: ${msg}')
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user