Skip to content

Instantly share code, notes, and snippets.

@phuesler
Last active October 7, 2024 14:15
Show Gist options
  • Save phuesler/7501710 to your computer and use it in GitHub Desktop.
Save phuesler/7501710 to your computer and use it in GitHub Desktop.
Simple tcp server using netcat
#!/bin/bash
# Simple tcp server using netcat
# - depending on the netcat version either use nc -l 5555 or nc -l -p 5555
# - verify with `telnet locahhost 5555`
# - quit the telnet with `ctrl-]` and then type quit
# - the while loop is there so reopen the port after a client has disconnected
# - supports only one client at a time
PORT=5555;
while :; do nc -l -p $PORT | tee output.log; sleep 1; done
@r3duc3
Copy link

r3duc3 commented Jan 21, 2021

I want the server accept connection from outside(global), not only from inside(localhost)

@phuesler
Copy link
Author

phuesler commented Jan 21, 2021

Do you have access to your internet router? Am I assuming correctly you are running this on your computer at home and not on a server?

@phuesler
Copy link
Author

I don't understand the question. What is your web? And what is the TCP server supposed to do?

@r3duc3
Copy link

r3duc3 commented Jan 23, 2021

ugh sorry, my question ends here. Thanks for your response

@Tuanm
Copy link

Tuanm commented Dec 5, 2022

I want the server accept connection from outside(global), not only from inside(localhost)

I guess you've already found out the solution.

For guys who come here later, you have to run this script on a public machine (having a public IP address) to make it accessible globally. Of course, the port on which nc is listening must be open as well (using ufw).

@phuesler
Copy link
Author

phuesler commented Dec 6, 2022

If that isn't the case, you can use a service like ngrok to make that port publicly available.

@richdrich
Copy link

On OSX this is while :; do nc -l localhost $PORT | tee output.log; sleep 1; done (or use brew for conventional unix syntax)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment