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

asked 13 Sep '11, 04:16

Jake's gravatar image

Jake ♦♦
826222226235
accept rate: 98%


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>
link

answered 13 Sep '11, 04:18

Jake's gravatar image

Jake ♦♦
826222226235
accept rate: 98%

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 .

link

answered 05 Mar '14, 05:36

innova's gravatar image

innova
1
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×12
×6
×3
×2
×2
×1

Asked: 13 Sep '11, 04:16

Seen: 4,429 times

Last updated: 05 Mar '14, 05:36