Solved

Get my machine IP adress

Posted on 1998-07-14
3
261 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:visualc
3 Comments
 

Author Comment

by:visualc
ID: 1319124
Adjusted points to 150
0
 

Accepted Solution

by:
mulenga earned 150 total points
ID: 1319125
You can use the following code sequence to get the ipv4 address:
( this assumes that WinSock has been properly initialized )

char buf[200];
gethostname(buf, sizeof(buf));
HOSTENT *pHostent = gethostbyname( buf );
if ( pHostent )
{
  IN_ADDR *paddr = (IN_ADDR *) pHostent->h_addr_list[0];
  printf( "IP address is %s\n", inet_ntoa (  *paddr  ) );
}

Hope this helps!
0
 

Expert Comment

by:butsy
ID: 1319126
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( );
      }
}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now