Link to home
Start Free TrialLog in
Avatar of visualc
visualc

asked on

Get my machine IP adress

Hi!
I have developing a client-server application.
When a client request a connection, it must send a password and the client-machine IP-adress (the adress will
be memorate in a database).
 How can I obtain in run-time the machine IP adress, where
the application is running?

                                                 Tibi
Avatar of visualc
visualc

ASKER

Adjusted points to 150
ASKER CERTIFIED SOLUTION
Avatar of mulenga
mulenga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This code will get the host name and the IP address for the computer on wich code is run. This works on VisualC++ 5SP1/2 on NT4 and 95 (98 too)
Requirements:
#include <ltwinsock2.h>

Link with Winsoc32.lib

That is it:
{
      WORD wVersionRequested;
      WSADATA wsaData;
      char name[255];
      CString ip;
      PHOSTENT hostinfo;
      wVersionRequested = MAKEWORD( 2, 0 );

      if ( 0==WSAStartup( wVersionRequested, &wsaData ) )
      {

            if( 0 == gethostname ( name, sizeof(name)))
            {
                  if((hostinfo = gethostbyname(name)) != NULL)
                  {
                        ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
                  }
            }
           
            WSACleanup( );
      }
}