How to listen for multiple tcp connection using nc
Simultaneous connections are not possible with netcat
. You should use something like ucspi-tcp
's tcpserver
tool or leverage xinetd
since you're on Linux.
See: https://superuser.com/questions/232747/netcat-as-a-multithread-server
Consecutive connections could be handled through a shell script that restarts netcat
after it finishes.
Linux - Receive data via UDP; accept TCP connections, pass the data to the clients
If it is not bi-directional traffic you could use the following:nc -l -u -p 20001 | ncat -k -l -p 30001
Learning from this post: How to listen for multiple tcp connection using nc it is not possible to connect multiple TCP streams to nc
so they advice to use ncat
.
How netcat can listen to the same port on the same host from two different terminals?
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
Read and write to the same netcat tcp connection
The correct way to do this in UNIX is to make use of a back pipe. You can do so as follows:
First, create a pipe: mknod bkpipe p
This creates a file named bkpipe
of type pipe.
Next, figure out what you need to do. Here are two useful scenarios. In these, replace the hosts/addresses and port numbers with the appropriate ports for your relay.
To forward data sent to a local port to a remote port on another machine:
nc -l -p 9999 0<bkpipe | nc remotehost 7000 | tee bkpipe
To connect to another machine and then relay data in that connection to another:
nc leftHost 6000 0<bkpipe | nc rightHost 6000 | tee bkpipe
If you simply need to handle basic IPC within a single host, however, you can do away with netcat completely and just use the FIFO pipe that mknod creates. If you stuff things into the FIFO with one process, they will hang out there until something else reads them out.
Using BusyBox version of netcat for listening tcp port
Here's the manual page for busybox's nc implementation.
The correct syntax is
nc -l -p <port>
The issue is, I think, that your version of busybox is compiled without nc listening capabilities. Indeed there's a config option at build time, NC_SERVER
, that needs to be enabled to turn that feature on.
Can you build another nc
, perhaps from this version, and copy the binary onto your embedded host? You may need to build a cross-compiler environment.
How can I modify the netcat command line to send many requests at once
No need to modify nc
; just pipe your input to it: yes hello | head -n 1000 | nc localhost:8080
.
Netcat uses different port than requested
It looks like you are using traditional netcat
which requires providing -p
argument for the listening port:
netcat -l 127.0.0.1 -p 33333
From nc -h
:
-p port local port number
Syntax you use would work with OpenBSD netcat.
Related Topics
How to Make Perl Wait for Child Processes Started in the Background with System()
How to "Interconnect" Two Sockets in Linux
Best Way to Overwrite File with Itself
How to Sort with Multiple Lines in Bash
How to Set a Ulimit from Inside a Perl Script That Applies to Its Children
Emacs, Linux and International Keyboard Layouts
Mmap: Will the Mapped File Be Loaded into Memory Immediately
Is There Any Significant Difference Between Tcp_Cork and Tcp_Nodelay in This Use-Case
Bash And/Or .Bashrc Not Working Properly After Su or Ssh Login Unless Run "Bash" Command
What Is File Hole and How Can It Be Used
How to Use Aio and Epoll Together in a Single Event Loop
Auto Exit Telnet Command Back to Prompt Without Human Intervention ^] Quit Close Exit Code 1
How to Make Sure the Floating Point Arithmetic Result the Same in Both Linux and Windows
What Is Chained Irq in Linux, When Are They Need to Used
How to Set Up My Linux X Terminal So That Emacs Has Access to 256 Colors
Difference Between -Shared and -Wl,-Shared of the Gcc Options
How to Get Debugging Symbols Working in Linux Perf Tool Inside Docker Containers