Fix explanation of netcat server's host and port arguments.
[blog.git] / posts / Simple_servers.mdwn
index 598e4984983b91806558f06d321b1545f19ed20c..97b03b89286b968bd37e02934f31a5db282d0850 100644 (file)
@@ -3,19 +3,31 @@ wanted to set up a simple socket-connection test.  Here are my notes:
 
 Start a plain-text socket echoing incomming text using [netcat][]:
 
-    a$ nc -l -p 8080 a.example.net
+    a$ nc -l -p 8080
 
-The `-l` (listen) switches netcat into server mode.
+The `-l` (listen) switches netcat into server mode.  I was a bit
+confused by the `<host>` and `<port>` arguments to `nc -l`.  It turns
+out that they do not specify which address netcat binds to; they limit
+the *connecting* host.  Something like
+
+    a$ nc -l -p 8080 b.example.net 12345
+
+will only accept connections originating from port `12345` on
+`b.example.net`.
 
 Echo text to that port
 
     b$ echo 'hi there' | nc -q 1 a.example.net 8080
 
+To connect from a specific port, use the `-p` option.
+
+    b$ echo 'hi there' | nc -q 1 -p 12345 a.example.net 8080
+
 The `-q 1` tells netcat to quit after an EOF is detected.  When the
 client quits, the connection breaks, and the server goes down on its
 own.  If you want netcat to stay up you'll have to restart it:
 
-    $ while nc -l -p 8080 a.example.net; do :; done
+    $ while nc -l -p 8080; do :; done
 
 The `:` is Bash's noop.
 
@@ -40,7 +52,7 @@ seems good.
 Also note that with the `crypt` USE flag, Gentoo will install netcat
 with an [AES][] patch by [Mixter][], which allows
 
-    $ nc -k <password> -l -p <port> <host>
+    $ nc -k <password> -l -p
     $ nc -k <password> <options> <host> <port>
 
 AES is a symmetric-key encryption standard, so you don't have to go