473,835 Members | 1,778 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 8102
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.comw rote in message
news:eh******** **@news2.netvis ion.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.Netw orkInformation;
using System.Net;

.....

IPGlobalPropert ies properties =
IPGlobalPropert ies.GetIPGlobal Properties();

IPEndPoint[] tcpEndPoints = properties.GetA ctiveTcpListene rs();

foreach (IPEndPoint p in tcpEndPoints)
{
Console.WriteLi ne("port {0} busy",p.Port.To String());

}

But you would also have to do the same for UDP using the
GetActiveUdpLis teners 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
7311
by: xEM | last post by:
how can i check is port number xxx opened on localhost ? -- pozdr. xEM~
13
3244
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 respective server - via a specific port (2934 or 2935) to see whether the server is still "live", so the hosters don't have to manually update their status on the site. Everything I've seen seems overly complex. Efforts so far have returned info via...
3
5059
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', '\0', '\1', 'Z', '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 DCB and COMMTIMEOUTS structure are declare a __value struct DCB *dcbCommPort = __nogc new DCB();
1
4367
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 device is attached to the COM (Serial) port of my system using VC++/C++ ? prblm: Regardless of any device is connected to serial/COM port, the port can be opened using CFile, if no other programme has locked the port. But i need to find Is any...
0
5544
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 reads the contents that are seperated by spaces, not tabs - will never be seperated by tabs as it is output from another program. The contents of the file are essiantlly 3 fields/values # # Text Field 1 will be a number from 1 to 26, Field 2 will...
3
8282
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 SerialPort ("COM1"); if(serialPort.IsOpen)
4
11293
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 triggered. Thanks in advanced.
0
1384
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 communication to work properly and I have no problem talking to the device. But what I have found, whenever the device is not connected or another device is connected to the port that does not need the Dtrenable set to true, the port get completely...
0
9810
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9653
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10524
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10562
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10236
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9348
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7768
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6968
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.