The -p
option specifies a source port, not a listening port.
The -l
option puts netcat into listening mode.
In your example, 1234
is the input value for the -p
option, not the -l
option, which means there is no explicit listening port being specified. If netcat is not failing, then most likely netcat is binding to port 0 instead, which tells the listening socket to bind to a random available ephemeral port. As such, your two netcat instances would actually be listening on different ports. Use netstat to verify.
According to the Linux manpage for netcat:
-l' Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host. It is an error to use this option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored.
-p source_port
Specifies the source port nc should use, subject to privilege restrictions and availability. It is an error to use this option in conjunction with the -l option.
So technically, your example may not be valid to begin with.
However, on some systems, including some Debian installs, depending on which flavor of netcat you use (in particular, the traditional flavor), you actually may need to use -l
and -p
together, but you need to swap their order to specify a listening port correctly, eg:
nc -l -p 1234