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;
}
#pragma comment (lib,"Ws2_32.lib")
#include <winsock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
#include <iostream>
#include <string>
int main() {
WSADATA wsa;
std::string domain;
int status;
struct addrinfo hints = { 0 }, * res, * p;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
// ошибки выводятся в ANSI кодировке, а консоль по умолчанию в OEM
SetConsoleCP(GetACP());
SetConsoleOutputCP(GetACP());
// На Windows необходима инициализация сетевой библиотеки
if (status = WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
std::cout << "Error: of getaddrinfo: " << get_error_message(status) << std::endl;
// принято возвращать причину ошибки, а не только сам факт
return status;
}
std::cout << ("Enter domain: ");
// даже доменные имена могут иметь длину 256 символов
// пользователь же может вообще ввести сколько ему угодно
std::getline(std::cin, domain);
if (status = getaddrinfo(domain.c_str(), "80", &hints, &res) != 0) {
std::cout << "Error: of getaddrinfo: " << get_error_message(status) << std::endl;
return status;
};
char ipstr[INET6_ADDRSTRLEN];
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: " << ipstr << std::endl;
};
freeaddrinfo(res);
system("pause");
// деструктор сетевой библиотеки
WSACleanup();
return 0;
}
#include <filesystem>
int main()
{
std::string file_path = "CP.mp4";
std::string path = "xxx/CP.mp4";
std::filesystem::copy_file(file_path, path);
return 0;
}