Solved

Get my machine IP adress

Posted on 1998-07-14
3
281 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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