Skip to content

Instantly share code, notes, and snippets.

@m1irka
Created March 7, 2026 07:52
Show Gist options
  • Select an option

  • Save m1irka/7acc2d068a28c13edc79e7e8db3c021f to your computer and use it in GitHub Desktop.

Select an option

Save m1irka/7acc2d068a28c13edc79e7e8db3c021f to your computer and use it in GitHub Desktop.
time/date/weather + city/eur/btc/ server + client
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <windows.h>
#include <ws2tcpip.h>
#include <string>
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int main() {
system("title CLIENT SIDE");
cout << "Client started!\n";
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) return 1;
addrinfo hints{};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
addrinfo* result = NULL;
const char* ip = "localhost";
iResult = getaddrinfo(ip, DEFAULT_PORT, &hints, &result);
if (iResult != 0) { WSACleanup(); return 2; }
SOCKET ConnectSocket = INVALID_SOCKET;
for (addrinfo* ptr = result; ptr != NULL; ptr = ptr->ai_next) {
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) { WSACleanup(); return 3; }
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); ConnectSocket = INVALID_SOCKET; continue; }
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) { WSACleanup(); return 4; }
cout << "Connected to server!\n";
string input;
while (true) {
cout << "Enter command [time/date/weather + city/eur/btc/exit]: ";
getline(cin, input);
if (input == "exit") break;
iResult = send(ConnectSocket, input.c_str(), (int)input.size(), 0);
if (iResult == SOCKET_ERROR) break;
char answer[DEFAULT_BUFLEN];
iResult = recv(ConnectSocket, answer, DEFAULT_BUFLEN, 0);
if (iResult > 0) {
answer[iResult] = '\0';
cout << "Server replied: " << answer << "\n";
}
else break;
}
shutdown(ConnectSocket, SD_SEND);
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <ws2tcpip.h>
#include <string>
#include <thread>
#include <ctime>
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
void handleClient(SOCKET ClientSocket) {
char buffer[DEFAULT_BUFLEN];
int iResult;
while (true) {
iResult = recv(ClientSocket, buffer, DEFAULT_BUFLEN, 0);
if (iResult <= 0) break;
buffer[iResult] = '\0';
string request(buffer);
if (request == "time") {
time_t now = time(0);
tm* ltm = localtime(&now);
char tbuf[64];
strftime(tbuf, sizeof(tbuf), "%H:%M:%S", ltm);
send(ClientSocket, tbuf, (int)strlen(tbuf), 0);
} else if (request == "date") {
time_t now = time(0);
tm* ltm = localtime(&now);
char dbuf[64];
strftime(dbuf, sizeof(dbuf), "%Y-%m-%d", ltm);
send(ClientSocket, dbuf, (int)strlen(dbuf), 0);
} else if (request.rfind("weather",0)==0) {
const char* msg = "Weather in city: Sunny +10C";
send(ClientSocket, msg, (int)strlen(msg), 0);
} else if (request == "eur") {
const char* msg = "EUR = 1.07 USD";
send(ClientSocket, msg, (int)strlen(msg), 0);
} else if (request == "btc") {
const char* msg = "BTC = 65000 USD";
send(ClientSocket, msg, (int)strlen(msg), 0);
} else {
const char* msg = "Unknown command";
send(ClientSocket, msg, (int)strlen(msg), 0);
}
}
shutdown(ClientSocket, SD_SEND);
closesocket(ClientSocket);
}
int main() {
system("title SERVER SIDE");
cout << "Server started!\n";
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) return 1;
addrinfo hints{};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
addrinfo* result = NULL;
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) { WSACleanup(); return 2; }
SOCKET ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
freeaddrinfo(result);
listen(ListenSocket, SOMAXCONN);
cout << "Server is listening...\n";
while (true) {
SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket != INVALID_SOCKET) {
cout << "Client connected!\n";
thread th(handleClient, ClientSocket);
th.detach();
}
}
closesocket(ListenSocket);
WSACleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment