Solved

Get my machine IP adress

Posted on 1998-07-14
3
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 109
isEverywhere  challenge 19 85
Windows shortcut location resolution on double click open 4 50
How do I get Window Title of all opened process? 4 33
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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

762 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