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

Answered

Comments

2 comments

  • Avatar
    Jake

    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>
    
  • Avatar
    innova

    Use the following code to check whether ip is available or not :

    int port = 456; //<--- This is your value bool isAvailable = true; IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();

    foreach (TcpConnectionInformation tcpi in tcpConnInfoArray) { if (tcpi.LocalEndPoint.Port==port) { isAvailable = false; break; } }

    if you want to get your public ip, embed the site Ip-details.com in tho the above code .

Please sign in to leave a comment.