Solved

IP lookup of localhost, ping IPs and use that to draw up a simple local network table

Posted on 2004-04-15
11
241 Views
Last Modified: 2010-04-01
I was planning on using Java, but it wasn't quite working out for me, so I'm going back to what I'm a little more comfortable with, C++. Due to time restrictions, I'm not going to be making a GUI for it, or even making it look pretty, I just want something that'll work. I've never done any network programming in C++, so that's what brings me here. As of now, I'm looking to simply create a list of IPs (and possibly the names) of the computers connected to the same network (and subnet, not going outside of the subnet) as the computer running the program. I plan to do this by first having the program find the IP of the machine that's running the program, and then put that into a sort of formula to go through and ping each of the other possible IPs on the network. If a ping return is detected, it will add that to the list, if not, it will skip it and go on to the next IP to ping. I know it may not be the fastest way to do it, but I am on a class C network, and it also seems it will be the easiest to do (easier to accomplish the first time means I don't waste time trying to get a more complex solution working). I'm looking for anyone who could either give some code samples to take a look at, or refer to a resource that they feel may help out. I'd very greatly appreciate any help anyone can offer. Thank you!
0
Comment
Question by:Shinru
11 Comments
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 166 total points
ID: 10832309

This should hepl u if y are new to network programming

http://www.ecst.csuchico.edu/~beej/guide/net/

Amit
0
 
LVL 19

Assisted Solution

by:mrwad99
mrwad99 earned 166 total points
ID: 10832362
..alternatively for a less indepth, more Windows (Winsock) oriented:

http://www.hal-pc.org/~johnnie2/winsock.html
0
 
LVL 86

Expert Comment

by:jkr
ID: 10832841
What OS are you using? On Win32, 'NetServerEnum()' (yes, the name is a bit misleading) will help you doing that even without having to 'ping' the hosts (it list only 'running' machines). See the sample at http://win32.mvps.org/network/nse.c
0
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!

 

Author Comment

by:Shinru
ID: 10836112
I am running Win32. You're saying the 'NetServerEnum()' function will automatically find and list all connected devices on the network? That would make things MUCH easier (and I might be able to add in the additional functionality I was hoping for). I have a couple questions about that though, first of all, is it in any specific library, and if so, which one? Secondly, do you have any examples of using that? How would I go about using that function in conjunction with displaying its results (IP and hostname).
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 168 total points
ID: 10836250
It will find all computers on the network. If you need e.g. shares, printers and so on, you should look at 'WNetEnumResource()' e.g. http://win32.mvps.org/network/wnoe_wner.c

#include <stdio.h>
#include <windows.h>
#pragma hdrstop



// compile and link with

//      cl wnoe_wner.c mpr.lib


int main( void );
int doEnum( int level, NETRESOURCE *pnr );
void exterr( void );



void exterr( void )
{
      char errbuf[2048], namebuf[2048];
      DWORD err;

      errbuf[0] = namebuf[0] = '\0';
      if ( WNetGetLastError( &err, errbuf, sizeof errbuf, namebuf, sizeof namebuf ) == NO_ERROR )
            printf( "Error %lu (\"%s\") reported by \"%s\".\n",
                  err, errbuf, namebuf );
      return;
}



