Thread: need help with this small code... pls..

  1. #1
    SuperGirl
    Guest

    need help with this small code... pls..

    Hi there, first of all, i want to make the "ip1" char, go 1 higher, and i don't know how to do that. (like from 127.0.0.1 to 127.0.0.2).

    And second, i'd like to know how to use it in system(); (or something else that does the same). i've tried adding it to system, but i get this error,
    error C2660: 'system' : function does not take 3 parameters

    How can i do this, please help.... i've searched my ass off..... hehe

    Code:
    #include<iostream.h>
    #include<process.h>
    
    int main()
    {
    	char ip1[15];
    	char ip2[15];
    
    	cout << "Start IP: ";
    	 cin >> ip1;
    
    	cout << "Stop IP: ";
    	 cin >> ip2;
    
    
    	if (ip1<ip2)
    	{
    		cout << "Start IP can not be higher than Stop IP." << endl;
    	}
    	else
    	{
    		cout << "Scanning: [" << ip1 << "] -> [" << ip2 << "]" << endl;
    
    		while(ip1!=ip2)
    		{
    			/* Here i want to add the "ip1" char to system(); */
    
    			/* Here i want the "ip1" char to go 1 higher. */
    		}
    	}
    
    	cin.get();
    	
    	return 0;
    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    if (ip1<ip2)
    That isn't doing what you want it to do. It's not comparing them lexigraphically (IE, 'a' < 'b' < 'c'), but rather simply comparing the addresses.

    It would be easiest to store the ips as 4 chars (think of them as bytes rather than letters, one for each octet in the IP), and then convert them to the human readable ip format

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <sstream>
    
    int main() {
    	unsigned char ip[4];
    	unsigned char upperLim;
    	int tempInt;  // read stuff as integers rather than characters
    
    	std::cout << "Enter space delimited IP, ie, 192 168 1 1\n";
    	for (int i = 0; i < 4; ++i) {
    		std::cin >> tempInt;
    		ip[i] = (unsigned char) tempInt;
    	}
    
    	std::cout << "Enter upper limit (inclusive, last octet only): ";
    	std::cin >> tempInt;
    	upperLim = (unsigned char) tempInt;
    
    	do {
    		std::ostringstream out; // use ostringstream to write the bytes into a string
    		for (int i = 0; i < 3; ++i) { // write out first 3 with a ,
    			out << (int) ip[i] << '.';
    		}
    		out << (int) ip[3];
    		std::cout << out.str() << '\n';
    // unixesque ping args, dunno if windows supports the -c option to ping or output redirection
    		std::string command_line_arg = "ping -c 1 " + out.str(); // + ">"  +out.str() + ".result";
    		system(command_line_arg.c_str());
    		ip[3]++;
    	} while  (ip[3] < upperLim);
    
    	return 0;
    }
    I sense some malicous use of this code... don't try to destroy stuff with it please.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Before calling system() you'll need to combine the strings/integers/other stuff using sprintf();

    Example:

    char ip1[] = "127.0.0.1";
    char pszBuffer[32];

    sprintf(pszBuffer, "ping %s", ip1);
    // pszBuffer now contains "ping 127.0.0.1;

    Now, to increment the IP is going to be a little tricky. You could make your own structure / class for IP's which would encapsulate all this... but here goes.

    Code:
    char *incrementIP(char *pszIP)
    {
    	char cDelim = '.';
    	unsigned int iIPField[4];
    
    	int i = 0;
    	do {
    
    		iIPField[i] = atoi(strtok(NULL, cDelim));
    		i++;
    
    	} while (i < 4);
    	i = 4;
    
    	while (iIPField[i] == 255)
    	{
    		iIPField[i] = 0;
    		i--;
    	} else {
    		iIPField[i]++;
    	}
    
    	char pszReturnIP[16];
    	sprintf(pszReturnIP, "%s.%s.%s.%s", iIPField[0], iIPField[1], iIPField[2], iIPField[3]);
    
    	return pszReturnIP;
    }
    Note, I didn't test this... it's almost 3AM, I have no clue why I just wrote that for you but I did... the function doesn't check for validity of the IP address... it assumes a real address is given (4 fields seperated by periods.

  4. #4
    SuperGirl
    Guest
    wow, thx guys....... don't worry i'm not gonna 'destroy' anything.

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by SuperGirl
    wow, thx guys....... don't worry i'm not gonna 'destroy' anything.
    i'm listening; what non-malicious use do you have for this code?
    hello, internet!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another approach:
    Code:
    #include <iostream>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    
    using namespace std;
    
    int main(void)
    {
        struct in_addr addr;
        unsigned long host_order;
        
        addr.s_addr = inet_addr("127.0.0.1");
        host_order = ntohl(addr.s_addr);
        
        for (int i = 0; i < 10; i++)
        {
            cout <<inet_ntoa(addr) <<endl;
            addr.s_addr = htonl(++host_order);
        }
        
    	return 0;
    }
    The IP address is stored as a long, as returned from inet_addr(), but it's in network byte order. So, using ntohl(), convert it to host order in order to store a copy. Then increment the copy and update the network ordered version when you want to use it.

    This code ain't bullet proof, and requires Unix style sockets. But it can be adapted for Winsock use if that's what you want.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small code works on VS6 crash on VS2005 WHY???
    By salvadoravi in forum C Programming
    Replies: 7
    Last Post: 02-10-2008, 09:32 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. help on my code PLS!!!
    By gtr_s15 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 07:48 AM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  5. code fragment pls help
    By Damo in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 09:57 AM