?
Solved

Port not used with TcpListener

Posted on 2006-11-15
9
Medium Priority
?
828 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

807 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