473,499 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check if a port is opened

Hi everybody!

I'm want to check if a certain port is opened or closed.
now I'm using try-catch to connect to the port. if it fails, it meas that
the port is closed.

The problem is that this method is extremely inefficient! anyone has a
better idea on how to do this kind of verification?

Thanks!
Oct 20 '06 #1
2 7993
Hi,

There is no better way if after you check whether the port is free you
immediately try to use it.

The two statements (check and use) won't execute as an atomic operation.
Therefore, it's possible that you'll check for the port being in use and it
will be "false", and then you'll try to acquire its use and an exception will
be thrown because another process slipped in before you.

So if you have to code for the exception handling anyway to account for the
lack of synchronicity, there is no reason to check the port's status in the
first place.
Why is try...catch inefficient for you?

--
Dave Sexton

"NoOne" <No**@none.comwrote in message
news:eh**********@news2.netvision.net.il...
Hi everybody!

I'm want to check if a certain port is opened or closed.
now I'm using try-catch to connect to the port. if it fails, it meas that
the port is closed.

The problem is that this method is extremely inefficient! anyone has a
better idea on how to do this kind of verification?

Thanks!

Oct 20 '06 #2
You could try something like this:

using System.Net.NetworkInformation;
using System.Net;

.....

IPGlobalProperties properties =
IPGlobalProperties.GetIPGlobalProperties();

IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners();

foreach (IPEndPoint p in tcpEndPoints)
{
Console.WriteLine("port {0} busy",p.Port.ToString());

}

But you would also have to do the same for UDP using the
GetActiveUdpListeners as well. It would give you a decent indication of
what is busy.

Kelly S. Elias
Webmaster
DevDistrict - C# Code Library
http://devdistrict.com

NoOne wrote:
Hi everybody!

I'm want to check if a certain port is opened or closed.
now I'm using try-catch to connect to the port. if it fails, it meas that
the port is closed.

The problem is that this method is extremely inefficient! anyone has a
better idea on how to do this kind of verification?

Thanks!
Oct 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7279
by: xEM | last post by:
how can i check is port number xxx opened on localhost ? -- pozdr. xEM~
13
3212
by: Adam | last post by:
Hi! I'm trying to write a small application for an online gaming site (flight sims), where people can add their local server to a list. Basically, I just need to be able to loop/ping each...
3
5017
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0',...
1
339
by: CeZaR | last post by:
Hi, I've posted earlier a question regarding the call to GetCommState. Here is the code for the function. The problem is that GetCommState always returns false!! Why? void Open() { // the...
1
4324
by: sujith[arsse] | last post by:
Hi all, I'm new to this community,so i think i shall introduce myself first, I'm a developer in VC++ 6.0 for past few years, Now my mission to post this thread is how can i find "Is any...
0
5507
by: 14Dallas | last post by:
Hi, I have been working with another programmer to write this code. I haven't written code in years - DBase III and Pascal - anyways, back to my code question. The program opens a file and...
3
8193
by: ricolee99 | last post by:
Hi Everyone, I'm using the System.IO.Ports.SerialPort component to attempt to kill an existing component opened by another application. I use the following code: SerialPort serialPort = new...
4
11266
by: Sir C4 | last post by:
Can someone give a quick example of how to check for activity on a TCP port? For example, I'd like to watch and listen for activity on port 80. Once activity is detected, I'd like an event to be...
0
1370
by: | last post by:
I have written a few device classes that use the Serialport class with no problems what so ever. I finally ran into a device that need to have the Dtrenable flag set to true in order to...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.