Question

How can I check if a certain TCP/IP port is already taken?

How can I check if a certain TCP/IP port is already taken?

Answer:

During installation of certain applications, such as server side software, you may want to allow the user to select a TCP/IP port. It is common to check whether that port is already in use or not. You can use the <portTest> rule to do just that, as shown in the example below. The application will not allow the end user to continue unless he chooses a free port. Remember that in Unix systems, root privileges are required to bind to ports less than 1024.

<stringParameter>
<name>tomcatport</name>
<title>HTTP Port</title>
<description>Tomcat HTTP Port</description>
<explanation>Please enter the port for your Tomcat server.</explanation>
<value>8080</value>
<default>8080</default>
<allowEmptyValue>1</allowEmptyValue>
<ask>1</ask>
<cliOptionName></cliOptionName>
<width>40</width>
<validationActionList>
<throwError>
  <text>Unable to bind to the given port number. Please select another one.</text>
  <ruleList>
    <portTest>
      <condition>cannot_bind</condition>
      <port>${tomcatport}</port>
    </portTest>
  </ruleList>
</throwError>
</validationActionList>
</stringParameter>