IP and Port

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,
I am writing a program using the TcpListener class. How can I get a
random unused port and the ip of my local host machine.


Thanks in Advance
 
to get a random unused port, try to use the Random Class to generate a number
up to 2500 or 3000


Tom.
 
Hi,

Just pass a 0 as the port number in the constructor.

Cheers,
 
Hi,
First of all thanks for all the help already given. Please can
someone tell what method should I use to get the ip of my machine.

Also, having an ip stored in a string, how can I convert it to the
object IPAddress.



Thanks in Advance
 
Hi,
Sorry for asking too many questions. I am doing the as follows.
Now how can I get the port number that was assigned.

TcpListener tcp = new TcpListener(0); // need to get random
port


Thanks for your patience
 
Xarky wrote:

[...snip...]
Please can
someone tell what method should I use to get the ip of my machine.

I got it somewhat like

IPAddress[] addressArray = Dns.Resolve(Dns.GetHostName()).AddressList;
IPAddress myLocalAddress = addressArray[0];

which requires access to a DNS-Server, I think.
 

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

Back
Top