Home > Back-end >  C write port scanning multithreaded program
C write port scanning multithreaded program

Time:10-13

Everyone a great god, and I ask, I wrote a port scanner, but is only scans to an open port, classmate told me to write is single-threaded, that how to implement multithreaded? Can be changed in my code?
#include
#include
#include //add the header file
# include
# include

//# pragma comment (lib, "ws2_32. Lib")

# pragma comment (lib, "ws2_32. Lib")


# define NETWORK_ERROR - 1
# define NETWORK_OK 0
# define PORT_MIN 1
# define PORT_MAX 65535

HANDLE hThread;
DWORD hID;

Char hostname [30].

Int starting_port=0;
Int ending_port=0;
Int nopen=0;

DWORD portscan ();

Int main ()
{
int ret;

WSADATA dat.
//the WSADATA structure is used to store call AfxSocketInit global function return Windows Sockets initialization information,
DWORD version;

Version=MAKEWORD (2, 2);
//MAKEWORD (2, 2) use WINSOCK2 version. WsaData used to store system back to the information about the WINSOCK.

Ret=WSAStartup (version, & amp; Dat);
//this function in the application initialization winsockDLL format: int PASCAL FAR WSAStartup (WORD wVersionRequested, LPWSADATA LPWSADATA);

If (ret!=0)
{
Printf (" Error initializing Winsock. \ n ");
WSACleanup ();
Return NETWORK_ERROR;
}
If (ret==0)
{

Printf (" Enter the hostname: ");
The scanf (" % s ", the hostname);

Printf (" Enter starting port: ");
The scanf (" % d ", & amp; Starting_port);

If (starting_port & lt; PORT_MIN)
{
Printf (" Invalid port number. \ n ");
WSACleanup ();
Return NETWORK_ERROR;
}

Printf (" Enter ending port: ");
The scanf (" % d ", & amp; Ending_port);

If (ending_port & gt; PORT_MAX)
{
Printf (" Invalid port number. \ n ");
WSACleanup ();
Return NETWORK_ERROR;
}

Printf (" \ nScanning [% s]... \ n ", the hostname);

HThread=CreateThread (0, 0, (LPTHREAD_START_ROUTINE) portscan, 0, 0, & amp; HID);
//Thread1=CreateThread (0, the default security level 0, the default stack size (2 m) ThreadProc, thread entry function 0, parameter is not zero, creation time, & amp; Thread1ID thread ID);

If (hThread==0)
{
Printf (" Failed to create thread. \ n ");
WSACleanup ();
Return NETWORK_ERROR;
}
Sleep (1);
}
WSACleanup ();
//WSAStartup should be used with WSACleanup in pairs, the WSAStartup function is to initialize Winsock DLL,
//WSACleanup is to remove and Socket library binding and release the Socket library occupies system resources,

Return NETWORK_OK;
}

DWORD portscan ()
{
Int I, nret;

The SOCKET thesocket;
LPHOSTENT hostent;

Thesocket=socket (AF_INET SOCK_STREAM, IPPROTO_TCP);//the first variable as the protocol suite, the second variable for the socket type, the third variable is the communication protocol used
(hostname hostent=gethostbyname ());//gethostbyname () returns corresponding to a given host name contains the host name and address of a pointer to a hostent structure information,

For (I=starting_port; I & lt; Ending_port + 1; + + I)
{

SOCKADDR_IN hostinfo;

Hostinfo. Sin_family=AF_INET;
Hostinfo. Sin_addr=* ((LPIN_ADDR) * hostent - & gt; H_addr_list);//sin. Addr store IP address
Hostinfo. Sin_port=htons (I);//sin_port storage port


//htons is the integer variables shift from host byte order to network byte order, is an integer in the address space is stored into: low byte is stored in the memory address,

Nret=connect (thesocket, (LPSOCKADDR) & amp; Hostinfo, sizeof (hostinfo));

//a: socket descriptor
//parameter 2: a pointer to the data organization sockaddr, including the destination port and IP address
//parameters 3: the length of the second sockaddr, can use sizeof (struct sockaddr) gain
//the return value of 0 represents success
If (nret==0)
{

Printf (" \ n \ n \ t % d ", I);
+ + nopen;
}
}

Printf (" \ nScan complete. \ n \ n ");
Printf (" the Number of ports the opened=% d \ n ", nopen);

Closesocket (thesocket);

return 0;

}





Some comments write more long, in order to let the students read, and also the great god, please advise, thanks!




CodePudding user response:

Awesome ((^ ^)
  • Related