Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 26a422d5ed | |||
| 416d45a1de |
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,43 +69,37 @@ fn main() {
|
|||||||
|
|
||||||
println('Listening on ${listen_addr}...')
|
println('Listening on ${listen_addr}...')
|
||||||
|
|
||||||
// Accept the connection from remote. This is a blocking call.
|
for {
|
||||||
// conn will store the new socket connected to the remote.
|
// Accept the connection from remote. This is a blocking call.
|
||||||
conn, remote_addr := socket.accept() or {
|
// conn will store the new socket connected to the remote.
|
||||||
eprintln('ACCEPT: ${err}')
|
conn, remote_addr := socket.accept() or {
|
||||||
exit(1)
|
eprintln('ACCEPT: ${err}')
|
||||||
|
exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close connection on exit.
|
||||||
|
defer {
|
||||||
|
conn.close() or { panic(err) }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get remote host and port in numeric format.
|
||||||
|
remote_host, remote_port := netio.name_info(remote_addr,
|
||||||
|
flags: netio.ni_numerichost | netio.ni_numericserv
|
||||||
|
)!
|
||||||
|
|
||||||
|
eprintln('Accepted connection. Remote address: ${remote_host}, remote port: ${remote_port}')
|
||||||
|
|
||||||
|
// Read 512 bytes of data from socket.
|
||||||
|
mut buf := []u8{len: 512} // Initialize the buffer to store message.
|
||||||
|
// Receive data and write it to the buffer.
|
||||||
|
read := conn.recv(mut buf, 0) or {
|
||||||
|
eprintln('RECV: ${err}')
|
||||||
|
exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a string from buffer without the trailing zeros.
|
||||||
|
msg := unsafe { tos_clone(buf.data) }
|
||||||
|
|
||||||
|
eprintln('Received from client: ${read} bytes, data: ${msg}')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close connection on exit.
|
|
||||||
defer {
|
|
||||||
conn.close() or { panic(err) }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get remote host and port in numeric format.
|
|
||||||
remote_host, remote_port := netio.name_info(remote_addr,
|
|
||||||
flags: netio.ni_numerichost | netio.ni_numericserv
|
|
||||||
)!
|
|
||||||
|
|
||||||
eprintln('Accpeted connection. Remote address: ${remote_host}, remote port: ${remote_port}')
|
|
||||||
|
|
||||||
// Read 512 bytes of data from socket.
|
|
||||||
mut buf := []u8{len: 512} // Initialize the buffer to store message.
|
|
||||||
// Receive data and write it to the buffer.
|
|
||||||
read := conn.recv(mut buf, 0) or {
|
|
||||||
eprintln('RECV: ${err}')
|
|
||||||
exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a string from buffer without the trailing zeros.
|
|
||||||
msg := unsafe { tos_clone(buf.data) }
|
|
||||||
|
|
||||||
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}')
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -39,7 +39,6 @@ pub fn Socket.new(domain AddrFamily, st SocketType, proto Protocol) !Socket {
|
|||||||
|
|
||||||
// type reports the actual socket type.
|
// type reports the actual socket type.
|
||||||
pub fn (s Socket) type() !SocketType {
|
pub fn (s Socket) type() !SocketType {
|
||||||
// return s.get_option_int(sol_socket, so_type)!
|
|
||||||
return s.get_option[SocketType](sol_socket, so_type)!
|
return s.get_option[SocketType](sol_socket, so_type)!
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,8 +112,8 @@ fn (s Socket) get_option_raw(level SocketLevel, option SocketOption, mut value v
|
|||||||
// ```v
|
// ```v
|
||||||
// import netio
|
// import netio
|
||||||
// mut socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
|
// mut socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
|
||||||
// socket.set_option(netio.sol_socket, netio.so_reuseaddr, true)!
|
// socket.set_option(netio.sol_socket, netio.so_reuseaddr, 1)!
|
||||||
// assert socket.get_option[bool](netio.sol_socket, netio.so_reuseaddr)! == true
|
// assert socket.get_option[int](netio.sol_socket, netio.so_reuseaddr)! == true
|
||||||
// ```
|
// ```
|
||||||
pub fn (s Socket) get_option[T](level SocketLevel, option SocketOption) !T {
|
pub fn (s Socket) get_option[T](level SocketLevel, option SocketOption) !T {
|
||||||
mut result := T{}
|
mut result := T{}
|
||||||
|
|||||||
Reference in New Issue
Block a user