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




  • Avatar

    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.

    <title>HTTP Port</title>
    <description>Tomcat HTTP Port</description>
    <explanation>Please enter the port for your Tomcat server.</explanation>
      <text>Unable to bind to the given port number. Please select another one.</text>
    Comment actions Permalink
  • Avatar

    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 in tho the above code .

    Comment actions Permalink

Please sign in to leave a comment.