Skip to content

Instantly share code, notes, and snippets.

@NV-Valentine
Created February 26, 2026 07:33
Show Gist options
  • Select an option

  • Save NV-Valentine/25c9c7720cdaf957a468385897195a19 to your computer and use it in GitHub Desktop.

Select an option

Save NV-Valentine/25c9c7720cdaf957a468385897195a19 to your computer and use it in GitHub Desktop.
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <string>
#include <ctime>
#include <ws2tcpip.h>
#include <windows.h>
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
#define PAUSE 1000
string getTime() {
time_t now = time(nullptr);
tm local{};
localtime_s(&local, &now);
char buf[16];
strftime(buf, sizeof(buf), "%H:%M", &local);
return buf;
}
int main()
{
setlocale(0, "");
system("title SERVER SIDE");
cout << "Server process started!\n";
Sleep(PAUSE);
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed with error: " << iResult << "\n";
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) {
cout << "getaddrinfo failed with error: " << iResult << "\n";
WSACleanup();
return 2;
}
SOCKET ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
cout << "socket failed with error: " << WSAGetLastError() << "\n";
freeaddrinfo(result);
WSACleanup();
return 3;
}
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
cout << "bind failed with error: " << WSAGetLastError() << "\n";
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 4;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
cout << "listen failed with error: " << WSAGetLastError() << "\n";
closesocket(ListenSocket);
WSACleanup();
return 5;
}
cout << "Listening... Run the client program!\n";
SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
cout << "accept failed with error: " << WSAGetLastError() << "\n";
closesocket(ListenSocket);
WSACleanup();
return 6;
}
closesocket(ListenSocket);
char message[DEFAULT_BUFLEN];
do {
iResult = recv(ClientSocket, message, DEFAULT_BUFLEN - 1, 0);
if (iResult > 0) {
cout << "Client sent: " << message << "\n";
message[iResult] = '\0';
string reply;
if (string(message) == "як справи") {
reply = "чудово";
}
else if (string(message) == "котра година") {
reply = getTime();
}
else {
reply = "не розумію";
}
int iSendResult = send(ClientSocket, reply.c_str(), (int)reply.size(), 0);
if (iSendResult == SOCKET_ERROR) {
cout << "send failed with error: " << WSAGetLastError() << "\n";
closesocket(ClientSocket);
WSACleanup();
return 7;
}
}
else if (iResult == 0) {
cout << "Connection closing...\n";
}
else {
cout << "recv failed with error: " << WSAGetLastError() << "\n";
closesocket(ClientSocket);
WSACleanup();
return 8;
}
} while (iResult > 0);
shutdown(ClientSocket, SD_SEND);
closesocket(ClientSocket);
WSACleanup();
cout << "Server shutting down!\n";
return 0;
}
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <string>
#include <windows.h>
#include <ws2tcpip.h>
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"
#define PAUSE 1000
int main(int argc, char** argv)
{
setlocale(0, "");
system("title CLIENT SIDE");
cout << "Client process started!\n";
Sleep(PAUSE);
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed with error: " << iResult << "\n";
return 11;
}
addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
const char* ip = "localhost";
addrinfo* result = NULL;
iResult = getaddrinfo(ip, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
cout << "getaddrinfo failed with error: " << iResult << "\n";
WSACleanup();
return 12;
}
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) {
cout << "socket failed with error: " << WSAGetLastError() << "\n";
WSACleanup();
return 13;
}
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) {
cout << "Unable to connect to server. Please check if the server process is running!\n";
WSACleanup();
return 14;
}
cout << "Connection to server succeeded!\n";
Sleep(PAUSE);
char answer[DEFAULT_BUFLEN];
string userInput;
// Основной цикл общения
while (true) {
cout << "Введіть повідомлення (або 'exit' для виходу): ";
getline(cin, userInput);
if (userInput == "exit") break;
// Отправка
iResult = send(ConnectSocket, userInput.c_str(), (int)userInput.size(), 0);
if (iResult == SOCKET_ERROR) {
cout << "send failed with error: " << WSAGetLastError() << "\n";
break;
}
// Получение ответа
iResult = recv(ConnectSocket, answer, DEFAULT_BUFLEN - 1, 0);
if (iResult > 0) {
answer[iResult] = '\0';
cout << "Відповідь сервера: " << answer << "\n";
}
else if (iResult == 0) {
cout << "Connection closed by server.\n";
break;
}
else {
cout << "recv failed with error: " << WSAGetLastError() << "\n";
break;
}
}
closesocket(ConnectSocket);
WSACleanup();
cout << "Client process is shutting down!\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment