From 784d27f41b46791b597a191e539778cd37623947 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 28 Mar 2020 11:21:22 +0200 Subject: [PATCH] examples: add a simple self contained tcp echo server --- examples/tcp_echo_server.v | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/tcp_echo_server.v diff --git a/examples/tcp_echo_server.v b/examples/tcp_echo_server.v new file mode 100644 index 0000000000..c68c7b359d --- /dev/null +++ b/examples/tcp_echo_server.v @@ -0,0 +1,35 @@ +import net + +// This file shows how a basic TCP echo server can be implemented using +// the `net` module. You can connect to the server by using netcat +// or telnet, in separate shells, for example: +// `nc 127.0.0.1 12345` +// `telnet 127.0.0.1 12345` + +fn handle_connection(con net.Socket) { + if _ := con.send_string("Welcome to V's TCP Echo server.\n") { + for { + line := con.read_line() + if line.len == 0 { break } + eprintln('received line: ' + line.trim_space()) + con.send_string(line) or { break } + } + } + con.close() or {} + return +} + +fn main() { + server_port := 12345 + eprintln('Starting an echo server, listening on port: $server_port') + server := net.listen(server_port) or { + panic(err) + } + for { + con := server.accept() or { + server.close() or {} + panic(err) + } + go handle_connection(con) + } +}