Solved

Detecting a PCs IP address in C++

Posted on 1998-08-11
1
571 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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++.

771 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