Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Detecting a PCs IP address in C++

Posted on 1998-08-11
1
Medium Priority
?
619 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 280 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

722 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