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
    Commented Jul 1, 2016 at 17:16

3 Answers 3


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
    Commented Jul 1, 2016 at 13:47
  • ncap also has options for proxies... Which allows for some interesting things... (gost looks awesome though....) Commented Nov 28, 2018 at 12:25
  • +1 for goproxy! Just amazing...
    – Luiz Vaz
    Commented May 6, 2020 at 12:47
  • The ncat command consistently fails after the first http connection. So typically getting the index.html, but failing on the later requests.
    – oligofren
    Commented Dec 3, 2021 at 10:57
  • 1
    @oligofren netcat works with single requests. To make it work continuously, it has to be in a loop, e.g. as in this gist, or use the -k flag.
    – Walf
    Commented Apr 9 at 2:32

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?

  • 1
    ubuntu needs package ncat for the -c, the nc package doesn't have it. Commented Mar 13 at 18:01

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
  • Are each of these pipes uni-directional and would you need both to have a duplex channel?
    – oligofren
    Commented Dec 3, 2021 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .