Solved

Port not used with TcpListener

Posted on 2006-11-15
9
820 Views
Last Modified: 2008-01-09
Hi Experts,

I need to  find a port that is not in use to connect my TcpListener.

Thanks in advance for your help.
0
Comment
Question by:noulouk
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17951138
Pick a port above 8000 and try to use if.
If its in use you should get an error so try another one.
0
 
LVL 9

Author Comment

by:noulouk
ID: 17951338
OK, what is the maximum localhost port number ?
0
 
LVL 9

Author Comment

by:noulouk
ID: 17951342
An int  number I suppose.
0
 
LVL 9

Author Comment

by:noulouk
ID: 17951358
I google a little and find max: 65535

Could you confirm ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Assisted Solution

by:ozymandias
ozymandias earned 250 total points
ID: 17951420
That sounds right.
It is the max value of an unsigned 16 bit int.
0
 
LVL 13

Accepted Solution

by:
AngryBinary earned 250 total points
ID: 17951515
You should not use any port below 49152 without IANA registration, since all ports below 49152 are considered reserved. Generally, people do it anyway. Ports below 1024 are definitely a no-no, and their use carries a significant chance of port conflict.

Best practices dictate that for a definitely safe port, pick one from 49152-65535 (which is indeed the max, btw), or you can register a port with the IANA [www.iana.org] if you plan to distribute the application for general use.

The application to register can be found at http://www.iana.org/protocols/forms.htm.

Cheers,
--Randall
0
 
LVL 9

Author Comment

by:noulouk
ID: 17954016
In msdn doc, they tell I can use new TcpListener(IPAddress.Any, 0) and a port is automatically assigned : is it the best solution ?

" The TcpListener class provides simple methods that listen for and accept incoming connection requests in blocking synchronous mode. You can use either a TcpClient or a Socket to connect with a TcpListener. Create a TcpListener using an IPEndPoint, a Local IP address and port number, or just a port number. Specify Any for the local IP address and 0 for the local port number if you want the underlying service provider to assign those values for you. If you choose to do this, you can use the LocalEndpoint property to identify the assigned information, after the socket has connected.  "
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17954052
If you don't care which port you actually end up with then yes.
0
 
LVL 13

Expert Comment

by:AngryBinary
ID: 17961264
That will get you a random port. That is fine for outgoing connections, ie ones that you will instantiate, but if you are setting up a listener, you will either want to specify a particular port so that you can specify in the client application which port to connect to, or need to set up an additional service to make the port number available to the client application some other way.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now