int doEnum( int level, NETRESOURCE *pnr )
{
      DWORD rc, rc2;
      HANDLE hEnum;
      DWORD count, bufsize, ui;
      NETRESOURCE buf[200];
      const char *type, *cont;

      rc = WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, pnr, &hEnum );
      if ( rc == ERROR_ACCESS_DENIED )
      {
            printf( "%-6.6s %-4.4s%*s  Error 5 -- access denied\n", "", "", level * 2, "" );
            return 1;
      }

      if ( rc )
      {
            printf( "WNOE(): rc = %lu, gle = %lu\n", rc, rc2 = GetLastError() );
            if ( rc2 == ERROR_EXTENDED_ERROR )
                  exterr();
            return 0;
      }

      while ( 1 )
      {
            count = (DWORD) -1L;
            bufsize = sizeof buf;
            rc = WNetEnumResource( hEnum, &count, buf, &bufsize );
            if ( rc != NO_ERROR )
                  break;
            for ( ui = 0; ui < count; ++ ui )
            {
                  switch ( buf[ui].dwDisplayType )
                  {
                        case RESOURCEDISPLAYTYPE_DOMAIN:
                              type = "domain"; break;
                        case RESOURCEDISPLAYTYPE_GENERIC:
                              type = "generic"; break;
                        case RESOURCEDISPLAYTYPE_SERVER:
                              type = "server"; break;
                        case RESOURCEDISPLAYTYPE_SHARE:
                              type = "share"; break;
                        default:
                              type = "unknown"; break;
                  }
                  cont = ( buf[ui].dwUsage & RESOURCEUSAGE_CONTAINER )? "container": "";
                  printf( "%-6.6s %-4.4s%*s  %s (%s)\n", type, cont, level * 2, "",
                        buf[ui].lpRemoteName, buf[ui].lpProvider );
                  // now we recurse if it's a container
                  if ( buf[ui].dwUsage & RESOURCEUSAGE_CONTAINER )
                        doEnum( level + 1, &buf[ui] );
            }
      }

      if ( rc != ERROR_NO_MORE_ITEMS ) // bad things
      {
            printf( "WNER(): rc = %lu, gle = %lu\n", rc, rc2 = GetLastError() );
            if ( rc2 == ERROR_EXTENDED_ERROR )
                  exterr();
      }

      WNetCloseEnum( hEnum );
      return 1;
}



int main( void )
{
      doEnum( 0, NULL );

      return 0;
}


The hostname will be returned in 'lpRemoteName' if the resource type is RESOURCEDISPLAYTYPE_SERVER. When you have the name, strip the leading backslashes and call 'gethostbyname()' as illustrated in e.g. http://support.microsoft.com/default.aspx?scid=kb;en-us;183988 ("HOWTO: Retrieve the IP Address of the Remote PPP Peer"):

   BOOL GetIpAddress(char *hostname)
   {
      WCHAR    msg[128];
      HOSTENT *lpHost=NULL;
           struct sockaddr_in   dest;

      lpHost = gethostbyname(hostname);
      if (lpHost == NULL)
      {
         wsprintf(msg, L"gethostbyname failed: %d", WSAGetLastError());
         MessageBox(NULL, msg, NULL, MB_OK);
      }
      else
      {
         for(int i=0; lpHost->h_addr_list[i] != NULL ;i++)
         {
            memcpy(&(dest.sin_addr), lpHost->h_addr_list[i],
                   lpHost->h_length);
            wsprintf(msg, L"IP address is: '%S'",
                     inet_ntoa(dest.sin_addr));
            MessageBox(NULL, msg, L"IP Address", MB_OK);
         }

      }
      return 0;
   }

The same applies if you are using 'NetServerEnum()' (http://win32.mvps.org/network/nse.c)
0
 
LVL 86

Accepted Solution

by:
jkr earned 168 total points
ID: 10892678
Is there anything else you need help with?
0
 

Author Comment

by:Shinru
ID: 10895527
No, sorry just haven't gotten to distribute points yet, but I will, I still have the links to this thread so don't worry. Thanks for the help by the way!
0
 
LVL 86

Expert Comment

by:jkr
ID: 11076756
Um, not really thinking that the links given are specific enough for a split. The 1st one is about using sockets in general ("Using Internet Sockets"), which does not apply and the other being a Winsock tutorial ("Johnnie's Winsock Tutorial"). Enumerating resources on a Windows network does not use sockets directly.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11076794
Hmm - I'll agree with that if one of the other participants explains how to do what was asked for using sockets. Especially for the ICMP ping part.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11086338
Any other opinions than just mine?
0

Featured Post

Independent Software Vendors: 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

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. …
  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 …
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 how to clear a vector as well as how to detect empty vectors in C++.

713 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