How to get IP address will be used by HTTP in C++ - TCP-IP

This is a discussion on How to get IP address will be used by HTTP in C++ - TCP-IP ; At the client side, when my client machine talks to server, the server will see the IP address of my machine as part of HTTP request. My question is how I can programmatically get this IP address which will be ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: How to get IP address will be used by HTTP in C++

  1. How to get IP address will be used by HTTP in C++

    At the client side, when my client machine talks to server, the server
    will see the IP address of my machine as part of HTTP request. My
    question is how I can programmatically get this IP address which will
    be used in HTTP request? I could use gethostname to get IP addresses,
    but that is a list. How can I tell which one will be used?

    Thanks very much in advance.

    Leo

  2. Re: How to get IP address will be used by HTTP in C++

    In article ,
    wrote:
    >At the client side, when my client machine talks to server, the server
    >will see the IP address of my machine as part of HTTP request.


    Your wording implies that the IP address of the client is sent as
    a field in the HTTP GET request or accompanying headers. However,
    there is no such field shown for HTTP/1.1 in RFC 2616.

    > My
    >question is how I can programmatically get this IP address which will
    >be used in HTTP request? I could use gethostname to get IP addresses,
    >but that is a list. How can I tell which one will be used?


    The server will see a source IP address for the packets because that is
    a basic part of TCP/IP, not because it is sent specially for HTTP.

    This is an important distinction. The packet source IP address seen by
    the server is the IP address that the server needs to reply to in order
    to get the reply back to the client. This is not necessarily the same
    as any IP address that the client knows itself under! A client could
    have a dozen different IP addresses on its LAN, and yet all of those
    might undergo Network Address Translation (NAT) at one or more points
    between the client and the server.

    This changes the nature of the problem completely. The problem is not
    one of looking through a gethostbyname() list and picking out the one
    that will be shown to the server: the problem becomes one of looking
    through the client's internal routing tables and using them to make an
    interface routing decision equivilent to what the operating system
    would do. In the general case, the operating system routing
    decision could even depend upon what TCP source port happened
    to be allocated for the request [though this would not be common.]

    Note by the way that gethostbyname() only returns information
    from a DNS lookup or equivilent information table, not information
    about what interfaces are currently configured on the client,
    and definitely not information about which weighting algorithm
    the client is using to make routing decisions.

  3. Re: How to get IP address will be used by HTTP in C++

    With Walter having said why the IP the server see's might not be the
    IP at the client thanks to those darn end-to-end breaking NATs , if
    you are content to have the IP at the client, and don't need to know
    before the connection to the HTTP server is made, you can make a
    getsockname() call after the connect() call and retrieve both the
    (initial) local IP that is being used by the client TCP stack.

    rick jones
    --
    No need to believe in either side, or any side. There is no cause.
    There's only yourself. The belief is in your own precision. - Jobert
    these opinions are mine, all mine; HP might not want them anyway...
    feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...

+ Reply to Thread