Created
May 2, 2026 11:32
-
-
Save NV-Valentine/3c65d37f511c0c7b2e07a4fde6805e20 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 <string> | |
| #pragma comment (lib, "Ws2_32.lib") | |
| #define DEFAULT_BUFLEN 512 | |
| #define DEFAULT_PORT "27015" | |
| class CurrencyClient { | |
| private: | |
| SOCKET ConnectSocket = INVALID_SOCKET; | |
| public: | |
| bool init(const std::string& serverName) { | |
| WSADATA wsaData; | |
| int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); | |
| if (iResult != 0) { | |
| std::cout << "WSAStartup failed: " << iResult << "\n"; | |
| return false; | |
| } | |
| struct addrinfo* result = NULL, hints; | |
| ZeroMemory(&hints, sizeof(hints)); | |
| hints.ai_family = AF_INET; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| iResult = getaddrinfo(serverName.c_str(), DEFAULT_PORT, &hints, &result); | |
| if (iResult != 0) { | |
| std::cout << "getaddrinfo failed: " << iResult << "\n"; | |
| WSACleanup(); | |
| return false; | |
| } | |
| ConnectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
| if (ConnectSocket == INVALID_SOCKET) { | |
| std::cout << "socket failed: " << WSAGetLastError() << "\n"; | |
| freeaddrinfo(result); | |
| WSACleanup(); | |
| return false; | |
| } | |
| iResult = connect(ConnectSocket, result->ai_addr, (int)result->ai_addrlen); | |
| if (iResult == SOCKET_ERROR) { | |
| std::cout << "connect failed: " << WSAGetLastError() << "\n"; | |
| closesocket(ConnectSocket); | |
| ConnectSocket = INVALID_SOCKET; | |
| WSACleanup(); | |
| return false; | |
| } | |
| freeaddrinfo(result); | |
| return true; | |
| } | |
| void run() { | |
| std::string input; | |
| char buffer[DEFAULT_BUFLEN]; | |
| while (true) { | |
| std::cout << "Enter currency pair (e.g. USD EUR) or EXIT: "; | |
| std::getline(std::cin, input); | |
| send(ConnectSocket, input.c_str(), static_cast<int>(input.size()), 0); | |
| if (input == "EXIT") break; | |
| int iResult = recv(ConnectSocket, buffer, DEFAULT_BUFLEN - 1, 0); | |
| if (iResult > 0) { | |
| buffer[iResult] = '\0'; | |
| std::cout << "Rate: " << buffer << std::endl; | |
| } | |
| } | |
| } | |
| ~CurrencyClient() { | |
| closesocket(ConnectSocket); | |
| WSACleanup(); | |
| } | |
| }; | |
| int main() { | |
| CurrencyClient client; | |
| if (client.init("127.0.0.1")) { | |
| client.run(); | |
| } | |
| 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 <windows.h> | |
| #include <ws2tcpip.h> | |
| #include <string> | |
| #pragma comment (lib, "Ws2_32.lib") | |
| #define DEFAULT_BUFLEN 512 | |
| #define DEFAULT_PORT "27015" | |
| class CurrencyClient { | |
| private: | |
| SOCKET ConnectSocket = INVALID_SOCKET; | |
| public: | |
| bool init(const std::string& serverName) { | |
| WSADATA wsaData; | |
| int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); | |
| if (iResult != 0) { | |
| std::cout << "WSAStartup failed: " << iResult << "\n"; | |
| return false; | |
| } | |
| struct addrinfo *result = NULL, hints; | |
| ZeroMemory(&hints, sizeof(hints)); | |
| hints.ai_family = AF_INET; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| iResult = getaddrinfo(serverName.c_str(), DEFAULT_PORT, &hints, &result); | |
| if (iResult != 0) { | |
| std::cout << "getaddrinfo failed: " << iResult << "\n"; | |
| WSACleanup(); | |
| return false; | |
| } | |
| ConnectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
| if (ConnectSocket == INVALID_SOCKET) { | |
| std::cout << "socket failed: " << WSAGetLastError() << "\n"; | |
| freeaddrinfo(result); | |
| WSACleanup(); | |
| return false; | |
| } | |
| iResult = connect(ConnectSocket, result->ai_addr, (int)result->ai_addrlen); | |
| if (iResult == SOCKET_ERROR) { | |
| std::cout << "connect failed: " << WSAGetLastError() << "\n"; | |
| closesocket(ConnectSocket); | |
| ConnectSocket = INVALID_SOCKET; | |
| WSACleanup(); | |
| return false; | |
| } | |
| freeaddrinfo(result); | |
| return true; | |
| } | |
| void run() { | |
| std::string input; | |
| char buffer[DEFAULT_BUFLEN]; | |
| while (true) { | |
| std::cout << "Enter currency pair (e.g. USD EUR) or EXIT: "; | |
| std::getline(std::cin, input); | |
| send(ConnectSocket, input.c_str(), static_cast<int>(input.size()), 0); | |
| if (input == "EXIT") break; | |
| int iResult = recv(ConnectSocket, buffer, DEFAULT_BUFLEN - 1, 0); | |
| if (iResult > 0) { | |
| buffer[iResult] = '\0'; | |
| std::cout << "Rate: " << buffer << std::endl; | |
| } | |
| } | |
| } | |
| ~CurrencyClient() { | |
| closesocket(ConnectSocket); | |
| WSACleanup(); | |
| } | |
| }; | |
| int main() { | |
| CurrencyClient client; | |
| if (client.init("127.0.0.1")) { | |
| client.run(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment