Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get subnet mask

Status
Not open for further replies.

fmaurer

Technical User
May 15, 2007
11
PL
Hi, I am the very beginner in network programming and need to find a way to show all IP addresses being in the same network. I considered solution of getting machine's IP and subnet mask and perform some bit operations. I've already found the code to get the IP address of the local machine
Code:
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string>


 int GetHostIP()
{
    char *Ip;
    WSADATA wsaData;
    struct hostent *pHostEnt=NULL; 
    struct sockaddr_in tmpSockAddr; //placeholder for the ip address

    // Not needed if it is already taken care by some other part of the application
    WSAStartup(MAKEWORD(2,0),&wsaData); 

    char hostname[100]; //assume windows doesn't allow having longer hostnames
	gethostname(hostname, sizeof hostname);
    //This will retrieve the ip details and put it into pHostEnt structure
    pHostEnt = gethostbyname(hostname);

    if(pHostEnt == NULL)
    {
        printf("Error occured: %s\n",GetLastError());
		WSACleanup();
        return 1;
    }
but still have no idea how to get a subnet mask.
I would be grateful for any suggestions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top