Solved

Port not used with TcpListener

Posted on 2006-11-15
9
819 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
Comment Utility
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
Comment Utility
OK, what is the maximum localhost port number ?
0
 
LVL 9

Author Comment

by:noulouk
Comment Utility
An int  number I suppose.
0
 
LVL 9

Author Comment

by:noulouk
Comment Utility
I google a little and find max: 65535

Could you confirm ?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 15

Assisted Solution

by:ozymandias
ozymandias earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If you don't care which port you actually end up with then yes.
0
 
LVL 13

Expert Comment

by:AngryBinary
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

9 Experts available now in Live!

Get 1:1 Help Now