# Include <string. h>
# Include <Winsock. h>
# Include <windows. h>
# Include <iostream. h>
# Pragma comment (Lib, "ws2_32.lib ")
Int main (INT argc, char * argv [])
{
Cout <"************************************ * ************** "<Endl
<"Single-threaded port scanner" <Endl
<"Author: Dongfang" <Endl
<"************************************* * ************* "<Endl;
Int portstart = 1, portend = 65535;
Int mysocket;
Int portcount = 0;
Struct sockaddr_in my_addr;
Wsadata;
Word wversionrequested = makeword (); // you can specify the minimum version number.
If (argc <2)
{
Cout <"Usage:" <argv [0] <"host portstart portend/N" <Endl;
Exit (1 );
}
If (portstart> portend)
Cout <"error! The Start port number must be smaller than the end port number "<Endl;
Else
{
If (wsastartup (wversionrequested, & wsadata ))
{
Cout <"failed to connect to the socket library. Check whether the version number is 1.1/N" <Endl;
Exit (1 );
}
Portstart = atoi (argv [2]);
Portend = atoi (argv [3]);
Lable: For (INT I = portstart; I <portend; I ++)
{// Atoi () converts a string to a number
Cout <"creating socket..." <Endl;
If (mysocket = socket (af_inet, sock_stream, 0) = invalid_socket)
{
Cout <"socket establishment failed" <Endl;
Goto lable;
}
My_addr.sin_family = af_inet;
My_addr.sin_port = htons (I); // convert the port
My_addr.sin_addr.s_addr = inet_addr (argv [1]); // htonl (inaddr_any) pays all the IP addresses of the machine
Cout <"scanning port:" <I <Endl;
If (connect (mysocket, (struct sockaddr *) & my_addr, sizeof (struct sockaddr) = socket_error)
{
Cout <"Port" <I <"Disable! "<Endl;
Closesocket (mysocket );
}
Else
{
Portcount ++;
Cout <"Port" <I <"Open/N" <Endl;
}
}
Cout <"Target Host" <argv [1] <"slave" <portstart <"--" <portend <"Total" <portcount <" ports opened "<Endl;
Closesocket (mysocket );
Wsacleanup (); // in order to allow the ws2_32.dll to free any resources for the Application
}
Return 0;
}