Solved

Detecting a PCs IP address in C++

Posted on 1998-08-11
1
612 Views
Last Modified: 2008-02-01
I've been trying to write a small command line utility which will detect the IP address of a PC. This is going to be used in a login script to work out what geographical region the PC is located in.

I've tried to implement this using the winsock function as shown below, but I keep on hitting the brick wall.

The WSAAddressToString & getsockname functiosn will give the address, but I first need to bind the address tothe socket. catch - 22.

Does anyone know how to query the IP address ?

Daniel.

    INT protocols[10+1];
      BYTE buffer[2048];
    DWORD bytesRequired;
      INT err;
      PPROTOCOL_INFO protocolInfo;
      PCSADDR_INFO csaddrInfo;
    INT protocolCount;
      INT addressCount;
      INT i;
    DWORD protocolIndex;
      SOCKET s;
      sockaddr FAR *name;
      int sizename;
      LPSTR  l_string;
      DWORD lengthString=256;
      WSADATA wsaData;

    bytesRequired = sizeof(buffer);
    err = EnumProtocols(NULL/*IPPROTO_TCP*/, buffer, &bytesRequired );
      if ( err <= 0 ) return;

    protocolCount = err;
    protocolInfo = (PPROTOCOL_INFO)buffer;  
    for ( i = 0, protocolIndex = 0;
        i < protocolCount && protocolIndex < 10;
        i++, protocolInfo++ )
            {
            
            if (WSAStartup(MAKEWORD(2,0),&wsaData )) return;
            s =      socket(protocolInfo->iAddressFamily,SOCK_STREAM,protocolInfo->iProtocol);
             if (getsockname(s,name,&sizename)) err=WSAGetLastError();
            l_string = (char *) calloc(1,256);
            WSAAddressToString(name,sizename,NULL,l_string,&lengthString);
 
 
            };
 
0
Comment
Question by:DanielGarcia
[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
1 Comment
 
LVL 23

Accepted Solution

by:
chensu earned 70 total points
ID: 1170022
Why do you use some WinSock 2 specific functions?

There is an example at
http://www.codeguru.com/internet/local_hostname.shtml
0

Featured Post

Industry Leaders: 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

738 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