| | |
Simple Port Scanner - What Do You Think?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 33
Reputation:
Solved Threads: 0
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
Note: i put system("pause") which is not recommended. cin.get() is a better one in case you didn't know.
First: Port Scanner
C++ Syntax (Toggle Plain Text)
/**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; }
Last edited by f.ben.isaac; Apr 12th, 2009 at 7:52 am.
![]() |
Similar Threads
- really need help getting rid of virus's and worms (Viruses, Spyware and other Nasties)
- Odd Errors When Compiling Winsock Code. (C++)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Multi Thread Help (Java)
- help...doubt regarding my UG project (Network Security)
- Port Scanner Reporting Zero Ports Opened. (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: Confusion with conversion of type1 to type2
- Next Thread: segfault... WHY!
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





