Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-04-15
11
Medium Priority
?
266 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
[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
11 Comments
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 664 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 664 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 672 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 672 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

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!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

609 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