examples/tcp_echo_server: cleanup, use defer{}
parent
a9724fd38d
commit
fa02130359
|
@ -1,23 +1,29 @@
|
||||||
import net
|
import net
|
||||||
|
|
||||||
// This file shows how a basic TCP echo server can be implemented using
|
// 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
|
// the `net` module. You can connect to the server by using netcat
|
||||||
// or telnet, in separate shells, for example:
|
// or telnet, in separate shells, for example:
|
||||||
// `nc 127.0.0.1 12345`
|
// `nc 127.0.0.1 12345`
|
||||||
// `telnet 127.0.0.1 12345`
|
// `telnet 127.0.0.1 12345`
|
||||||
|
|
||||||
fn handle_connection(con net.Socket) {
|
fn handle_connection(con net.Socket) {
|
||||||
if _ := con.send_string("Welcome to V's TCP Echo server.\n") {
|
eprintln('new client connected')
|
||||||
|
defer {
|
||||||
|
eprintln('closing connection: $con')
|
||||||
|
con.close() or { }
|
||||||
|
}
|
||||||
|
con.send_string("Welcome to V's TCP Echo server.\n") or {
|
||||||
|
return
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
line := con.read_line()
|
line := con.read_line()
|
||||||
if line.len == 0 { break }
|
if line.len == 0 {
|
||||||
eprintln('received line: ' + line.trim_space())
|
|
||||||
con.send_string(line) or { break }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
con.close() or {}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
eprintln('received line: ' + line.trim_space())
|
||||||
|
con.send_string(line) or {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
server_port := 12345
|
server_port := 12345
|
||||||
|
|
Loading…
Reference in New Issue