Skip to content

Instantly share code, notes, and snippets.

@m1irka
Created February 26, 2026 07:21
Show Gist options
  • Select an option

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

Select an option

Save m1irka/e25b40a5e5839fea8acf94553ffce779 to your computer and use it in GitHub Desktop.
HW_integer
#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 integer (or 'exit' to quit): ";
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 <algorithm>
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
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";
SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
closesocket(ListenSocket);
cout << "Client connected!\n";
do {
char message[DEFAULT_BUFLEN] = { 0 };
iResult = recv(ClientSocket, message, DEFAULT_BUFLEN, 0);
if (iResult > 0) {
message[iResult] = '\0';
cout << "Client sent: " << message << "\n";
int num = atoi(message);
num++;
string reply = to_string(num);
cout << "Server replying: " << reply << "\n";
send(ClientSocket, reply.c_str(), (int)reply.size(), 0);
}
else if (iResult == 0) {
cout << "Connection closing...\n";
}
else {
cout << "Receive failed: " << WSAGetLastError() << "\n";
break;
}
} while (iResult > 0);
shutdown(ClientSocket, SD_SEND);
closesocket(ClientSocket);
WSACleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment