mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
examples: add client ip logging to examples/tcp_echo_server.v
This commit is contained in:
parent
bae9ed0350
commit
73771b741c
1 changed files with 7 additions and 3 deletions
|
@ -1,13 +1,17 @@
|
||||||
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) {
|
||||||
eprintln('new client connected')
|
peer_ip := con.peer_ip() or {
|
||||||
|
'0.0.0.0'
|
||||||
|
}
|
||||||
|
eprintln('${peer_ip:16} | new client connected')
|
||||||
defer {
|
defer {
|
||||||
eprintln('closing connection: $con')
|
eprintln('${peer_ip:16} | closing connection...')
|
||||||
con.close() or { }
|
con.close() or { }
|
||||||
}
|
}
|
||||||
con.send_string("Welcome to V's TCP Echo server.\n") or {
|
con.send_string("Welcome to V's TCP Echo server.\n") or {
|
||||||
|
@ -18,7 +22,7 @@ fn handle_connection(con net.Socket) {
|
||||||
if line.len == 0 {
|
if line.len == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
eprintln('received line: ' + line.trim_space())
|
eprintln('${peer_ip:16} | received line: ' + line.trim_space())
|
||||||
con.send_string(line) or {
|
con.send_string(line) or {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue