468,170 Members | 2,103 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,170 developers. It's quick & easy.

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 7584
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********@gmail.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*********@nnowslpianmk.com>
wrote:
On Tue, 01 Jan 2008 23:58:54 -0800, naamala <ra********@gmail.comwrote:
But I need to get a randomunusedportnumber.
How can I get thisunusedportnumber 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********@gmail.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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Brad Tilley | last post: by
5 posts views Thread by Keith | last post: by
2 posts views Thread by Droopy | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.