Linux – Using netcat for port forwarding

.nclinuxnetcatnetworking

I have a process that listens on an IP:port – in fact it is spark streaming which connects to a socket. The issue is that I wish to somehow create a server that connects to spark on one port and data is streamed into this server from another port.

For example, the spark streaming example uses the netcat utility (for example nc -lk 5005). However, I have another service that listens for incoming messages and then spit out a message. So I need some kind of server that can listen to messages from service A and pass them to spark.

My service A, relies on sockets. And my spark consumer relies on sockets.

Here is what I have done so far is the forwarding from port to port but this does not seem to work:

nc -X 4 -x 127.0.0.1:5005 localhost 5006

With the idea that the service A:5005 -> socket -> 5006 -> Spark

I cannot seem to find the correct way to make this work.

Some answers have suggested the following:

socat tcp-l:5005,fork,reuseaddr tcp:127.0.0.1:5006

My spark socket reciever doesn't or cannot seem to connect. I get the error: Error connecting to 127.0.0.1:5006 - java.net.ConnectException: Connection refused

Best Answer

you can't use only nc for forward traffic, nc have not keep-alive or fork mode

you must use another tools instead nc; for example use socat or ncat


this command listen on port 5050 and forward all to port 2020

socat tcp-l:5050,fork,reuseaddr tcp:127.0.0.1:2020

Ncat is a feature-packed networking utility which reads and writes data across networks from the command line. Ncat was written for the Nmap Project as a much-improved reimplementation of the venerable Netcat. It

ncat -l localhost 8080 --sh-exec "ncat example.org 80"

And you can use another tools:

Listen on port 1234 and forward it to port 4567 on address "1.1.1.1"

./proxy tcp -p ":1234" -T tcp -P "1.1.1.1:4567"

Listen on port 1234 and forward it to port 4567 on address "1.1.1.1" source

./gost -L tcp://:1234/1.1.1.1:4567
Related Question