Thread: Extracting WAN IP out of a SOCKET

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91

    Extracting WAN IP out of a SOCKET

    Hello everyone,

    I have an application which serves as a server. Whenever a client connects to the server, I want to display his or her IP as a string (in a MessageBox).

    All I have, is a SOCKET variable with an established connection to the client, getting the IP out of it should be possible, right?

    With kind regards,
    apsync

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Indeed you do get the client's IP upon their connection to you. But just be aware that getting your own WAN IP is not necessarily as simplistic since you will usually end up getting the IP of your router. Though if you have a direction connection to the 'net you should be good to even get your own WAN IP.

    Since "how" was never asked, I will assume you can see where to get their IP from your existing code (which you really should).

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Quote Originally Posted by master5001 View Post
    Indeed you do get the client's IP upon their connection to you. But just be aware that getting your own WAN IP is not necessarily as simplistic since you will usually end up getting the IP of your router. Though if you have a direction connection to the 'net you should be good to even get your own WAN IP.

    Since "how" was never asked, I will assume you can see where to get their IP from your existing code (which you really should).
    Hmm, yea I didn't ask how to retrieve it, but isn't that selfspeaking? I mean what else can the purpose of this topic be? ;-)

    I am not trying to get my own IP, but the clients. Since they are connecting to the server, I can't see their IP on my source either, or did I misunderstood you?

    Btw, other thing I have forgot to mention, which can be important too, is that I am trying to get this done in C.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fair enough, and don't worry. I usually program in C. Check out this since I don't feel like writing a basic server or guessing how much code you have to arbitrarily start.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Quote Originally Posted by master5001 View Post
    Fair enough, and don't worry. I usually program in C. Check out this since I don't feel like writing a basic server or guessing how much code you have to arbitrarily start.
    Thank you for the fast response, again, however I think I didn't explain too well.

    I already have a server that's functioning, spawns a thread when a client connects to the server, etc, and I only want to add the functionality to show someones IP, since I have no clue how to do is.

    I can post some code but it's like any other server application, so I thought that it wouldn't be important.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If we're in the Windows forum, that means MSDN, so when you type "socket address" and do a little clicking you get to here. SO_BSP_STATE would appear to be what you want, if I'm understanding you correctly.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Quote Originally Posted by tabstop View Post
    If we're in the Windows forum, that means MSDN, so when you type "socket address" and do a little clicking you get to here. SO_BSP_STATE would appear to be what you want, if I'm understanding you correctly.
    Yes, that is what I am looking for, however doesn't SO_BSP_STATE require Windows Vista?

    Very naïve of me to not mention my OS, I apologies. I am running Windows XP.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by apsync View Post
    Yes, that is what I am looking for, however doesn't SO_BSP_STATE require Windows Vista?

    Very naïve of me to not mention my OS, I apologies. I am running Windows XP.
    I don't think anything on that page requires anything newer than Win95. (I did a search for Vista, and it's not on that page except in the list of all the OS's at the end.)

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Quote Originally Posted by tabstop View Post
    I don't think anything on that page requires anything newer than Win95. (I did a search for Vista, and it's not on that page except in the list of all the OS's at the end.)
    On the page (the link in my previous post) at the bottom says Windows Vista or Windows Server 2008 is required. Also the chart on this page shows that only Windows Vista and Windows Server 2008 supports it.

    Any equivalent of SO_BSP_STATE that works on XP would be helpful.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then I guess it's back to Berkeley and getpeername().

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Don't forget that you can extract that information from the buffer passed to accept( ), as well.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Quote Originally Posted by tabstop View Post
    Then I guess it's back to Berkeley and getpeername().
    Oh, alright, I'll give it a shot.

    Quote Originally Posted by Sebastiani View Post
    Don't forget that you can extract that information from the buffer passed to accept( ), as well.
    Is that so? Where exactly is the remote address stored, or any examples I can have a look at?


    Kind regards,
    apsync

  13. #13
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    This is great, just found out where the octets are stored. Here is an example for anyone that is trying to achieve the same:

    Code:
    struct sockaddr_in sa_i;
    ...
    printf("%u.%u.%u.%u\n", sa_i.S_un.S_un_b.s_b1, sa_i.S_un.S_un_b.s_b2, sa_i.S_un.S_un_b.s_b3, sa_i.S_un.S_un_b.s_b4
    Thanks everyone who posted here to achieve my goal.

    With kind regards,
    apsync
    Last edited by apsync; 10-04-2008 at 02:38 PM.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, assuming a sockaddr_in variable 'info', ntohl( info.sin_addr.s_addr ) would yield the client's address.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem while constructing IP packet and sending using socket() system call
    By cavestine in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-15-2007, 05:49 AM
  2. Need help with retrieving and adding WAN IP to file
    By col sig sauer in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2007, 11:33 PM
  3. Question about My WAN IP
    By failure_to in forum Networking/Device Communication
    Replies: 5
    Last Post: 08-22-2004, 01:45 PM
  4. IP without connecting to external socket
    By Hunter2 in forum Networking/Device Communication
    Replies: 5
    Last Post: 08-26-2003, 07:50 AM