Port Scanner in C++

Thread Solved
Reply

Join Date: Oct 2006
Posts: 172
Reputation: HLA91 is an unknown quantity at this point 
Solved Threads: 2
HLA91 HLA91 is offline Offline
Junior Poster

Port Scanner in C++

 
0
  #1
Jan 7th, 2008
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
  1. /*--------------------
  2. | if your using dev-c++
  3. | you need to link
  4. | -lws2_32
  5. | to your application
  6. ---------------------*/
  7.  
  8. #include <winsock2.h>
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. char IP[20];
  13. int start, end, temp, err, nret;
  14. SOCKET sock;
  15. SOCKADDR_IN Info;
  16. WSADATA wsadata;
  17.  
  18. int main()
  19. {
  20. err = WSAStartup(MAKEWORD(2, 2), &wsadata);
  21. if(err != 0)
  22. {
  23. cout << "Error with winsock. Will Now Exit." << endl;
  24. cin.get();
  25. return 0;
  26. }
  27.  
  28. cout << "Target IP: ";
  29. cin>>IP;
  30. cout << "Starting Port: ";
  31. cin>>start;
  32. cout << "End Port: ";
  33. cin>>end;
  34.  
  35. cin.ignore();
  36.  
  37. cout << endl << endl << "Starting Scan..." << endl << endl;
  38.  
  39. temp = start;
  40. while(temp < end)
  41. {
  42. sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  43.  
  44. Info.sin_family = AF_INET;
  45. Info.sin_port = htons(start);
  46. nret = connect(sock, NULL, NULL);
  47. // error is for line above
  48. if(nret != SOCKET_ERROR)
  49. {
  50. cout << "Port " << temp << " - OPEN! " << endl;
  51. }
  52. temp++;
  53. closesocket(sock);
  54. }
  55. cout << endl << "Finished With Scan..." << endl;
  56.  
  57. cin.get();
  58. return 0;
  59. }


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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Port Scanner in C++

 
0
  #2
Jan 7th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 172
Reputation: HLA91 is an unknown quantity at this point 
Solved Threads: 2
HLA91 HLA91 is offline Offline
Junior Poster

Re: Port Scanner in C++

 
0
  #3
Jan 7th, 2008
Can you explain in a bit more detail please, I don't quite get what you are saying?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Port Scanner in C++

 
0
  #4
Jan 7th, 2008
Say
nret = connect(sock, NULL, 0);

C++ defines NULL as a pointer.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC