• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

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

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
Shinru
Asked:
Shinru
4 Solutions
 
Sys_ProgCommented:

This should hepl u if y are new to network programming

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

Amit
0
 
mrwad99Commented:
..alternatively for a less indepth, more Windows (Winsock) oriented:

http://www.hal-pc.org/~johnnie2/winsock.html
0
 
jkrCommented:
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
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!

 
ShinruAuthor Commented:
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
 
jkrCommented:
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
 
jkrCommented:
Is there anything else you need help with?
0
 
ShinruAuthor Commented:
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
 
jkrCommented:
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
 
jkrCommented:
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
 
jkrCommented:
Any other opinions than just mine?
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now