C++ TCP端口扫描
差不多就闭毁是这样的吧
#include stdio.h
#include winsock.h
#pragma comment(lib,"wsock32.lib")
int main(int argc, char **argv)
{
SOCKET sd_client;
u_short iPortStart, iPortEnd, port;
struct sockaddr_in addr_srv;
char *pszHost;
WSADATA wsaData;
WORD wVersionRequested;
int err;
switch(argc)
{
case 2:
iPortStart = 0;
iPortEnd = 65535;
pszHost =argv[1];
break;
case 3:
iPortStart = iPortEnd = atoi(argv[2]);
pszHost =argv[1];
break;
case 4:
iPortStart = atoi(argv[2]);
iPortEnd = atoi(argv[3]);
pszHost =argv[1];
break;
default:
printf("正确的命令行参数:\n");
printf("游启[IP] 扫描所神态如有端口\n");
printf("[IP] [端口]:扫描单个端口\n");
printf("[IP] [端口1] [端口2]:扫描端口1到端口2\n");
return 1;
}
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, wsaData );
if ( err != 0 )
{
printf("Error %d: Winsock not available\n", err);
return 1;
}
for(port=iPortStart; port=iPortEnd; port++)
{
sd_client = socket(PF_INET, SOCK_STREAM, 0);
if (sd_client == INVALID_SOCKET)
{
printf("no more socket resources\n");
return 1;
}
addr_srv.sin_family = PF_INET;
addr_srv.sin_addr.s_addr=inet_addr(pszHost);
addr_srv.sin_port = htons(port);
err = connect(sd_client, (struct sockaddr *) addr_srv, sizeof(addr_srv));
if (err == INVALID_SOCKET)
{
printf("不能连接此端口:%d\n", port);
closesocket(sd_client);
continue;
}
printf("扫描此端口成功:%d\n", port);
closesocket(sd_client);
}
WSACleanup();
return 0;
}
0条大神的评论