Click to See Complete Forum and Search --> : How to obtain new PORT
vikaskumartiwari
December 27th, 2004, 03:12 AM
while writting , socket Program , How can I assure that the port I have selected
, does'nt get confilicted with with other application ,
how to obtain a unique PORT number
OReubens
December 27th, 2004, 08:55 AM
Pick one. Preferably somewhere over 2000 is reasonably 'safe' and likely not being used by something else.
However, you should always have a config option allowing users to select a different port because there are not guarantees someone else isn't using that port already.
There is no way to register your port for exclusive use. There are only 64K different ports, which would mean there could ever only be 64K programs in the entire world using sockets.
If you get lucky enough to get a program that gets used worldwide, you could end up with a port nobody else is likely going to use, because YourProgram uses that one ;-)
Ejaz
December 27th, 2004, 09:10 AM
Furthmore to what already suggested...
From MSDN
For TCP/IP, if the port is specified as zero, the service provider assigns a unique port to the application with a value between 1024 and 5000. The application can use getsockname after calling bind to learn the address and the port that has been assigned to it. If the Internet address is equal to INADDR_ANY, getsockname cannot necessarily supply the address until the socket is connected, since several addresses can be valid if the host is multihomed. Binding to a specific port number other than port 0 is discouraged for client applications, since there is a danger of conflicting with another socket already using that port number.
Also, from http://tangentsoft.net/wskfaq/articles/lame-list.html,
Client applications that bind to a specific port. Suffocating in self lameness.
Reason: By definition, client applications actively initiate a network communication, unlike server applications which passively wait for communication. A server must bind() to a specific port which is known to clients that need to use the service, however, a client need not bind() its socket to a specific port in order to communicate with a server.
Not only is it unnecessary for all but a very few application protocols, it is dangerous for a client to bind() to a specific port number. There is a danger in conflicting with another socket that is already using the port number, which would cause the call to bind() to fail with WSAEADDRINUSE.
Alternative: Simply let the Winsock implementation assign the local port number implicitly when you call connect() (on stream or datagram sockets), or sendto() (on datagram sockets).
btw, you can get a better answer at Network Programming (http://www.codeguru.com/forum/forumdisplay.php?f=62)
Andreas Masur
December 27th, 2004, 03:33 PM
[ Moved thread ]
Mathew Joy
December 29th, 2004, 01:28 PM
I would say above 1024. Above 5000 is better. Avoid choosing ports that are easier to remember, it is likely that some other app has chosen it. If you are using too many client connections, you may have to bind to a port above 5000 before you use connect(). You can also use one from range of ports. So in case a port is already choosen you have another. Yet another way is to choose one port for your app, and tell the user to make sure no other application is using that port.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.