okay, I'm trying to write a little portscanner, and all it can do right now is scan one port at a time. I am trying to use command-line arguments, but when I try to get a range of ports to scan, I have trouble getting the port number arguments (i.e., 1-1024). I can get the 1 just fine. Here is the code I have so far; hopefully someone can help me figure out the code to make this happen.

later on I'm going to make this scanner able to scan multiple IP addresses as well as multiple ports. anyway, the code:

Code:
#include <iostream>
#include <stdlib.h>
#include <winsock2.h>
using namespace std;

void usage (char *progname)
{
   cerr << "\nUsage:\n\t" << progname << " -h <host> -p <port>\n\n";
   cerr << "Where:\n\t<host> is the host you want to scan.\n\t<port> is the port you want to scan.\n";
   cerr << "\t<port> can be a range as well as a single number (i.e., -p 1-1024).\n\n";

   WSACleanup();
   exit (1);
}

int main (int argc, char *argv[])
{
   char *host_name = "localhost";
   u_short start_port;
   u_short end_port;
   int retval;
   u_int host_addr;
   struct sockaddr_in host;
   struct hostent *hp;
   WSADATA wsaData;
   SOCKET scan_sock;

   if (argc > 1)
   {
      for (int i = 1; i < argc; i++)
      {
         if ((argv[i][0] == '-') || (argv[i][0] == '/'))
         {
            switch (tolower (argv[i][1]))
            {
               case 'h':
                  host_name = argv[++i];
                  break;

               case 'p':
                  start_port = atoi (argv[++i]);
                  /* what the f**k do I put here?! */

                  break;

               default:
                  usage (argv[0]);
                  break;
            }
         }
         else
            usage (argv[0]);
      }
   }

   if (WSAStartup (0x0202, &wsaData) == SOCKET_ERROR)
   {
      cerr << "\nWSAStartup() failed with error: " << WSAGetLastError() << endl;
         WSACleanup();
         return 0;
   }

   if (((start_port <= 0) || (start_port > 65535)) || ((end_port <= 0) || (start_port > 65535)))
      usage (argv[0]);

   if (isalpha (host_name[0]))
      hp = gethostbyname (host_name);
   else
   {
      host_addr = inet_addr (host_name);
      hp = gethostbyaddr ((char*)&host_addr, 4, AF_INET);
   }

   if (hp == NULL)
   {
      cerr << "\nCannot resolve address [" << host_name << "]: ";
      cerr << "Error: " << WSAGetLastError() << endl;
      WSACleanup();
      return 0;
   }

   scan_sock = socket (AF_INET, SOCK_STREAM, 0);

   if (scan_sock < 0)
   {
      cerr << "\nError opening socket: " << WSAGetLastError() << endl;
      WSACleanup();
      return 0;
   }

   cout << "\nScanning [" << host_name << "] for open ports...\n\n";
   cout << "\nPORT\tSTATUS\n\n";

   if ((start_port > 0) || (start_port < 65536))
   {
      /* copy resolved host information to struct sockaddr_in */
      memset (&host, 0, sizeof (host));
      memcpy (&(host.sin_addr), hp->h_addr_list, hp->h_length);
      host.sin_family = AF_INET;
      host.sin_port = htons (start_port);
      host.sin_addr.S_un.S_addr = host_addr;

      retval = connect (scan_sock, (struct sockaddr*)&host, sizeof (host));

      if (retval == 0)
      {
         cout << start_port << "\tOpen\n\n";
         closesocket (scan_sock);
         WSACleanup();
         return 0;
      }

      else
      {
         cout << start_port << "\tClosed\n\n";
         closesocket (scan_sock);
         WSACleanup();
         return 0;
      }
   }

   else if (((start_port > 0) && (start_port < 65536)) && ((end_port > 0) && (end_port < 65536)))
   {
      /* copy resolved host information to struct sockaddr_in */
      memset (&host, 0, sizeof (host));
      memcpy (&(host.sin_addr), hp->h_addr_list, hp->h_length);
      host.sin_family = AF_INET;
      host.sin_addr.S_un.S_addr = host_addr;

      for (int i = start_port; i <= end_port; i++)
      {
         host.sin_port = htons (i);

         retval = connect (scan_sock, (struct sockaddr *)&host, sizeof (host));

         if (retval == 0)
         {
            cout << i << "\tOpen\n\n";
            closesocket (scan_sock);
            WSACleanup();
            return 0;
         }

         else
         {
            cout << i << "\tClosed\n\n";
            closesocket (scan_sock);
            WSACleanup();
            return 0;
         }
      }
   }

   return 0;
}