Created
March 5, 2026 07:55
-
-
Save Marie-zaj/8ff2b037f1e18f5d0b717a18136c6ab9 to your computer and use it in GitHub Desktop.
ClServRiadok
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 <ws2tcpip.h> | |
| #include <string> | |
| using namespace std; | |
| #pragma comment(lib, "Ws2_32.lib") | |
| #define DEFAULT_PORT "27015" | |
| #define DEFAULT_BUFLEN 512 | |
| #define SERVER_IP "127.0.0.1" | |
| class TcpClient { | |
| SOCKET connectSocket = INVALID_SOCKET; | |
| bool initializeWinsock() { | |
| WSADATA wsaData; | |
| return WSAStartup(MAKEWORD(2, 2), &wsaData) == 0; | |
| } | |
| bool connectToServer() { | |
| addrinfo hints{}; | |
| hints.ai_family = AF_INET; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| addrinfo* result = nullptr; | |
| if (getaddrinfo(SERVER_IP, DEFAULT_PORT, &hints, &result) != 0) | |
| return false; | |
| connectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
| if (connectSocket == INVALID_SOCKET) | |
| return false; | |
| if (connect(connectSocket, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR) | |
| return false; | |
| freeaddrinfo(result); | |
| return true; | |
| } | |
| void communicate() { | |
| string message; | |
| char buffer[DEFAULT_BUFLEN]; | |
| while (true) { | |
| cout << "Введіть рядок (exit для виходу): "; | |
| getline(cin, message); | |
| if (message == "exit") | |
| break; | |
| send(connectSocket, message.c_str(), (int)message.length(), 0); | |
| int bytesReceived = recv(connectSocket, buffer, DEFAULT_BUFLEN, 0); | |
| if (bytesReceived > 0) { | |
| buffer[bytesReceived] = '\0'; | |
| cout << "Відповідь сервера: " << buffer << endl; | |
| } | |
| else { | |
| cout << "Помилка отримання відповіді.\n"; | |
| break; | |
| } | |
| } | |
| } | |
| public: | |
| bool start() { | |
| if (!initializeWinsock()) return false; | |
| if (!connectToServer()) return false; | |
| communicate(); | |
| closesocket(connectSocket); | |
| WSACleanup(); | |
| return true; | |
| } | |
| }; | |
| int main() { | |
| setlocale(0, ""); | |
| SetConsoleCP(1251); | |
| SetConsoleOutputCP(1251); | |
| system("title CLIENT"); | |
| TcpClient client; | |
| if (!client.start()) { | |
| cout << "Помилка запуску клієнта\n"; | |
| return 1; | |
| } | |
| return 0; | |
| } |
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 <ws2tcpip.h> | |
| #include <string> | |
| using namespace std; | |
| #pragma comment(lib, "Ws2_32.lib") | |
| #define DEFAULT_PORT "27015" | |
| #define DEFAULT_BUFLEN 512 | |
| class TcpServer { | |
| SOCKET listenSocket = INVALID_SOCKET; | |
| SOCKET clientSocket = INVALID_SOCKET; | |
| bool initializeWinsock() { | |
| WSADATA wsaData; | |
| return WSAStartup(MAKEWORD(2, 2), &wsaData) == 0; | |
| } | |
| bool createSocket() { | |
| addrinfo hints{}; | |
| hints.ai_family = AF_INET; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| hints.ai_flags = AI_PASSIVE; | |
| addrinfo* result = nullptr; | |
| if (getaddrinfo(NULL, DEFAULT_PORT, &hints, &result) != 0) | |
| return false; | |
| listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
| if (listenSocket == INVALID_SOCKET) | |
| return false; | |
| if (bind(listenSocket, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR) | |
| return false; | |
| freeaddrinfo(result); | |
| if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) | |
| return false; | |
| cout << "Сервер очікує підключення...\n"; | |
| clientSocket = accept(listenSocket, NULL, NULL); | |
| if (clientSocket == INVALID_SOCKET) | |
| return false; | |
| cout << "Клієнт підключився!\n"; | |
| return true; | |
| } | |
| string reverseString(const string& str) { | |
| string reversed = str; | |
| reverse(reversed.begin(), reversed.end()); | |
| return reversed; | |
| } | |
| void processClient() { | |
| char buffer[DEFAULT_BUFLEN]; | |
| while (true) { | |
| int bytesReceived = recv(clientSocket, buffer, DEFAULT_BUFLEN, 0); | |
| if (bytesReceived > 0) { | |
| buffer[bytesReceived] = '\0'; | |
| string received(buffer); | |
| cout << "Отримано: " << received << endl; | |
| string reversed = reverseString(received); | |
| send(clientSocket, reversed.c_str(), (int)reversed.length(), 0); | |
| } | |
| else if (bytesReceived == 0) { | |
| cout << "Клієнт відключився.\n"; | |
| break; | |
| } | |
| else { | |
| cout << "Помилка прийому.\n"; | |
| break; | |
| } | |
| } | |
| } | |
| public: | |
| bool start() { | |
| if (!initializeWinsock()) return false; | |
| if (!createSocket()) return false; | |
| processClient(); | |
| closesocket(clientSocket); | |
| closesocket(listenSocket); | |
| WSACleanup(); | |
| return true; | |
| } | |
| }; | |
| int main() { | |
| setlocale(0, ""); | |
| SetConsoleCP(1251); | |
| SetConsoleOutputCP(1251); | |
| system("title SERVER"); | |
| TcpServer server; | |
| if (!server.start()) { | |
| cout << "Помилка запуску сервера\n"; | |
| return 1; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment