我发明的东西最初是网络浏览器,但现在是开放端口的扫描器。我想知道您认为这是一个现实的或不可靠的端口测试工具。我在我的电脑上找到了一些打开的端口,并且能够通过它们发送数据。
#include <windows.h>
#include <winsock2.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define SCK_VERSION2 0x0202
#define DEFAULT_BUFLEN 2000
#define DEFAULT_PORT 27015
namespace Globals{
u_short PORT;
int i = 135;
}
using namespace Globals;
int sck() {
//----------------------
// Declare and initialize variables.
WSADATA wsaData;
int iResult;
SOCKET ConnectSocket = INVALID_SOCKET;
struct sockaddr_in clientService;
char name[500] = "";
char ipADDRESS[500] = "";
char sPORT[500] = "";
sockaddr_in sName;
int sNameSize = sizeof(sName);
char const* sendbuf = "GET HTTP/1.1\r\n"
"Host: 97-80-226-196.dhcp.leds.al.charter.com\r\n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Accept-Language: en-us,en;q=0.5\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
"Keep-Alive: 300\r\n"
"Connection: keep-alive\r\n"
"Pragma: no-cache\r\n"
"DNT: 1"
"Cache-Control: no-cache\r\n\r\n";
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN; //23.214.132.132 GoDaddy.com
int WSAERROR = WSAGetLastError();
//----------------------
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
//----------------------
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %i\n", WSAGetLastError() );
WSACleanup();
return 1;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
//u_short PORT;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(//The ip address);
clientService.sin_port = htons(PORT);
//----------------------
// Connect to server.
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if ( iResult == SOCKET_ERROR) {
closesocket (ConnectSocket);
//printf("Unable to connect to server: %i\n", WSAGetLastError());
int Error = WSAGetLastError();
cout << i <<": Unable to connect to server: " << Error << endl;
WSACleanup();
return 1;
}
//----------------------
//Get local host name
iResult = gethostname(name, sizeof(name));
if (iResult == NO_ERROR) {
printf("Host Name: %s\n", name);
}
else if (iResult == SOCKET_ERROR) {
printf("Could not resolve host name: %i", WSAGetLastError());
}
//------------------------
//Get peer name
iResult = getpeername(ConnectSocket, (struct sockaddr*)&sName, &sNameSize);
if (iResult == NO_ERROR)
printf("Peer Name: %s\n", inet_ntoa(sName.sin_addr));
else if (iResult == SOCKET_ERROR)
printf("Could not get peer name: %i\n", WSAGetLastError());
//-------------------------
// Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
else
printf("Bytes Sent: %i\n", iResult);
//-----------------------------
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 ) {
printf("Bytes received: %d\n", iResult); //printf("Bytes received: %d\n", iResult);
printf("From server: %s\n", recvbuf);
}
else if ( iResult == 0 )
printf("Connection closed\n");
else if (WSAERROR == WSAETIMEDOUT)
printf("recv failed: WSAETIMEDOUT\n");
} while( iResult > 0 );
// cleanup
closesocket(ConnectSocket);
WSACleanup();
system("PAUSE");
return 0;
}
int main() {
for (i;i>=50;i++) {
PORT = i;
sck();
}
}发布于 2015-08-18 05:16:06
服务器可以无限期地打开套接字。
// Receive until the peer closes the connection所以这会挂起你的应用程序。尝试连接到端口23,看看会发生什么。即使是网络服务器也可以延长端口的开放时间,用于链接(错误的字)请求,并且不需要重新连接的成本。
PORT = i;
sck();全局变量。什么。不要这样做。函数应该是独立的(任何依赖于外部状态的东西都有可能出错,因为其他人会在最不经意的时候改变这种状态。将端口作为参数传递。
sck(i);iResult = WSAStartup(MAKEWORD(2,2), &wsaData);这只需要在应用程序中完成一次。所以您可以将它移出sck()函数。
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if ( iResult == SOCKET_ERROR) {
closesocket (ConnectSocket); // Its not open at this point.iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );请参见人派:如果send()返回-1为错误,则需要检查errno以检查实际错误。并不是所有的错误都必须是终端机。EINTR不是一个问题。
https://codereview.stackexchange.com/questions/101250
复制相似问题