Thread: why doesn't getsockname() do what I want?

  1. #1
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    why doesn't getsockname() do what I want?

    The project that i'm working on is a server. I want the server to display my IP (instead of waiting for my logs to show it). I tried using getsockname(), however, it is to my knowledge that I need to use connect() before calling getsockname() in order to get my ip. Since I use listen() to wait for a client I'm not even sure that I should use a connect() call. Does anyone know what I need to do? Or maybe even a completely different approach to the problem.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You could try using

    gethostname()

    to get the hosts name then

    gethostbyname()

    to receive a list of addresses in the hostnet struct.

    the getsockopt() is probably failing as you are yet to connect.

    from MSDN
    "The getsockname function does not always return information about the host address when the socket has been bound to an unspecified address, unless the socket has been connected with connect or accept (for example, using ADDR_ANY). A Windows Sockets application must not assume that the address will be specified unless the socket is connected. The address that will be used for the socket is unknown unless the socket is connected when used in a multihomed host. If the socket is using a connectionless protocol, the address may not be available until I/O occurs on the socket."
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Please explain exactly what you are trying to accomplish. Are you having problems getting a server/client program to work?

    Kuphryn

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'll try that out novacain. It sounds like that just may work. To answer kuphryn's question, I am not having any trouble getting it to work. I am trying to make the server a little smarter by having it know details such as its own IP and whether or not there is an internet connection. If I can't get this stuff to work I'll live. It would just be a nice addition to have the server log something like:

    Connection made (255.255.255.255)

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Kinda ironic how I used the ip 255.255.255.255 for my example. I'm getting info about myself. Which does make sense as I would be the host. But what I want is my ISP assigned ip. Can anyone help?

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>But what I want is my ISP assigned ip.

    But then you have a connection to get assigned an IP, otherwise it will be in your reg if static (like a local IP 192.168.xxx.xxx with sub net 255.255.xxx.xxx for your NIC).

    Look at

    RasEnumConnections() to get the handle and RasGetProjectionInfo() for the IP.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I had to download a newer version of mingw since mine didn't have the ras api. But it works! Novacain is the hero of the day Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getsockname() bad address error
    By iemonslice in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-10-2004, 12:26 PM