Visual C++ Port Scanner

So I am writing the simple CLI based port scanner and for the LOVE of goodness I cannot get it to work. Please any suggestions? When I run it always returns no available open ports when I KNOW there is. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
void PortScan()
{
	char IPaddress[20];   

	int startport, endport, error;
	int x = 0;
	int y = 0;
	SOCKET sock; 
	SOCKADDR_IN Info; 
	WSADATA wsadata;


	cout << "Please enter in the host's IP address to scan ";
	cin >> IPaddress;


    error = WSAStartup(MAKEWORD(2, 2), &wsadata);
    
	if(error != 0) 
	{
	  cout << "Error with winsock. Will Now Exit." << endl;
	  exit(1);
	}
	
	
	cout << "Starting port scan on " << IPaddress << ".... " << endl;

	
	
	for(y=1;y<20000;y++)
	{
	
	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 

	 Info.sin_addr.s_addr = inet_addr(IPaddress);
	 Info.sin_family = AF_INET; 
	 Info.sin_port = htons(y); 
	 x = connect(sock, NULL, NULL);
	 
	 if(x != SOCKET_ERROR)
	  {
		 cout << "Port " << y << " - OPEN! " << endl;
	  }
	 
	  closesocket(sock); 
	}
   

	WSACleanup();

	cout << "Port scan has finished" << endl;
	system ("pause");


}

No matter what IP address I try it always returns no open ports.
I would recheck your code. Exactly what is the connect connecting to?
Topic archived. No new replies allowed.