title: Best way to monitor for a server on a TCP port
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>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 <strong>connect()</strong> and <strong>sleep()</strong> in a loop? Are there any command-line utilities to do this? </p>
  
  <p>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.</p>
  
  <blockquote>
    <p>$ sudo fuser -n tcp 8000 <br>
    8000/tcp:    9677</p>
    
    <p>$ sudo netstat -nlp  | grep 8000 <br>
    tcp  0      0 0.0.0.0:8000           0.0.0.0:*               LISTEN      9677/mpd</p>
  </blockquote>
  
  <p>I can try any solution that does not involve changing the behaviour of the MPD server.</p>