Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get my machine IP adress

Posted on 1998-07-14
3
Medium Priority
?
325 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 600 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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