mercy_smile
@mercy_smile

Какая ошибка в коде?

WSADATA wsa;

	std::string domin;
	int status;

	struct addrinfo hints = { 0 }, * res, * p;
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	
	SetConsoleCP(GetACP());
	SetConsoleOutputCP(GetACP());

	if (status = WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
		std::cout << "Error: of WSAStatus: " << gai_strerror(status) << std::endl;
		return status;
	}

	std::cout << "Enter domin: " << std::endl;
	std::getline(std::cin, domin);

	if (status = getaddrinfo(domin.c_str(), "80", &hints, &res) != 0) {
		std::cout << ("Error: of getaddrinfo", gai_strerror(status));
		return status;
	};

	std::string buffhost = "HTTP/1.0\r\nHost: ";
	std::string content = "Content-Type: application/x-www-form-urlencoded\r\nContent-Length:\r\n\r\n";

	std::string fine = {0};
	char sendbuf[1024];
	
	if (1) {
		buffhost = buffhost + domin + "\r\n";
		fine = buffhost + content;
		memcpy(sendbuf, fine.c_str(), 1024);
		std::cout << fine << std::endl;
	};

	char ipstr[1024]; 
	int s;

	char bufrecv[2048];

	for (p = res; p != NULL; p = p->ai_next) {
		const char* ipver;
		void* addr;
		if (p->ai_family == AF_INET) {
			struct sockaddr_in* ipv4 = (struct sockaddr_in*)p->ai_addr;
			addr = &(ipv4->sin_addr);
			ipver = "IPv4: ";
		}
		else {
			struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)p->ai_addr;
			addr = &(ipv6->sin6_addr);
			ipver = "IPv6: ";
		}

		inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr));
		std::cout << "addres: " << ipver << ipstr << std::endl;

		if (((s = socket(p->ai_family, p->ai_socktype, p->ai_protocol))) == INVALID_SOCKET) {
			std::cout << "error of socket: " << WSAGetLastError() << std::endl;
			continue;
		}

		if ((connect(s, p->ai_addr, p->ai_addrlen)) == INVALID_SOCKET) {
			std::cout << "Error of connect: " << WSAGetLastError() << std::endl;
			continue;
		}

		send(s, sendbuf, sizeof(sendbuf), 0);
		
		if (recv(s, bufrecv, sizeof(bufrecv), 0) == NULL) {
			std::cout << " recv " << WSAGetLastError() << std::endl;
		}
	};

	freeaddrinfo(res);
	WSACleanup();
	system("pause");
	return 0;
}

я отправляю http запрос на сервер но в recv ничего не приходит.
  • Вопрос задан
  • 106 просмотров
Решения вопроса 2
@galaxy
А что вы шлете на сервер, не пробовали задуматься?
Если проследить:
HTTP/1.0
Host: domain.com
Content-Type: application/x-www-form-urlencoded
Content-Length:

Первая строчка не смущает? А Content-Length?

Ну и send(s, sendbuf, sizeof(sendbuf), 0); - почему sizeof? 1024 байта с кучей 0x00 хотите послать несчастному серверу?
Ответ написан
@none7
int main() {
    const char* endl = "\r\n";
    WSADATA wsa;

    std::string domin;
    int status;
    std::string write_data;

    struct addrinfo hints = { 0 }, * res, * p;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    SetConsoleCP(GetACP());
    SetConsoleOutputCP(GetACP());

    if (status = WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
        std::cout << "Error: of WSAStatus: " << gai_strerror(status) << std::endl;
        return status;
    }

    std::cout << "Enter domin: " << std::endl;
    std::getline(std::cin, domin);

    if (status = getaddrinfo(domin.c_str(), "80", &hints, &res) != 0) {
        std::cout << ("Error: of getaddrinfo", gai_strerror(status));
        return status;
    };

    {
        std::ostringstream ws;

        ws << "GET / HTTP/1.1" << endl;
        ws << "Host: " << domin << endl;
        ws << "Accept: text/html, text/*" << endl;
        ws << "Connection: close" << endl;
        ws << endl;

        write_data = ws.str();
    }

    std::cout << write_data;

    char ipstr[1024];
    int s = INVALID_SOCKET;

    char bufrecv[2048];

    for (p = res; p != NULL; p = p->ai_next) {
        const char* ipver;
        void* addr;
        if (p->ai_family == AF_INET) {
            struct sockaddr_in* ipv4 = (struct sockaddr_in*)p->ai_addr;
            addr = &(ipv4->sin_addr);
            ipver = "IPv4: ";
        }
        else {
            struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)p->ai_addr;
            addr = &(ipv6->sin6_addr);
            ipver = "IPv6: ";
        }

        inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr));
        std::cout << "addres: " << ipver << ipstr << std::endl;

        if (s != INVALID_SOCKET) {
            shutdown(s, SD_BOTH);
            closesocket(s);
        }

        if (((s = socket(p->ai_family, p->ai_socktype, p->ai_protocol))) == INVALID_SOCKET) {
            std::cout << "error of socket: " << WSAGetLastError() << std::endl;
            continue;
        }

        if ((connect(s, p->ai_addr, p->ai_addrlen)) == SOCKET_ERROR) {
            std::cout << "Error of connect: " << WSAGetLastError() << std::endl;
            continue;
        }

        bool success = true;
        int writed = 0;
        for (size_t i = 0, data_len = write_data.length(); i < data_len; i += writed) {
            writed = send(s, write_data.c_str() + i, (int)min(data_len - i, INT_MAX), 0);
            if (writed == SOCKET_ERROR) {
                std::cout << "Error of send: " << WSAGetLastError() << std::endl;
                success = false;
                break;
            }
        }
        if (success) {
            int readed;

            while ((readed = recv(s, bufrecv, sizeof(bufrecv), 0)) != SOCKET_ERROR && readed != 0) {
                std::cout << std::string(bufrecv, readed);
            }
            if (readed == SOCKET_ERROR) {
                std::cout << " recv " << WSAGetLastError() << std::endl;
            }
            break;
        }
    }

    if (s != INVALID_SOCKET)
    {
        shutdown(s, SD_BOTH);
        closesocket(s);
    }

    freeaddrinfo(res);
    WSACleanup();
    system("pause");
    return 0;
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы
22 нояб. 2024, в 03:54
1500 руб./за проект
22 нояб. 2024, в 02:56
10000 руб./за проект
22 нояб. 2024, в 00:55
500 руб./за проект