Solved

Detecting a PCs IP address in C++

Posted on 1998-08-11
1
589 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Implementing a ResolveEventHandler in C++ 13 131
Grammars for C C++ and java 1 122
C++ standard library based binary archive format 6 98
IdTCPClient1->Disconnect(); not working 3 66
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

813 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

10 Experts available now in Live!

Get 1:1 Help Now