How good this code in terms of clarity, readability, and overall design? Easy to understand them or not?

Note: i put system("pause") which is not recommended. cin.get() is a better one in case you didn't know.

First: Port Scanner

/**program description:
 *
 * this is a simple port scanner
 * based on TCP connection. You
 * should insert IP and starting
 * port as well as ending port.
 * IPv4 is accepted only. Works 
 * on Windows OS
 *
 * exercise: do input validation
 *
 * Author: X-Shab
 * Copyright 2008 - 2009
 *
 */

#include<iostream>
#include<string>
#include<sstream>
#include<Ws2tcpip.h>            //used instead of "Winsock2.h"
using namespace std;

struct addrinfo hints;      	//input connection type and ip type 
struct addrinfo *fullInfo;	//holds full address information of the target host
string ip;
string port;

void initiateWinsockFacility()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int error;

    wVersionRequested = MAKEWORD(2, 2);

    error = WSAStartup(wVersionRequested, &wsaData);

    if (error != 0) 
    {
        cout<<"WSAStartup failed with error: "<<error<<endl;

	system("pause");
        exit(-1);
    }
}

//get full information about the connection to be made - fullInfo carries the output
void getFullAddressInformation(const char *ipAddress, const char *portNumber)
{
	int status = getaddrinfo(ipAddress, portNumber, &hints, &fullInfo);
	
	if(status != 0)
	{
		cout<<"getaddrinfo() error: "<< gai_strerror(status) <<endl;
		WSACleanup();
		exit(-1);
	}
}

string convertIntToString(int number)
{
	string stringOutput;
	
	stringstream ss;
	ss << number;
	ss >> stringOutput;

	return stringOutput;
}

int convertStringToInt(string stringStream)
{
	int integerOutput;

	stringstream ss;
	ss << stringStream;
	ss >> integerOutput;

	return integerOutput;
}

int convertConstCharToInt(const char *characters)
{
	int integerOutput;
	string stringFromConstChar(characters);
	
	stringstream ss;
	ss << stringFromConstChar;
	ss >> integerOutput;
	
	return integerOutput;
}

int main()
{
	initiateWinsockFacility();
	
	int endingPort;
	int socketFileDescriptor;

	system("cls");

	memset(&hints, 0, sizeof(hints));

	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;

	cout<<"Please enter your target IP: ";
	cin>>ip;

	cout<<"Enter Starting Port: ";
	cin>>port;

	cout<<"Enter Ending Port: ";
	cin>>endingPort;
	
	cout<<"Start Checking for Open Ports: "<<endl;

	int incrementingPort = convertStringToInt(port);

	while(incrementingPort <= endingPort)
	{
		getFullAddressInformation(ip.c_str(), port.c_str());
	
		socketFileDescriptor = socket(fullInfo->ai_family, fullInfo->ai_socktype, fullInfo->ai_protocol);
		
		if(socketFileDescriptor == INVALID_SOCKET )
		{
			cout<<"Error: failed to create a socket.\n";

			freeaddrinfo(fullInfo);
			WSACleanup();

			system("pause");
			exit(-1);
		}

		int connectStatus = connect(socketFileDescriptor, fullInfo->ai_addr, fullInfo->ai_addrlen);

		if(connectStatus == SOCKET_ERROR)
		{
			//check type of error you get
			if(WSAGetLastError() == 10060)
			{
				cout<<"Connection Timed Out - Please check your IP"<<endl;

				closesocket(socketFileDescriptor);
				freeaddrinfo(fullInfo);
				WSACleanup();

				system("pause");
				exit(-1);
			}

			cout<<"Port No. "<< port <<" is Closed"<<endl;
	
		}else{
			cout<<"Port No. "<< port <<" is Open"<<endl;
		}

		closesocket(socketFileDescriptor);
		freeaddrinfo(fullInfo);
	
		incrementingPort++;

		port = convertIntToString(incrementingPort);
	}

	/**not needed since its been implemented
	 * on the last loop above
	 *
	 * closesocket(socketFileDescriptor);
	 * freeaddrinfo(fullInfo);
	 *
	 */

        WSACleanup();
	system("pause");

	return 0;
}

Not bad..Good example for beginners..Whats 'she' do anyway..Checking for ports that's been opened? or what..(sorry for my bad English)

Not bad..Good example for beginners..Whats 'she' do anyway..Checking for ports that's been opened? or what..(sorry for my bad English)

Yes it checks which ports are opened and which are closed...You specify starting port and ending port.

Well..Since you already know which one is recommended..Good job dude..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.