46

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

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

3 Answers 3

77

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
4
  • 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, 2016 at 13:47
  • ncap also has options for proxies... Which allows for some interesting things... (gost looks awesome though....) Nov 28, 2018 at 12:25
  • +1 for goproxy! Just amazing...
    – Luiz Vaz
    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
    Dec 3, 2021 at 10:57
2

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

REF:

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

How can i keep netcat connection open?

0
0

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

Or:

nc target_ip port_to_be_forwarded < pip | nc -l -p port_to_listen > pip
1
  • Are each of these pipes uni-directional and would you need both to have a duplex channel?
    – oligofren
    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 .