From 83518198c8cc448fbe7e58c5ef92e971a2477203 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 5 Sep 2022 11:27:30 -0500 Subject: [PATCH] Add Occupy A Local Port With Netcat as a Unix TIL --- README.md | 3 ++- unix/occupy-a-local-port-with-netcat.md | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 unix/occupy-a-local-port-with-netcat.md diff --git a/README.md b/README.md index aae1293..28e5829 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1237 TILs and counting..._ +_1238 TILs and counting..._ --- @@ -1213,6 +1213,7 @@ _1237 TILs and counting..._ - [List The Available JDKs](unix/list-the-available-jdks.md) - [List The Stack Of Remembered Directories](unix/list-the-stack-of-remembered-directories.md) - [Map A Domain To localhost](unix/map-a-domain-to-localhost.md) +- [Occupy A Local Port With Netcat](unix/occupy-a-local-port-with-netcat.md) - [Only Show The Matches](unix/only-show-the-matches.md) - [Open The Current Command In An Editor](unix/open-the-current-command-in-an-editor.md) - [Partial String Matching In Bash Scripts](unix/partial-string-matching-in-bash-scripts.md) diff --git a/unix/occupy-a-local-port-with-netcat.md b/unix/occupy-a-local-port-with-netcat.md new file mode 100644 index 0000000..62d04da --- /dev/null +++ b/unix/occupy-a-local-port-with-netcat.md @@ -0,0 +1,20 @@ +# Occupy A Local Port With Netcat + +The `netcat` (`nc`) utility can listen to a specific port on your local +machine. This, in effect, occupies that port. This is handy for a variety of +reasons. In my case, I find it useful for testing out a web server script that +can gracefully react to its desired port being occupied. + +The `-l` flag is how you tell `nc` to listen. Then you specify `localhost` as +the host and whatever port you want to occupy after that. + +```bash +$ nc -l localhost 5000 +``` + +Now if I try to run my web server script for port `5000`, I see the `Address +already in use` error. + +See `nc -h` for more details. + +[source](https://twitter.com/jbrancha/status/1566820110366654474?s=20&t=HcUMm1aRXpEgCAzONhy34w)