Skip to content

Instantly share code, notes, and snippets.

@Marie-zaj
Created March 12, 2026 12:02
Show Gist options
  • Select an option

  • Save Marie-zaj/f8d2b2b1d0f370957a47d83a877141f1 to your computer and use it in GitHub Desktop.

Select an option

Save Marie-zaj/f8d2b2b1d0f370957a47d83a877141f1 to your computer and use it in GitHub Desktop.
TCP_exchange_rate
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <windows.h>
#include <ws2tcpip.h>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
#define PORT "27015"
#define BUFLEN 512
class CurrencyClient {
private:
SOCKET connectSocket = INVALID_SOCKET;
public:
bool start(const char* ip = "127.0.0.1") {
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
addrinfo hints{}, * result;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
getaddrinfo(ip, PORT, &hints, &result);
connectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (connect(connectSocket, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR) {
cout << "Connection error\n";
return false;
}
cout << "Connected to server\n";
run();
closesocket(connectSocket);
WSACleanup();
return true;
}
void run() {
char buffer[BUFLEN];
while (true) {
string message;
cout << "\nEnter currencies (example: USD EUR) or EXIT: ";
getline(cin, message);
send(connectSocket, message.c_str(), message.size(), 0);
if (message == "EXIT")
break;
int bytes = recv(connectSocket, buffer, BUFLEN, 0);
if (bytes > 0) {
buffer[bytes] = '\0';
cout << "Rate: " << buffer << endl;
}
}
}
};
int main() {
setlocale(0, "");
CurrencyClient client;
client.start();
return 0;
}
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <windows.h>
#include <ws2tcpip.h>
#include <string>
#include <map>
#include <vector>
#include <fstream>
#include <ctime>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
#define PORT "27015"
#define BUFLEN 512
class CurrencyServer {
private:
SOCKET listenSocket = INVALID_SOCKET;
SOCKET clientSocket = INVALID_SOCKET;
map<string, double> rates;
vector<string> requests;
ofstream logFile;
public:
CurrencyServer() {
rates["USD EUR"] = 0.86;
rates["EUR USD"] = 1.16;
rates["USD UAH"] = 38.5;
rates["UAH USD"] = 0.026;
logFile.open("log.txt", ios::app);
}
string getTime() {
time_t now = time(0);
char buf[64];
ctime_s(buf, sizeof(buf), &now);
return string(buf);
}
bool start() {
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
addrinfo hints{}, * result;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(NULL, PORT, &hints, &result);
listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
bind(listenSocket, result->ai_addr, (int)result->ai_addrlen);
listen(listenSocket, SOMAXCONN);
cout << "Server started...\n";
sockaddr_in clientAddr;
int size = sizeof(clientAddr);
clientSocket = accept(listenSocket, (sockaddr*)&clientAddr, &size);
char clientIP[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, INET_ADDRSTRLEN);
int clientPort = ntohs(clientAddr.sin_port);
logFile << "Client connected: " << clientIP << ":" << clientPort << endl;
logFile << "Connect time: " << getTime() << endl;
work();
logFile << "Requests:\n";
for (auto r : requests)
logFile << r << endl;
logFile << "Disconnect time: " << getTime() << endl;
logFile << "--------------------------\n";
closesocket(clientSocket);
closesocket(listenSocket);
WSACleanup();
return true;
}
void work() {
char buffer[BUFLEN];
while (true) {
int bytes = recv(clientSocket, buffer, BUFLEN, 0);
if (bytes <= 0)
break;
buffer[bytes] = '\0';
string request = buffer;
if (request == "EXIT")
break;
cout << "Client asked: " << request << endl;
requests.push_back(request);
if (rates.count(request)) {
string rate = to_string(rates[request]);
send(clientSocket, rate.c_str(), rate.size(), 0);
}
else {
string msg = "No rate";
send(clientSocket, msg.c_str(), msg.size(), 0);
}
}
}
};
int main() {
setlocale(0, "");
CurrencyServer server;
server.start();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment