Outaspace has asked for the wisdom of the Perl Monks concerning the following question:

Hail great All-Knowing, I have a two little scripts (client/server) that communicate via a port at localhost (both on same machine). But now I wonder what will happen if I start another pair of the scripts. Is there a way to check if the ports are in use (it would be fatal, if I mix them), or do I yust have to try to open the ports and when it fails I know that they are used? Kind regards Andre

Replies are listed 'Best First'.
Re: Checking if a port is allready used
by tirwhan (Abbot) on Jan 10, 2006 at 12:08 UTC

    The netstat command will show you the status of your network connections

    netstat -nat

    Will print out a list of all open TCP connections with ports (on Linux, check your local manpage if you're running a different *NIX, netstat tends to have subtly different syntax on the different variants). Since you're only communicating on the local host you'll likely only be interested in communications on 127.0.0.1

    netstat -nlt

    will print out a list of sockets your machine is listening on (and which are therefore blocked). For Linux you can also add "-p" to give you the processes listening on these sockets (if you're root).

    BTW, if you're expecting to want to reuse the same port for a different process a lot, you should set SO_REUSEADDR when opening the socket for listening.

    Update: Check out Net::Netstat::Wrapper for a Perl interface to netstat


    There are ten types of people: those that understand binary and those that don't.
Re: Checking if a port is allready used
by jbrugger (Parson) on Jan 10, 2006 at 12:08 UTC
    Hi Outaspace,
    If you have a server, that has to be able to handle more connections simultaniously and asynchrone, i'd use something like an md5-hash to identify the client-server connecton (like a session-key).
    Anyway, your server-program has to decide if it's accepting more than one connection on a given port, since only one program at a time can listen on a given port.
    But since you don't provide mutch information about your design, used modules etc. i don't think a lot people can help you now.
    Perhaps you can have a look at cpan? Net::Server, IO::Socket, IO::Socket, Sockets: Client/Server Communication etc.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Checking if a port is allready used
by Limbic~Region (Chancellor) on Jan 10, 2006 at 13:38 UTC

      ... which simply attempts to open the port and return true or false; but wraps that up in a complicated interface.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.