Linux – Best way to monitor for a server on a TCP port

linuxtcp

I have a remote Music Player Daemon(MPD) server running on a linux machine. I have a client listening to this stream on another linux machine.

When the MPD server is asked to pause or stop the stream, it disconnects all clients connected on the TCP port. Consequently, when the server starts streaming again, the clients have to be manually reconnected.

I want to write a program that will monitor the TCP port for a server accepting connections, and then automatically restart the clients. Can I do better than running connect() and sleep() in a loop? Are there any command-line utilities to do this?

I can run the client on the machine running the MPD server, if it will help. The following will tell me if a process is listening on a local port, but they do not block if a process isn't, so I still need to wrap them in a loop.

$ sudo fuser -n tcp 8000
8000/tcp: 9677

$ sudo netstat -nlp | grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 9677/mpd

I can try any solution that does not involve changing the behaviour of the MPD server.

Best Answer

There is always the possibility of writing a relay server that proxies for MPD.

It sits there listening on a different port for your clients and makes connections to MPD in their stead. When MPD disconnects, the relay just attempts to reconnect every few seconds without disconnecting its clients.

Related Topic