Socket: how to get a unique port number ?

Discussion in 'Server Networking' started by Polaris, Apr 4, 2005.

  1. Polaris

    Polaris Guest


    I'm writing a server program on WinXP using Win Socket. I need to assign a
    listening port for my server. Just wonder if there is any API I can use to
    find an currently un-used port for the system? Seems the answer is "no", but
    just like to make sure, or is there any other way to find a port which is
    not in use?

    I'm not using MFC's socket class.

    Thanks in Advance!
    Polaris, Apr 4, 2005
    1. Advertisements

  2. If you need a specific number that you will always know what it is,..
    then, is "open season" on anything above port 5000. There is always a
    chance you will use on that some other Application uses, that is just the
    way it is.
    Phillip Windell, Apr 4, 2005
    1. Advertisements

  3. Polaris

    Sinna Guest


    I had the same problem about two weeks ago.
    I solved it by using GetTcpTable (the API equivalent of NETSTAT).
    By sorting the returned list I'm able to assign a port to my process
    that is not in use currently.
    I don't think it's completely fail-safe, but it's up to the other
    programs to not launch their process on a port that I have in use.

    Sinna, Apr 5, 2005
  4. Polaris

    Dave Guest

    how do you plan on telling your client app how to find the port the server
    is listening on? normally the server side port is a static assignment so
    the clients know how to connect to it. It could be changed like you can
    change the http port in iis, but then of course you have to have a way to
    tell your clients to also change the port they try to connect to.
    Dave, Apr 5, 2005
  5. Polaris

    Polaris Guest

    Hi everyone:

    In my case, the client and server are running on the same machine. The
    server can write the listening port in registry and the client can read it
    there. I think I will try GetUdpTable ...

    Thanks all for your help !

    Polaris, Apr 5, 2005
  6. Polaris

    Sinna Guest

    Well, sorry to mention, but client and server are running on the same PC
    and when launching the client, the server is launched on a given port
    (determined by the client). When the client shuts down, the server is
    also shut down.

    Sinna, Apr 6, 2005
  7. Polaris

    Sinna Guest

    Don't use GetUdpTable, but GetTcpTable!
    I can't imagine you're using the UDP-protocol instead of the TCP-protocol.

    Sinna, Apr 6, 2005
  8. Polaris

    Dave Guest

    so its not really a client-server connection just two processes on the same
    machine that need to talk. and its not even really a 'server' if it is only
    started when the client wants to use it, usually servers are sitting there
    listening all the time just waiting for connections from clients. you might
    want to look at other ipc mechanisms, you may find that some other method is
    better suited for what you want to do than tcp/ip.
    Dave, Apr 6, 2005
  9. Polaris

    Sinna Guest

    I know, but in fact the server acts as a proxy for a Remote Device. We
    use the TCP/IP-mechanism so the client doesn't has to know if it's
    connecting to a Local IP or a Remote IP.
    This reduces a lot of reprogramming.

    Sinna, Apr 6, 2005
  10. Polaris

    Polaris Guest

    Thanks Sinna. I'm using UDP socket, I know for best security, TCP should be
    used, but I think both server and the client are running on the same
    machine, it should be ok and it is much simpler to code. But I might change
    it later.

    Polaris, Apr 6, 2005
  11. Not security , but reliability , but that not so relevant for you ( the same
    host )
    Arkady Frenkel, Apr 7, 2005
  12. Polaris

    Polaris Guest

    Right, Rilability it is ... Thanks !

    Polaris, Apr 7, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.