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

N

naamala

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
 
C

christery

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
 
N

naamala

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
 
P

Peter Duniho

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
 
N

naamala

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
 
P

Peter Duniho

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top