aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/examples/net_udp_server_and_client.v
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/old/examples/net_udp_server_and_client.v
downloadcli-tools-windows-master.tar.gz
cli-tools-windows-master.tar.bz2
cli-tools-windows-master.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/old/examples/net_udp_server_and_client.v')
-rw-r--r--v_windows/v/old/examples/net_udp_server_and_client.v44
1 files changed, 44 insertions, 0 deletions
diff --git a/v_windows/v/old/examples/net_udp_server_and_client.v b/v_windows/v/old/examples/net_udp_server_and_client.v
new file mode 100644
index 0000000..a69d178
--- /dev/null
+++ b/v_windows/v/old/examples/net_udp_server_and_client.v
@@ -0,0 +1,44 @@
+import os
+import os.cmdline
+import net
+
+fn main() {
+ println('Usage: net_udp_server_and_client [-l] [-p 5000]')
+ println(' -l - act as a server and listen')
+ println(' -p XXXX - custom port number')
+ println('------------------------------------------')
+ is_server := '-l' in os.args
+ port := cmdline.option(os.args, '-p', '40001').int()
+ mut buf := []byte{len: 100}
+ if is_server {
+ println('UDP echo server, listening for udp packets on port: $port')
+ mut c := net.listen_udp(':$port') ?
+ for {
+ read, addr := c.read(mut buf) or { continue }
+ println('received $read bytes from $addr')
+ c.write_to(addr, buf[..read]) or {
+ println('Server: connection dropped')
+ continue
+ }
+ }
+ } else {
+ println('UDP client, sending packets to port: ${port}.\nType `exit` to exit.')
+ mut c := net.dial_udp('localhost:$port') ?
+ for {
+ mut line := os.input('client > ')
+ match line {
+ '' {
+ line = '\n'
+ }
+ 'exit' {
+ println('goodbye.')
+ exit(0)
+ }
+ else {}
+ }
+ c.write_string(line) ?
+ read, _ := c.read(mut buf) ?
+ println('server : ' + buf[0..read].bytestr())
+ }
+ }
+}