-
-
Save phuesler/7501710 to your computer and use it in GitHub Desktop.
#!/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 |
You have to listen on 0.0.0.0. Depending on your netcat version you can do -l 0.0.0.0
. Then your computer needs to be reachable from the internet either through a public ip address or NAT settings on your firewall/router. If it is from within the LAN, then this work if your local firewall allows it.
On my mac this works:
nc -l 0.0.0.0 5555
emm how?😅
What are you trying to achieve?
I tried to create tcp server, the command is nc -lvp 9999
and this is the output
Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::9999
Ncat: Listening on 0.0.0.0:9999
and only in local can connect to that server, and I want public can connect too but how?
- Is the server accessible by another computer within the same network?
emm... I dont know
What is the server supposed to do? Deliver a website?
I want the server accept connection from outside(global), not only from inside(localhost)
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?
I don't understand the question. What is your web? And what is the TCP server supposed to do?
ugh sorry, my question ends here. Thanks for your response
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
).
If that isn't the case, you can use a service like ngrok to make that port publicly available.
On OSX this is while :; do nc -l localhost $PORT | tee output.log; sleep 1; done
(or use brew for conventional unix syntax)
how to create tcp server like this and public can connect to spesific address?