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 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:

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

  • are you sure port 5006 is open?! ; whatis ouput this command sudo netstat -nltp – Baba Jul 1 '16 at 17:16

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:

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 ""

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

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

./gost -L tcp://:1234/
  • 1
    Thanks, I tried the first socat test, but the issue is that my streaming instance expects something there all the time and is listening at 5006 - without this, I get an error. – disruptive Jul 1 '16 at 13:47
  • ncap also has options for proxies... Which allows for some interesting things... (gost looks awesome though....) – Gert van den Berg Nov 28 '18 at 12:25
  • +1 for goproxy! Just amazing... – Luiz Vaz May 6 '20 at 12:47

I had a similar use case. Mine was to listen to an external port and forward it to an internal port which pointed at a reverse ssh tunnel.

External port: 8001

Internal port: 18820

nc -l -k -p 8001 -c "nc 18820"


Simple way to create a tunnel from one local port to another?

How can i keep netcat connection open?


For port forwarding only by net cat you can use pipes:

mkfifo pip
nc -l -p port_to_listen < pip | nc target_ip port_to_be_forwarded > pip


nc target_ip port_to_be_forwarded < pip | nc -l -p port_to_listen > pip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.