Created
February 27, 2026 21:23
-
-
Save Marie-zaj/aa050285baec5dc5aa0f9d8575a344b3 to your computer and use it in GitHub Desktop.
12.02
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 <windows.h> | |
| #include <iostream> | |
| #include <ws2tcpip.h> | |
| #include <string> | |
| #pragma comment(lib, "Ws2_32.lib") | |
| #define DEFAULT_BUFLEN 512 | |
| #define DEFAULT_PORT "27015" | |
| using namespace std; | |
| class Client | |
| { | |
| private: | |
| WSADATA wsaData{}; | |
| SOCKET connectSocket = INVALID_SOCKET; | |
| public: | |
| bool initialize() | |
| { | |
| if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) | |
| { | |
| cout << "WSAStartup failed\n"; | |
| return false; | |
| } | |
| addrinfo hints{}; | |
| hints.ai_family = AF_INET; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| addrinfo* result = nullptr; | |
| if (getaddrinfo("localhost", DEFAULT_PORT, &hints, &result) != 0) | |
| { | |
| cout << "getaddrinfo failed\n"; | |
| WSACleanup(); | |
| return false; | |
| } | |
| connectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
| if (connectSocket == INVALID_SOCKET) | |
| { | |
| cout << "Socket creation failed\n"; | |
| WSACleanup(); | |
| return false; | |
| } | |
| if (connect(connectSocket, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR) | |
| { | |
| cout << "Connection failed\n"; | |
| closesocket(connectSocket); | |
| WSACleanup(); | |
| return false; | |
| } | |
| freeaddrinfo(result); | |
| cout << "Connected to server!\n"; | |
| return true; | |
| } | |
| void run() | |
| { | |
| string message; | |
| char buffer[DEFAULT_BUFLEN]; | |
| while (true) | |
| { | |
| cout << "Enter message: "; | |
| getline(cin, message); | |
| send(connectSocket, message.c_str(), (int)message.length(), 0); | |
| if (message == "exit") | |
| break; | |
| ZeroMemory(buffer, DEFAULT_BUFLEN); | |
| int bytesReceived = recv(connectSocket, buffer, DEFAULT_BUFLEN, 0); | |
| if (bytesReceived > 0) | |
| { | |
| cout << "Server response: " << buffer << endl; | |
| } | |
| } | |
| } | |
| void cleanup() | |
| { | |
| shutdown(connectSocket, SD_SEND); | |
| closesocket(connectSocket); | |
| WSACleanup(); | |
| cout << "Client stopped.\n"; | |
| } | |
| }; | |
| int main() | |
| { | |
| Client client; | |
| if (!client.initialize()) | |
| return 1; | |
| client.run(); | |
| client.cleanup(); | |
| 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 <windows.h> | |
| #include <iostream> | |
| #include <ws2tcpip.h> | |
| #include <string> | |
| #include <ctime> | |
| #pragma comment(lib, "Ws2_32.lib") | |
| #define DEFAULT_BUFLEN 512 | |
| #define DEFAULT_PORT "27015" | |
| using namespace std; | |
| class Server | |
| { | |
| private: | |
| WSADATA wsaData{}; | |
| SOCKET listenSocket = INVALID_SOCKET; | |
| SOCKET clientSocket = INVALID_SOCKET; | |
| addrinfo* result = nullptr; | |
| public: | |
| bool initialize() | |
| { | |
| if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) | |
| { | |
| cout << "WSAStartup failed\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; | |
| if (getaddrinfo(NULL, DEFAULT_PORT, &hints, &result) != 0) | |
| { | |
| cout << "getaddrinfo failed\n"; | |
| WSACleanup(); | |
| return false; | |
| } | |
| listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
| if (listenSocket == INVALID_SOCKET) | |
| { | |
| cout << "Socket creation failed\n"; | |
| WSACleanup(); | |
| return false; | |
| } | |
| if (bind(listenSocket, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR) | |
| { | |
| cout << "Bind failed\n"; | |
| freeaddrinfo(result); | |
| closesocket(listenSocket); | |
| WSACleanup(); | |
| return false; | |
| } | |
| freeaddrinfo(result); | |
| if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) | |
| { | |
| cout << "Listen failed\n"; | |
| closesocket(listenSocket); | |
| WSACleanup(); | |
| return false; | |
| } | |
| cout << "Server is waiting for connection...\n"; | |
| return true; | |
| } | |
| string getCurrentTime() | |
| { | |
| time_t now = time(0); | |
| tm localTime; | |
| localtime_s(&localTime, &now); | |
| char buffer[6]; | |
| strftime(buffer, sizeof(buffer), "%H:%M", &localTime); | |
| return string(buffer); | |
| } | |
| string processMessage(const string& message) | |
| { | |
| if (message == "how are you") | |
| return "I am great!"; | |
| else if (message == "what time is it") | |
| return getCurrentTime(); | |
| else if (message == "exit") | |
| return "Goodbye!"; | |
| else | |
| return "Unknown command"; | |
| } | |
| void run() | |
| { | |
| clientSocket = accept(listenSocket, NULL, NULL); | |
| if (clientSocket == INVALID_SOCKET) | |
| { | |
| cout << "Accept failed\n"; | |
| return; | |
| } | |
| cout << "Client connected!\n"; | |
| char buffer[DEFAULT_BUFLEN]; | |
| int bytesReceived; | |
| do | |
| { | |
| ZeroMemory(buffer, DEFAULT_BUFLEN); | |
| bytesReceived = recv(clientSocket, buffer, DEFAULT_BUFLEN, 0); | |
| if (bytesReceived > 0) | |
| { | |
| string message(buffer); | |
| cout << "Client says: " << message << endl; | |
| string response = processMessage(message); | |
| send(clientSocket, response.c_str(), (int)response.length(), 0); | |
| if (message == "exit") | |
| break; | |
| } | |
| } while (bytesReceived > 0); | |
| shutdown(clientSocket, SD_SEND); | |
| } | |
| void cleanup() | |
| { | |
| closesocket(clientSocket); | |
| closesocket(listenSocket); | |
| WSACleanup(); | |
| cout << "Server stopped.\n"; | |
| } | |
| }; | |
| int main() | |
| { | |
| Server server; | |
| if (!server.initialize()) | |
| return 1; | |
| server.run(); | |
| server.cleanup(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment