Solved

Detecting a PCs IP address in C++

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

830 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