Hi all

I was looking for a port scanner to see what was open on my laptop and I thought as I was learning C++ why not search for a C++ port scanner. I came across this code I can make sense of most of it but it wont compile. I have linked the -lws2_32 file that I was told is needed because I use Dev, I looked in Project -> Project Options -> Parameters -> and i went to the little folder icon and searched for the library in the lib but it wasn't there is i just went to the "Linker" edit box and typed -lws2_32 but that didn't work either. Here is the code

/*--------------------
| if your using dev-c++
| you need to link
| -lws2_32
| to your application
---------------------*/

#include <winsock2.h>
#include <iostream>
using namespace std;

char IP[20];
int start, end, temp, err, nret;
SOCKET sock;
SOCKADDR_IN Info;
WSADATA wsadata;
  
int main()
{
   err = WSAStartup(MAKEWORD(2, 2), &wsadata);
   if(err != 0)
   {
      cout << "Error with winsock. Will Now Exit." << endl;
      cin.get();
      return 0;
   }
  
   cout << "Target IP: ";
   cin>>IP;
   cout << "Starting Port: ";
   cin>>start;
   cout << "End Port: ";
   cin>>end;
  
   cin.ignore();
  
   cout << endl << endl << "Starting Scan..." << endl << endl;
  
   temp = start;
   while(temp < end)
   {
      sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
      Info.sin_family = AF_INET;
      Info.sin_port = htons(start);
      nret = connect(sock, NULL, NULL);
      // error is for line above
      if(nret != SOCKET_ERROR)
      {
         cout << "Port " << temp << " - OPEN! " << endl;
      }
      temp++;
      closesocket(sock);
    }
    cout << endl << "Finished With Scan..." << endl;
    
    cin.get();
    return 0;
}

These are the error messages I am getting on line 46 it has a comment underneath
C:\Dev-Cpp\port.cpp In function `int main()':
C:\Dev-Cpp\port.cpp [Warning] passing NULL used for non-pointer converting 3 of `int connect(SOCKET, const sockaddr*, int)'
[Linker error] undefined reference to `__cpu_features_init'
C:\Dev-Cpp\port.cpp ld returned 1 exit status

Can someone please explain what is wrong, I think it is because I am not linking that file correctly?


Cheers

HLA91

C++ is a little bit more strict about how it defines NULL than C. The compiler is complaining because you are passing a (void *)0 as the third argument, when it expects an (int)0.

Hope this helps.

Can you explain in a bit more detail please, I don't quite get what you are saying?

Say nret = connect(sock, NULL, 0); C++ defines NULL as a pointer.

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.