Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ Compiling problems


ghost's Avatar
0 0

I wrote this code (which is based off of the tutorial on port scanners) today, which is supposed to be a simple port scanner. (don't try to correct anything that i may have messed up… unless it contributes to the compiling problem). I have yet to be able to compile it. I keep getting a build error, but it wont tell me what the error is. While the file linking was turned on I got the error(s):[Linker Error] undefined reference to "WSAStartup@8" and it gave me like 6 of these for different winsock functions. I suppose that this has something to do with the build error as well. Anyway im using Bloodshed DEV-CPP. Never had this kind of problem with it before. Anyway here is the code:

#include <winsock.h>
#include <windows.h>

using namespace std;

int main()
{
    SetConsoleTitle("Port Scanner" );
    
    char ip[MAX_PATH];
    cout << "Enter IP Address: ";
    cin >> ip;
    cout << endl;
    
    SOCKET mysocket;
    WSAData wsaData;
    mysocket = WSAStartup (MAKEWORD(1,1),&wsaData);
    
    if(mysocket == SOCKET_ERROR)
    {
        cout << "Socket Error...\n";
    }else{    
        mysocket = socket (AF_INET, SOCK_STREAM, 0);
        
        int x;
        for(x=1; x<6000; x++)
        {
             SOCKADDR_IN server;
             server.sin_port = htons(x);
             server.sin_family = AF_INET;
             server.sin_addr.s_addr = inet_addr(ip);
             
             connect(mysocket,(SOCKADDR*)(&server),sizeof(&server));
             
             if(mysocket == SOCKET_ERROR)
             {
                 cout << x << "\t Closed\n";
             }else{
                 cout << x << "\t Open\n";
             }        
         }
         closesocket(mysocket);
         WSACleanup();
    }
}; ```

thanks for any help.

ghost's Avatar
0 0

create a project and add that file to the projects, then include the library for wsock32 :)


ghost's Avatar
0 0

exactly how do i go about including the lib file wsock32 … (you are talking about wsock32.a right?)


ghost's Avatar
0 0

what compiler are you using?


ghost's Avatar
0 0

BloodShed's Dev-CPP


ghost's Avatar
0 0

right, click the new project button, and label it as an empty project, name it whatever you like. then right click on the name of it in the box on the right and click add to project, and add a file containing this code to the project

then click project at the top menu bar, then project options, then parameters, then click "add library or object" and browse to dev-cpp/lib/wsock32.a


ghost's Avatar
0 0

alrighty, thanks alot. I would have never figured that one out myself, so thanks.


ghost's Avatar
0 0

#include <winsock2> thats all winsock is outa date use winsock2 for everything.