Created
March 5, 2026 07:34
-
-
Save XoLinA/80a85c2a82f95a646fdf07d24be159de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define WIN32_LEAN_AND_MEAN | |
| #include <iostream> | |
| #include <windows.h> | |
| #include <ws2tcpip.h> | |
| #include <thread> | |
| #include <string> | |
| #include <algorithm> | |
| using namespace std; | |
| #pragma comment (lib, "Ws2_32.lib") | |
| #define DEFAULT_BUFLEN 512 | |
| #define DEFAULT_PORT "27015" | |
| #define PAUSE 1 | |
| class TcpServer { | |
| SOCKET listenSocket = INVALID_SOCKET; | |
| SOCKET clientSocket = INVALID_SOCKET; | |
| addrinfo* result = nullptr; | |
| public: | |
| ~TcpServer() { | |
| if (clientSocket != INVALID_SOCKET) closesocket(clientSocket); | |
| if (listenSocket != INVALID_SOCKET) closesocket(listenSocket); | |
| if (result) freeaddrinfo(result); | |
| WSACleanup(); | |
| } | |
| bool initialize() { | |
| WSADATA wsaData; | |
| int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); | |
| if (iResult != 0) { | |
| cerr << "WSAStartup впала: " << iResult << "\n"; | |
| return false; | |
| } | |
| addrinfo hints{}; | |
| hints.ai_family = AF_INET; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| hints.ai_flags = AI_PASSIVE; | |
| iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); | |
| if (iResult != 0) { | |
| cerr << "getaddrinfo впала: " << iResult << "\n"; | |
| return false; | |
| } | |
| listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
| if (listenSocket == INVALID_SOCKET) { | |
| cerr << "Не вдалося створити сокет: " << WSAGetLastError() << "\n"; | |
| return false; | |
| } | |
| iResult = bind(listenSocket, result->ai_addr, (int)result->ai_addrlen); | |
| if (iResult == SOCKET_ERROR) { | |
| cerr << "Bind впала: " << WSAGetLastError() << "\n"; | |
| return false; | |
| } | |
| iResult = listen(listenSocket, SOMAXCONN); | |
| if (iResult == SOCKET_ERROR) { | |
| cerr << "Listen впала: " << WSAGetLastError() << "\n"; | |
| return false; | |
| } | |
| cout << "Очікування клієнта...\n"; | |
| clientSocket = accept(listenSocket, NULL, NULL); | |
| if (clientSocket == INVALID_SOCKET) { | |
| cerr << "Accept впала: " << WSAGetLastError() << "\n"; | |
| return false; | |
| } | |
| cout << "Клієнт підключився!\n"; | |
| return true; | |
| } | |
| string reverseString(string text) { | |
| reverse(text.begin(), text.end()); | |
| return text; | |
| } | |
| void receiver() { | |
| char recvbuf[DEFAULT_BUFLEN]; | |
| while (true) { | |
| int iResult = recv(clientSocket, recvbuf, DEFAULT_BUFLEN, 0); | |
| if (iResult > 0) { | |
| recvbuf[iResult] = '\0'; | |
| string message = recvbuf; | |
| cout << "\nКлієнт пише: " << message << "\n"; | |
| string reversed = reverseString(message); | |
| send(clientSocket, reversed.c_str(), reversed.length(), 0); | |
| } | |
| else if (iResult == 0) { | |
| cout << "Клієнт відключився\n"; | |
| break; | |
| } | |
| else { | |
| cerr << "recv помилка: " << WSAGetLastError() << "\n"; | |
| break; | |
| } | |
| Sleep(PAUSE); | |
| } | |
| } | |
| void run() { | |
| thread receiveThread(&TcpServer::receiver, this); | |
| receiveThread.join(); | |
| } | |
| }; | |
| int main() { | |
| setlocale(0, ""); | |
| SetConsoleCP(1251); | |
| SetConsoleOutputCP(1251); | |
| system("title СЕРВЕРНА СТОРОНА"); | |
| auto server = make_unique<TcpServer>(); | |
| if (!server->initialize()) { | |
| cerr << "Не вдалося ініціалізувати сервер.\n"; | |
| return 1; | |
| } | |
| server->run(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment