Skip to content

Instantly share code, notes, and snippets.

@XoLinA
Created February 18, 2026 14:14
Show Gist options
  • Select an option

  • Save XoLinA/569e29d0734fad12a50a55fcebdd0dc6 to your computer and use it in GitHub Desktop.

Select an option

Save XoLinA/569e29d0734fad12a50a55fcebdd0dc6 to your computer and use it in GitHub Desktop.
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <string>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
using namespace std;
int main() {
setlocale(0, "");
cout << "Client starting...\n";
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
addrinfo hints{};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
addrinfo* result = nullptr;
getaddrinfo("localhost", DEFAULT_PORT, &hints, &result);
SOCKET connectSocket = socket(
result->ai_family,
result->ai_socktype,
result->ai_protocol
);
connect(
connectSocket,
result->ai_addr,
(int)result->ai_addrlen
);
freeaddrinfo(result);
cout << "Connected to server!\n";
while (true) {
string message;
cout << "\nEnter message (type exit to quit): ";
getline(cin, message);
if (message == "exit")
break;
send(
connectSocket,
message.c_str(),
message.size(),
0
);
char recvBuffer[DEFAULT_BUFLEN];
int iResult = recv(
connectSocket,
recvBuffer,
DEFAULT_BUFLEN - 1,
0
);
if (iResult > 0) {
recvBuffer[iResult] = '\0';
cout << "Server reply: "
<< recvBuffer
<< endl;
}
else break;
}
shutdown(connectSocket, SD_SEND);
closesocket(connectSocket);
WSACleanup();
cout << "Client shutting down.\n";
return 0;
}
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
using namespace std;
string getCurrentTime() {
time_t now = time(0);
tm localTime;
localtime_s(&localTime, &now);
char buffer[10];
sprintf_s(
buffer,
"%02d:%02d",
localTime.tm_hour,
localTime.tm_min
);
return string(buffer);
}
int main() {
setlocale(0, "");
cout << "Server starting...\n";
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
addrinfo hints{};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
addrinfo* result = nullptr;
getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
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 << "Waiting for client connection...\n";
SOCKET clientSocket = accept(listenSocket, NULL, NULL);
closesocket(listenSocket);
cout << "Client connected!\n";
char recvBuffer[DEFAULT_BUFLEN];
int iResult;
do {
iResult = recv(
clientSocket,
recvBuffer,
DEFAULT_BUFLEN - 1,
0
);
if (iResult > 0) {
recvBuffer[iResult] = '\0';
string message = recvBuffer;
string reply;
cout << "Client says: " << message << endl;
if (message == "how are you")
reply = "great!";
else if (message == "what time is it")
reply = getCurrentTime();
else
reply = "I don't understand";
send(
clientSocket,
reply.c_str(),
reply.size(),
0
);
}
} while (iResult > 0);
shutdown(clientSocket, SD_SEND);
closesocket(clientSocket);
WSACleanup();
cout << "Server shutting down.\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment