473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 8418
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
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...
5
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...
9
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...
0
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,...
2
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...
4
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. ...
12
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...
5
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...
4
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...
0
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...

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.