Thread: List Available ports

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    10

    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.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM