| | |
Odd Errors When Compiling Winsock Code.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 33
Reputation:
Solved Threads: 0
Hello, first of all i'm glad to be a member here. Nice to be with you!
I wrote a simple dum port scanner. For practicing purposes. It worked fine under linux, i ported to windows, i linked it to winsock libraries. I keep getting errors, such as
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|29|error C3861: 'getaddrinfo': identifier not found|
Anyway here is my code if you want to see it:
Errors:
PLZ HELP.... I ALREADY DID ALL I CAN. I'm about to give up....
I wrote a simple dum port scanner. For practicing purposes. It worked fine under linux, i ported to windows, i linked it to winsock libraries. I keep getting errors, such as
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|29|error C3861: 'getaddrinfo': identifier not found|
Anyway here is my code if you want to see it:
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<string> #include<sstream> #include"WinSock2.h" using namespace std; struct addrinfo hints; //fill your host info - hints is input struct addrinfo *results; //gets your host info - results is output char *remoteIP = new char[40]; //holds inserted remote IP string tempPort; //holds ports temporarly int startingPort; //stores the starting range of port number int status; //receives the status of your pc address int currentPort; //holds current port //program description void progDesc() { cout<<"This is a simple port scanner, scan range of\n"; cout<<"ports on your local machine...\n"<<endl; } //getaddrinfe locates your machine, more specific //details of your host address is returned to results. void getAddrIn() { status = getaddrinfo(remoteIP, NULL , &hints, &results); if(status != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } } //Run the program int main() { int endingPort; //stores the ending rage of port number WORD wVersionRequested; WSADATA wsaData; int err; /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { /* Tell the user that we could not find a usable */ /* Winsock DLL. */ printf("WSAStartup failed with error: %d\n", err); return 1; } system("clear"); //tell user what program does progDesc(); //set size of hints to zero memset(&hints, 0, sizeof hints); //fill some of your host address info hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; //ask for remote IP cout<<"Please enter your target IP: "; cin>>remoteIP; getAddrIn(); //ask port range from user cout<<"Enter Starting Port: "; cin>>startingPort; cout<<"Enter Ending Port: "; cin>>endingPort; cout<<endl; cout<<"Start Checking: "<<endl; //check the status while(startingPort <= endingPort) { //call getaddrinfo() getAddrIn(); //create a socket. int socketfd; socketfd = socket(results->ai_family, results->ai_socktype, results->ai_protocol); if(socketfd == -1 ) { cout<<"Error: failed to create a socket.\n"; return 2; } struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr; specifyPort->sin_port = htons(startingPort); int connectStatus; connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen); if(connectStatus == -1 ) { if(errno == ECONNREFUSED) cout<<"Port "<<startingPort<<" is Closed or Blocked.\n"; //Alternatively, you can do: //cout << "connect failed; reason = " << strerror(errno) <<endl; }else{ cout<<"Port "<<startingPort<<" is OPEN.\n"; } closesocket(socketfd); //move to the next port in the specified range startingPort++; } //deallocate memory delete[] remoteIP; //free linkedlist of struct addrinfo *results freeaddrinfo(results); WSACleanup(); return 0; }
Errors:
C++ Syntax (Toggle Plain Text)
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\xlocale|342|warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|7|error C2079: 'hints' uses undefined struct 'addrinfo'| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|29|error C3861: 'getaddrinfo': identifier not found| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|33|error C3861: 'gai_strerror': identifier not found| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|53|error C2228: left of '.ai_family' must have class/struct/union| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|54|error C2228: left of '.ai_socktype' must have class/struct/union| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_family' must point to class/struct/union/generic type| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_socktype' must point to class/struct/union/generic type| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_protocol' must point to class/struct/union/generic type| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|90|error C2027: use of undefined type 'addrinfo'| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|90|error C2227: left of '->ai_addr' must point to class/struct/union/generic type| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2027: use of undefined type 'addrinfo'| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2227: left of '->ai_addr' must point to class/struct/union/generic type| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2027: use of undefined type 'addrinfo'| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2227: left of '->ai_addrlen' must point to class/struct/union/generic type| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|99|error C2065: 'ECONNREFUSED' : undeclared identifier| C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|119|error C3861: 'freeaddrinfo': identifier not found| ||=== Build finished: 19 errors, 1 warnings ===|
PLZ HELP.... I ALREADY DID ALL I CAN. I'm about to give up....
Last edited by f.ben.isaac; Oct 26th, 2008 at 1:47 am.
you are missing a header file -- see example program here
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2008
Posts: 1,407
Reputation:
Solved Threads: 114
I managed to make the code compile free of errors and warnings, here are the changes I made. Hope this helps :]
#include<iostream> #include<string> #include<sstream> #include<winsock2.h> #include<ws2tcpip.h> #pragma comment(lib, "Ws2_32.lib") using namespace std; struct addrinfo hints; //fill your host info - hints is input struct addrinfo *results; //gets your host info - results is output char *remoteIP = new char[40]; //holds inserted remote IP string tempPort; //holds ports temporarly int startingPort; //stores the starting range of port number int status; //receives the status of your pc address int currentPort; //holds current port //program description void progDesc() { cout<<"This is a simple port scanner, scan range of\n"; cout<<"ports on your local machine...\n"<<endl; } //getaddrinfe locates your machine, more specific //details of your host address is returned to results. void getAddrIn() { status = getaddrinfo(remoteIP, NULL , &hints, &results); if(status != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } } //Run the program int main() { int endingPort; //stores the ending rage of port number WORD wVersionRequested; WSADATA wsaData; int err; /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { /* Tell the user that we could not find a usable */ /* Winsock DLL. */ printf("WSAStartup failed with error: %d\n", err); return 1; } system("cls"); //tell user what program does progDesc(); //set size of hints to zero memset(&hints, 0, sizeof hints); //fill some of your host address info hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; //ask for remote IP cout<<"Please enter your target IP: "; cin>>remoteIP; getAddrIn(); //ask port range from user cout<<"Enter Starting Port: "; cin>>startingPort; cout<<"Enter Ending Port: "; cin>>endingPort; cout<<endl; cout<<"Start Checking: "<<endl; //check the status while(startingPort <= endingPort) { //call getaddrinfo() getAddrIn(); //create a socket. SOCKET socketfd; socketfd = (int) socket(results->ai_family, results->ai_socktype, results->ai_protocol); if(socketfd == -1 ) { cout<<"Error: failed to create a socket.\n"; return 2; } struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr; specifyPort->sin_port = htons(startingPort); int connectStatus; connectStatus = connect(socketfd, results->ai_addr, (int)results->ai_addrlen); if(connectStatus == -1 ) { if(errno == WSAECONNREFUSED) cout<<"Port "<<startingPort<<" is Closed or Blocked.\n"; //Alternatively, you can do: //cout << "connect failed; reason = " << strerror(errno) <<endl; }else{ cout<<"Port "<<startingPort<<" is OPEN.\n"; } closesocket(socketfd); //move to the next port in the specified range startingPort++; } //deallocate memory delete[] remoteIP; //free linkedlist of struct addrinfo *results freeaddrinfo(results); WSACleanup(); return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help the lazy Lad!!!
- Next Thread: C++ encryption
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






