How to perform action upon connection to netcat listener?

The subject pretty much says it all. I’m searching for a method to perform some actions on my linux machine upon connection to a netcat listener, such as sending an e-mail/sms notification.

The best I’ve been able to come up with would be setup some pipes as the input/output to the listener, and have a shell script running which monitors those pipes, but I was hoping the linux guru’s here might have something a bit more elegant.

Piping is your best bet but I don’t what you would want to pipe

nc -l -u 2222 | tee vidrx.mp4 | mplayer -fps 70 -vf-add flip -demuxer
h264es -

Here is a command I use at work to stream a video signal from a rpi robot. Hope this helps.

As @lantern-shadowclaw pointed out, you can use pipes, otherwise, if netcat is not a strict requirement, you can use socket units with systemd, listening on a port of choice and activating a service for each connection (or event).

Here’s the recipe: https://www.freedesktop.org/software/systemd/man/systemd.socket.html