Solved

Port not used with TcpListener

Posted on 2006-11-15
9
822 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Author Comment

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

Could you confirm ?
0
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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