int tryscan int pPort int port pPort getaddrinfosuc port true addrinfo

 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
int tryscan(int *pPort)
{
int port = *pPort;
getaddrinfosuc[port] = true;
addrinfo *pAddr = new addrinfo;
memcpy(pAddr, result, sizeof(addrinfo));
//filling port number
pAddr->ai_addr->sa_data[0] = port/256;
pAddr->ai_addr->sa_data[1] = port % 256;
praddr(pAddr->ai_addr);
char currPort[10];
memset(currPort, 0, sizeof(currPort));
itoa(port, currPort, 10);
//cout << "Scanning address " << "localhost" << ", port " << currPort << '\n';
SOCKET ConnSock = INVALID_SOCKET;
ConnSock = socket(pAddr->ai_family, pAddr->ai_socktype, pAddr->ai_protocol);
if(ConnSock == INVALID_SOCKET)
{
cout << "Error at socket(): " << WSAGetLastError() << '\n';
//system("cls");
return port;
}
int iResult = connect(ConnSock, pAddr->ai_addr, (int)pAddr->ai_addrlen);
if(iResult == SOCKET_ERROR)
{
closesocket(ConnSock);
ConnSock = INVALID_SOCKET;
}
if(ConnSock == INVALID_SOCKET)
{
//cout << "Unable to connect to server " << "localhost" << ", port " << currPort << '\n';
//system("cls");
goto CLEANUP;
}
else
{
//cout << "Successfully connected to server " << "localhost" << ", port " << currPort << '\n';
connsuc[port] = true;
}
iResult = shutdown(ConnSock, SD_SEND);
if (iResult == SOCKET_ERROR)
{
printf("shutdown failed: %d\n", WSAGetLastError());
}
closesocket(ConnSock);
//system("cls");
CLEANUP:
delete pAddr;
return port;
}