List Available ports
Hi, I'm working on a server-client assignment and I need to run my client program on several remote machines. The client program takes a port number as a parameter. How can I list the available ports on the machine running the server program, so I can use them in the client program? I know I can use netstat but I'm sorta confused with the options and flags. This is Unix btw.
This is why it's good there are a lot of ports, and why many of those numbers are publicly reserved for certain applications. That doesn't mean you can't use them -- it's convention, so more like you shouldn't use them. They are listed in /etc/services.
What you are proposing is not possible. Both the client and the server must have the port they will use predetermined before they connect -- since otherwise, they can't connect. It makes no sense for your client to connect to the server and ask what port to use to connect. And of course, there is no way for the server to tell the client what port to use without a connection.
Well, that is not quite true. If you are writing a private service, you can always phone your friend and say "I'm all set up, use port 666". (according to /etc/services, 666 is conventionally a doom server, but if you are not running a conventional doom server, it doesn't matter).
Anyway, pick an unused or reasonable port number (such as 666) and use that. If the machine is yours and you are not running any other kind of server at all, you can use anything, including 80 (normal http service), but if you use 80 (worst choice!), you run the chance of some bizarre web crawling automaton trying to connect to scan your robots.txt ;)
Yes, unfortunately many of the ports are reserved for applications that are no longer in use, and where never in widespread use to begin with.
Use whatever port you want, just be aware that if you use one like 80, you wont be able to urn a web server on the same machine, 99.9% of the 'reserved' ports haven't been used for decades.