473,803 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i get unused/available port in the local system using C# ?

Hi,

How can i get unused/available port in the local system using C# ?
Or
Is there any way to find out whether the particular port number is
being used by any other application using C#?

Thanks
naamala
Dec 28 '07 #1
5 8491
How can i get unused/available port in the local system using C# ?

Thinking ip socket, you should *havent tried this in c#* know remote
port/ip and that should suffice,

if you are writing a server, anything under 1024 is taken for some
thing but antything over is for grabs...

as long as noone else already got it and then there is a problem,

they got your ip but not talking to the right program ;)

so find out what is never used under 1024 and grab that

or take a high number and start your server at boot and both solutions
should work...

but be aware of the firewalls

//CY
Dec 28 '07 #2
Hi,

Thank you for your reply,

But I need to get a random unused port number.
How can I get this unused port number Or How can I check whether the
port number is under usage or not?

thanks
naamala
On Dec 28 2007, 10:56 pm, christ...@gmail .com wrote:
How can i getunused/availableportin the local system using C# ?

Thinking ip socket, you should *havent tried this in c#* know remoteport/ip and that should suffice,

if you are writing a server, anything under 1024 is taken for some
thing but antything over is for grabs...

as long as noone else already got it and then there is a problem,

they got your ip but not talking to the right program ;)

so find out what is never used under 1024 and grab that

or take a high number and start your server at boot and both solutions
should work...

but be aware of the firewalls

//CY
Jan 2 '08 #3
On Tue, 01 Jan 2008 23:58:54 -0800, naamala <ra********@gma il.comwrote:
But I need to get a random unused port number.
How can I get this unused port number Or How can I check whether the
port number is under usage or not?
It would be better if you could explain what the higher-level goal is.

That said, if you don't care what the port is, you should simply not
specify it. Or rather, specify 0 for the port. Winsock (on which the
..NET Socket class is built) will simply pick a port for you in that case.
In most cases, you don't even need to bind the socket explicitly. Just
create your socket and use it (connect for TCP, any send for UDP) and it
will automatically be bound, to an available port.

Pete
Jan 2 '08 #4
Hi,

Thank for the early reply.

I need to start a server using WCF service using netTcpbinding with IP
address & unused port number on the local machine.
After successfully starting the server I will public my IP address &
portnumber to a messaging mechanism
which will be picked by the clients. Now clients will create a proxy
class for that WCF service using the IP address & port number and
can do the operation.

thanks
ramesh
On Jan 2, 2:35 pm, "Peter Duniho" <Np*********@nn owslpianmk.com>
wrote:
On Tue, 01 Jan 2008 23:58:54 -0800, naamala <ra********@gma il.comwrote:
But I need to get a randomunusedpor tnumber.
How can I get thisunusedportn umber Or How can I check whether the
portnumber is under usage or not?

It would be better if you could explain what the higher-level goal is.

That said, if you don't care what theportis, you should simply not
specify it. Or rather, specify 0 for theport. Winsock (on which the
.NET Socket class is built) will simply pick aportfor you in that case.
In most cases, you don't even need to bind the socket explicitly. Just
create your socket and use it (connect for TCP, any send for UDP) and it
will automatically be bound, to an availableport.

Pete
Jan 2 '08 #5
On Wed, 02 Jan 2008 04:25:47 -0800, naamala <ra********@gma il.comwrote:
I need to start a server using WCF service using netTcpbinding with IP
address & unused port number on the local machine.
After successfully starting the server I will public my IP address &
portnumber to a messaging mechanism
How will you "public" your IP address? The "messaging mechanism" should
be simply looking at your endpoint address as _it_ sees it, rather than
relying on any information from you.

Assuming that's the case, simply specifying 0 as the port # will work
fine. The "messaging mechanism" will, on receiving any communications
from your server, be able to identify your actual address and use it
correctly. Assuming it's well-designed, that is.

Pete
Jan 2 '08 #6

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

Similar topics

18
9877
by: Brad Tilley | last post by:
Instead of me arbitrarily assigning a high port number to a variable, is it possible to check for ports that are unused and then randomly assign one of them to a variable? Something like this is what I'm thinking: port = socket.getunusedport() Any ideas?
5
4363
by: Keith | last post by:
I am trying to write a simple FTP client, but I am having trouble with the PORT Command. Does anyone know how to Get an IP Address and port number to establish a data port for the server to connect to. Any info would be appreciated.
9
43177
by: mBird | last post by:
I wrote a service that listens for broadcast messages from my firewall (UDP snmp trap messages) and parses and puts the data in an database. I'd also like to write an app that is a simple form that can listen in when it runs (so I can see messages in a form as they occur.) So I need the ability to listen to a UDP port with two apps at the same time (the service and my app). But when I do that I get an error:...
0
3035
by: Gregory Hassett | last post by:
Hello, I want to periodically send a TCP packet to a peer, always from the same source port. That is, each packet will come from my local ip address, port 8801, and go to the peer's ip address, to HIS port 8801. This means that I need to bind my sender socket to (myaddress,8801), use it for the send, then shut it down and close it. Then I want to wait, say 30 seconds, and do it all over again. Naturally, the socket's ReuseAddress...
2
702
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no more data to expect). Sometimes, Socket.Available returns 0 but the other end of the connection did not close it ! 2) This class will be used by many other classes so I have to use the
4
11208
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. What I would like to do is make the serial port accessible via a web service. The web service and the legacy application would be running on the same machine. The mobile application would access the web service via a network connection. It...
12
9629
by: zacks | last post by:
Suddenly, in a VB2005 project I am working on, several variables show up in the list of warnings are being unused local variables. But they all are. Several of them are the ex variable used in a Try/Catch, and the Catch for each one references the ex exception variable. Yet it is still flagged as unused? What gives?
5
9505
by: LongBow | last post by:
Hello, Is there a way, in .NET, to determine what are the avialable Serial (Communications) Ports on a Windows OS and is there a way to determine that port isn't being use other than attempting to opening the Serial Port and catching the associated exception? Thanks If there isn't a way to determine the Serial Port within .NET I would be willing, I guess, to use a WinAPI is that was the only way.
4
5808
by: Daniel | last post by:
is there some per-process-limit on memory in .net processes? is there any way to increase it? i keep getting System.OutOfMemoryException when my box has 8 gigs of unused memory.
0
9703
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
10555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10069
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
9127
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
7607
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
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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
3
2974
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.