Created
March 20, 2026 10:25
-
-
Save Vinnik67/64ed78a0f4c5cf0f9f59dca76b3d07c5 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 _WINSOCK_DEPRECATED_NO_WARNINGS | |
| #pragma execution_character_set("utf-8") | |
| #include <ws2tcpip.h> | |
| #include <iostream> | |
| #include <string> | |
| #include <windows.h> | |
| #include <chrono> | |
| #include <vector> | |
| using namespace std; | |
| #pragma comment (lib, "Ws2_32.lib") | |
| #define DEFAULT_BUFLEN 4096 | |
| #define SERVER_IP "127.0.0.1" | |
| #define DEFAULT_PORT "8888" | |
| SOCKET client_socket; | |
| wstring login; | |
| wstring password; | |
| // ПРАКТИКА4: антифлуд/антимат | |
| vector<wstring> badWords = { L"С++", L"мат", L"погане" }; | |
| bool containsBadWord(const wstring& msg) { | |
| for (auto& w : badWords) { | |
| if (msg.find(w) != wstring::npos) return true; | |
| } | |
| return false; | |
| } | |
| DWORD WINAPI Sender(void* param) { | |
| auto lastSent = chrono::steady_clock::now() - chrono::seconds(2); | |
| while (true) { | |
| wstring message; | |
| getline(wcin, message); | |
| auto now = chrono::steady_clock::now(); | |
| if (chrono::duration_cast<chrono::seconds>(now - lastSent).count() < 1) { | |
| wcout << L"Антифлуд: зачекайте 1 секунду\n"; | |
| continue; | |
| } | |
| if (containsBadWord(message)) { | |
| wcout << L"Антимат: повідомлення заблоковано\n"; | |
| continue; | |
| } | |
| wstring formatted = login + L": " + message; | |
| string utf8(formatted.begin(), formatted.end()); | |
| send(client_socket, utf8.c_str(), (int)utf8.size(), 0); | |
| lastSent = now; | |
| } | |
| } | |
| DWORD WINAPI Receiver(void* param) { | |
| char response[DEFAULT_BUFLEN]; | |
| while (true) { | |
| int result = recv(client_socket, response, DEFAULT_BUFLEN - 1, 0); | |
| if (result <= 0) break; | |
| response[result] = '\0'; | |
| string msg(response); | |
| // ДЗ: показувати кількість клієнтів у заголовку | |
| if (msg.find("COUNT:") == 0) { | |
| int count = stoi(msg.substr(6)); | |
| wstring title = L"В чаті людей: " + to_wstring(count); | |
| SetConsoleTitleW(title.c_str()); | |
| } | |
| else { | |
| wcout << L"Відповідь від сервера: " << msg.c_str() << endl; | |
| } | |
| } | |
| return 0; | |
| } | |
| int main() { | |
| system("title Клієнт"); | |
| wcout << L"Введіть логін: "; | |
| getline(wcin, login); | |
| wcout << L"Введіть пароль: "; | |
| getline(wcin, password); | |
| WSADATA wsaData; | |
| int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); | |
| if (iResult != 0) { | |
| wcout << L"WSAStartup error: " << iResult << endl; | |
| return 1; | |
| } | |
| addrinfo hints{}, * result = nullptr; | |
| ZeroMemory(&hints, sizeof(hints)); | |
| hints.ai_family = AF_UNSPEC; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| iResult = getaddrinfo(SERVER_IP, DEFAULT_PORT, &hints, &result); | |
| if (iResult != 0) { | |
| wcout << L"getaddrinfo error: " << iResult << endl; | |
| WSACleanup(); | |
| return 2; | |
| } | |
| client_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
| if (connect(client_socket, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR) { | |
| wcout << L"Connect error\n"; | |
| WSACleanup(); | |
| return 3; | |
| } | |
| freeaddrinfo(result); | |
| // відправляємо логін+пароль серверу | |
| wstring credentials = login + L";" + password; | |
| string utf8(credentials.begin(), credentials.end()); | |
| send(client_socket, utf8.c_str(), (int)utf8.size(), 0); | |
| system("cls"); | |
| wcout << L"Авторизація успішна! Тепер можна писати повідомлення.\n"; | |
| CreateThread(0, 0, Sender, 0, 0, 0); | |
| CreateThread(0, 0, Receiver, 0, 0, 0); | |
| Sleep(INFINITE); | |
| } |
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 _WINSOCK_DEPRECATED_NO_WARNINGS | |
| #pragma execution_character_set("utf-8") | |
| #include <ws2tcpip.h> | |
| #include <iostream> | |
| #include <string> | |
| #include <windows.h> | |
| #include <chrono> | |
| #include <vector> | |
| using namespace std; | |
| #pragma comment (lib, "Ws2_32.lib") | |
| #define DEFAULT_BUFLEN 4096 | |
| #define SERVER_IP "127.0.0.1" | |
| #define DEFAULT_PORT "8888" | |
| SOCKET client_socket; | |
| wstring login; | |
| wstring password; | |
| // ПРАКТИКА4: антифлуд/антимат | |
| vector<wstring> badWords = { L"С++", L"мат", L"погане" }; | |
| bool containsBadWord(const wstring& msg) { | |
| for (auto& w : badWords) { | |
| if (msg.find(w) != wstring::npos) return true; | |
| } | |
| return false; | |
| } | |
| DWORD WINAPI Sender(void* param) { | |
| auto lastSent = chrono::steady_clock::now() - chrono::seconds(2); | |
| while (true) { | |
| wstring message; | |
| getline(wcin, message); | |
| auto now = chrono::steady_clock::now(); | |
| if (chrono::duration_cast<chrono::seconds>(now - lastSent).count() < 1) { | |
| wcout << L"Антифлуд: зачекайте 1 секунду\n"; | |
| continue; | |
| } | |
| if (containsBadWord(message)) { | |
| wcout << L"Антимат: повідомлення заблоковано\n"; | |
| continue; | |
| } | |
| wstring formatted = login + L": " + message; | |
| string utf8(formatted.begin(), formatted.end()); | |
| send(client_socket, utf8.c_str(), (int)utf8.size(), 0); | |
| lastSent = now; | |
| } | |
| } | |
| DWORD WINAPI Receiver(void* param) { | |
| char response[DEFAULT_BUFLEN]; | |
| while (true) { | |
| int result = recv(client_socket, response, DEFAULT_BUFLEN - 1, 0); | |
| if (result <= 0) break; | |
| response[result] = '\0'; | |
| string msg(response); | |
| // ДЗ: показувати кількість клієнтів у заголовку | |
| if (msg.find("COUNT:") == 0) { | |
| int count = stoi(msg.substr(6)); | |
| wstring title = L"В чаті людей: " + to_wstring(count); | |
| SetConsoleTitleW(title.c_str()); | |
| } | |
| else { | |
| wcout << L"Відповідь від сервера: " << msg.c_str() << endl; | |
| } | |
| } | |
| return 0; | |
| } | |
| int main() { | |
| system("title Клієнт"); | |
| wcout << L"Введіть логін: "; | |
| getline(wcin, login); | |
| wcout << L"Введіть пароль: "; | |
| getline(wcin, password); | |
| WSADATA wsaData; | |
| int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); | |
| if (iResult != 0) { | |
| wcout << L"WSAStartup error: " << iResult << endl; | |
| return 1; | |
| } | |
| addrinfo hints{}, * result = nullptr; | |
| ZeroMemory(&hints, sizeof(hints)); | |
| hints.ai_family = AF_UNSPEC; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| iResult = getaddrinfo(SERVER_IP, DEFAULT_PORT, &hints, &result); | |
| if (iResult != 0) { | |
| wcout << L"getaddrinfo error: " << iResult << endl; | |
| WSACleanup(); | |
| return 2; | |
| } | |
| client_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
| if (connect(client_socket, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR) { | |
| wcout << L"Connect error\n"; | |
| WSACleanup(); | |
| return 3; | |
| } | |
| freeaddrinfo(result); | |
| // відправляємо логін+пароль серверу | |
| wstring credentials = login + L";" + password; | |
| string utf8(credentials.begin(), credentials.end()); | |
| send(client_socket, utf8.c_str(), (int)utf8.size(), 0); | |
| system("cls"); | |
| wcout << L"Авторизація успішна! Тепер можна писати повідомлення.\n"; | |
| CreateThread(0, 0, Sender, 0, 0, 0); | |
| CreateThread(0, 0, Receiver, 0, 0, 0); | |
| Sleep(INFINITE); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment