Visitar URL original
Feature: OS-chosen port available to application · Issue #703 · cpp-netlib/cpp-netlib · GitHub
Skip to content

Feature: OS-chosen port available to application #703

Description

@eile

Use case: I want to let the OS choose the port by supplying port 0 during initialization. This allows multiple applications to run concurrently, and the actual port will be communicated e.g. through zeroconf to the user.

I need to know the port chosen from the application, e.g., by having an API on the server to query the used address and port.

For example, in https://github.com/HBPVIS/ZeroEQ/blob/0.6.0/tests/http/server.cpp#L244 the URI has port 0 by default, and the URI returned after initialization in line 255 will contain the chosen port so that the client can connect to the server.

API proposal:

class Server
{
    std::string port() const; // >0 after listen, 0 otherwise
    std::string address() const;
};

Activity

  1. deanberris commented on Nov 14, 2016

    @deanberris
    Member

    Hi @eile -- I'm so sorry I just saw this.

    I think it's worth being able to access the IP address and port used by the system and make it available to the user. I have a problem with returning strings though, as it may be better to return a copy of the address and the actual port values directly.

    I like the idea of exposing this from the server's API, and passing in options that help decide whether/how to initialise the server. For example, there are ways of specifying the port and address to listen to through the server's options type. It should be feasible to provide the IP address you'd want, and a 0 for the port. I suppose you're already doing that and would like a way in pulling it out.

    I saw your pull request before seeing this post.

    Thanks again, and I'd like to see an updated PR with these comments addressed.

    Cheers

  2. umennel commented on Jan 30, 2017

    @umennel

    I allow myself to comment on this, since I am interested in this feature as well. I think returning strings is ok because the internal data type is string already, so no conversion/copy is required in the getter functions. Returning another data type should incorporate changing the internal storage type as well.
    @elie, could you apply the changes to async_server as well?

  3. dnachbaur commented on Feb 1, 2017

    @dnachbaur

    See updated PR: #729

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions