I found a port scanner online that I wanted to plug into the compiler to tinker with
Code:#include <cstdlib> #include <iostream> #include <fstream> #include <string> #include <winsock.h> #include <algorithm> using namespace std; // declare globals SOCKET sock; ofstream outfile; //Connecttohost connects to a remote host bool ConnectToHost(int PortNo, char* IPAddress) { //start up winsock WSADATA wsadata; int error = WSAStartup(0×0202, &wsadata); //error did something happen if (error) { return false; } // check if we get the right winsock version if (wsadata.wVersion != 0×0202) { if (wsadata.wVersion != 0×0202) { WSACleanup(); return false; } SOCKADDR_IN target; target.sin_family = AF_INET; target.sin_port = htons (PortNo); target.sin_addr.s_addr = inet_addr (IPAddress); sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // create socket if (sock == INVALID_SOCKET) { return false; // could not create the socket } // try connecting if (connect(sock, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR) { cout << "false" << endl; outfile << "false" << endl; return false; //couldn’t connect } else { cout << "true" << endl; outfile << "true" << endl; return true; // success } } void CloseConnection () { if (sock) closesocket(sock); WSACleanup(); } char *convertStringToChar(const string &str) { char *retPtr(new char[str.length() + 1]); copy(str.begin(), str.end(), retPtr); retPtr[str.length()] = ”; return retPtr; } int main() { string line; int myport1; int myport2; int i; cout << "\n\n"; cout << "Enter the starting PORT You wish to attempt a connection on, example: \t 21 \n" << endl; cin >> myport1; cout << "Enter the Ending PORT You wish to attempt a connection on, example: \t 25 \n" << endl; cin >> myport2; outfile.open("outfile.txt"); ifstream myfile("input.txt"); if (myfile.is_open()) { while (!myfile.eof()) { getline(myfile, line); for (i = myport1; i <= myport2; i++) { ConnectToHost(i, convertStringToChar(line)); cout << i << " \t " << line << endl; outfile << i << " \t" << line << endl; } } myfile.close(); outfile.close(); } else cout << "Unable to open file input.txt\n\n"; system("PAUSE"); return EXIT_SUCCESS; }
here are the errors I got
1>------ Build started: Project: port scan, Configuration: Debug Win32 ------
1> port scan.cpp
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(2): warning C4627: '#include <cstdlib>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(3): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(4): warning C4627: '#include <fstream>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(5): warning C4627: '#include <string>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(6): warning C4627: '#include <winsock.h>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(7): warning C4627: '#include <algorithm>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(117): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Apparently it really wants this stdafx.h file so I included it and retried.
Code:#include "stdafx.h" #include <cstdlib> #include <iostream> #include <fstream> #include <string> #include <winsock.h> #include <algorithm> using namespace std; // declare globals SOCKET sock; ofstream outfile; //Connecttohost connects to a remote host bool ConnectToHost(int PortNo, char* IPAddress) { //start up winsock WSADATA wsadata; int error = WSAStartup(0×0202, &wsadata); //error did something happen if (error) { return false; } // check if we get the right winsock version if (wsadata.wVersion != 0×0202) { if (wsadata.wVersion != 0×0202) { WSACleanup(); return false; } SOCKADDR_IN target; target.sin_family = AF_INET; target.sin_port = htons (PortNo); target.sin_addr.s_addr = inet_addr (IPAddress); sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // create socket if (sock == INVALID_SOCKET) { return false; // could not create the socket } // try connecting if (connect(sock, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR) { cout << "false" << endl; outfile << "false" << endl; return false; //couldn’t connect } else { cout << "true" << endl; outfile << "true" << endl; return true; // success } } void CloseConnection () { if (sock) closesocket(sock); WSACleanup(); } char *convertStringToChar(const string &str) { char *retPtr(new char[str.length() + 1]); copy(str.begin(), str.end(), retPtr); retPtr[str.length()] = ”; return retPtr; } int main() { string line; int myport1; int myport2; int i; cout << "\n\n"; cout << "Enter the starting PORT You wish to attempt a connection on, example: \t 21 \n" << endl; cin >> myport1; cout << "Enter the Ending PORT You wish to attempt a connection on, example: \t 25 \n" << endl; cin >> myport2; outfile.open("outfile.txt"); ifstream myfile("input.txt"); if (myfile.is_open()) { while (!myfile.eof()) { getline(myfile, line); for (i = myport1; i <= myport2; i++) { ConnectToHost(i, convertStringToChar(line)); cout << i << " \t " << line << endl; outfile << i << " \t" << line << endl; } } myfile.close(); outfile.close(); } else cout << "Unable to open file input.txt\n\n"; system("PAUSE"); return EXIT_SUCCESS; }
again, a list of errors
1>------ Build started: Project: port scan, Configuration: Debug Win32 ------
1> port scan.cpp
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(20): error C2146: syntax error : missing ')' before identifier '×0202'
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(20): error C2660: 'WSAStartup' : function does not take 1 arguments
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(20): error C2059: syntax error : ')'
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(30): error C2146: syntax error : missing ')' before identifier '×0202'
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(30): error C2059: syntax error : ')'
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(30): error C2065: '×0202' : undeclared identifier
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(30): error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(31): error C2146: syntax error : missing ')' before identifier '×0202'
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(31): error C2059: syntax error : ')'
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(31): error C2065: '×0202' : undeclared identifier
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(31): error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(64): error C2601: 'CloseConnection' : local function definitions are illegal
1> c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(16): this line contains a '{' which has not yet been matched
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(72): error C2601: 'convertStringToChar' : local function definitions are illegal
1> c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(16): this line contains a '{' which has not yet been matched
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(83): error C2601: 'main' : local function definitions are illegal
1> c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(16): this line contains a '{' which has not yet been matched
1>c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(117): fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\rian saville\my documents\visual studio 2010\projects\port scan\port scan\port scan.cpp(16)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And the first time I compiled it, it thought for some reason that words inside quotes didn't count as strings. Oddly enough, it recognised them when I deleted and reentered the quotes. Is the code wrong or does VS just not like it when you copy and paste? If someone can give me a simpler port scan, I'd appreciate it. That's all I want.



LinkBack URL
About LinkBacks



