Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Курсовая работа.docx
Скачиваний:
0
Добавлен:
02.08.2026
Размер:
691 Кб
Скачать

Приложение 6 Текст программы «server2.Cpp»

#include <iostream>

#include <winsock2.h>

#include <ws2tcpip.h>

#include <string>

#include <thread>

#include <windows.h>

#include <ctime>

#pragma comment(lib, "Ws2_32.lib")

void getSystemInfo(std::string& response, const std::string& unit) {

MEMORYSTATUSEX memInfo;

memInfo.dwLength = sizeof(MEMORYSTATUSEX);

GlobalMemoryStatusEx(&memInfo);

DWORDLONG totalMemory = memInfo.ullTotalPhys; // Total physical memory in bytes

DWORDLONG freeMemory = memInfo.ullAvailPhys; // Free physical memory in bytes

// Convert to the selected unit

if (unit == "mb") {

totalMemory /= (1024 * 1024);

freeMemory /= (1024 * 1024);

response += "Total physical memory: " + std::to_string(totalMemory) + " MB\n";

response += "Free physical memory: " + std::to_string(freeMemory) + " MB\n";

}

else if (unit == "gb") {

totalMemory /= (1024 * 1024 * 1024);

freeMemory /= (1024 * 1024 * 1024);

response += "Total physical memory: " + std::to_string(totalMemory) + " GB\n";

response += "Free physical memory: " + std::to_string(freeMemory) + " GB\n";

}

else {

response += "Total physical memory: " + std::to_string(totalMemory) + " bytes\n";

response += "Free physical memory: " + std::to_string(freeMemory) + " bytes\n";

}

}

std::string getCurrentTime() {

time_t now = time(0);

char buffer[80];

struct tm tstruct;

localtime_s(&tstruct, &now); // Use localtime_s for thread safety

strftime(buffer, sizeof(buffer), "%Y-%m-%d %X", &tstruct);

return std::string(buffer);

}

void handleClient(SOCKET clientSocket) {

char unitBuffer[10];

int bytesReceived = recv(clientSocket, unitBuffer, sizeof(unitBuffer) - 1, 0);

if (bytesReceived > 0) {

unitBuffer[bytesReceived] = '\0'; // Null-terminate the string

std::string unit(unitBuffer);

std::cout << "Received unit: " << unit << std::endl; // Debug message

std::string response;

getSystemInfo(response, unit);

response += "Current time: " + getCurrentTime() + "\n";

send(clientSocket, response.c_str(), response.size(), 0);

}

else {

std::cerr << "Error receiving data from client" << std::endl;

}

closesocket(clientSocket);

std::cout << "Connection with client closed." << std::endl;

}

int main() {

WSADATA wsaData;

WSAStartup(MAKEWORD(2, 2), &wsaData);

SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);

if (serverSocket == INVALID_SOCKET) {

std::cerr << "Error creating socket" << std::endl;

return 1;

}

struct sockaddr_in serverAddress;

serverAddress.sin_family = AF_INET;

serverAddress.sin_addr.s_addr = INADDR_ANY;

serverAddress.sin_port = htons(8082);

if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {

std::cerr << "Error binding socket" << std::endl;

closesocket(serverSocket);

return 1;

}

listen(serverSocket, 5);

std::cout << "Server is running and waiting for connections on port 8082..." << std::endl;

while (true) {

SOCKET clientSocket = accept(serverSocket, nullptr, nullptr);

if (clientSocket == INVALID_SOCKET) {

std::cerr << "Error accepting connection" << std::endl;

continue;

}

std::cout << "Client connected!" << std::endl;

// Create a new thread to handle the client

std::thread clientThread(handleClient, clientSocket);

clientThread.detach(); // Detach the thread to run independently

}

closesocket(serverSocket);

WSACleanup();

return 0;

}