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

asked 13 Sep '11, 04:16

Jake's gravatar image

Jake ♦♦
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.

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

answered 13 Sep '11, 04:18

Jake's gravatar image

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


answered 05 Mar '14, 05:36

innova's gravatar image

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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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



Asked: 13 Sep '11, 04:16

Seen: 4,326 times

Last updated: 05 Mar '14, 05:36