Best perl method to determine if a TCP port exists

What is the best method using perl to determine if tcp port is accessible. I want to select a port before creating a network service. All this needs to be done through automation, and you need to create a service configuration, including the available tcp port in the range used by the service.

Possible methods:

  • IO :: Socket to verify binding on the candidate port.
  • Net :: Telnet to verify connection to the candidate port
  • Linux :: Proc :: Net :: TCP

What is the best method and why? I don't really like test binding because it requires root for privileged ports, and if something goes wrong with the script, it can leave all the tested ports in time_wait. I like Linux :: Proc :: Net :: TCP because it is fast but not portable.

+4
source share
1 answer

bindwill immediately and specifically inform you if the port is unavailable ( EADDRINUSE). Other options make no sense.

Since it seems that you really don't care about which port you are bound to, it seems to me that a better approach would be to binda port 0that binds to an available port. You can determine the port that your socket was connected to using sockaddr_in.

If, on the other hand, the desired port is within 1.1023, then you just need to reserve a port for your program. The system will not automatically assign a service to these ports. (I'm not sure if this is true on Windows.)

+4
source

Source: https://habr.com/ru/post/1671485/


All Articles