Skip to content

Instantly share code, notes, and snippets.

@XoLinA
Created February 19, 2026 09:37
Show Gist options
  • Select an option

  • Save XoLinA/6420109af8f409b2386782fc1d7d2890 to your computer and use it in GitHub Desktop.

Select an option

Save XoLinA/6420109af8f409b2386782fc1d7d2890 to your computer and use it in GitHub Desktop.
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <string>
#include <windows.h>
#include <ws2tcpip.h>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int main() {
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);
send( connectSocket,message.c_str(),message.size(),0);
if (message == "exit")
break;
char buffer[DEFAULT_BUFLEN];
int iResult = recv(connectSocket, buffer, DEFAULT_BUFLEN - 1,0);
if (iResult > 0) {
buffer[iResult] = '\0';
cout << "Server reversed: "<< buffer << endl;
}
else {
break;
}
}
shutdown(connectSocket, SD_SEND);
closesocket(connectSocket);
WSACleanup();
cout << "Client stopped.\n";
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
#include <ws2tcpip.h>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
int main()
{
setlocale(0, "");
system("title SERVER SIDE");
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 = NULL;
getaddrinfo(NULL, "27015", &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...\n";
SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
cout << "Client connected!\n";
closesocket(ListenSocket);
while (true)
{
char message[512]{};
int bytes = recv(ClientSocket, message, 512, 0);
if (bytes > 0)
{
message[bytes] = '\0';
string text = message;
cout << "Client message: " << text << endl;
if (text == "exit")
break;
reverse(text.begin(), text.end());
send(ClientSocket, text.c_str(), text.size(), 0);
}
else if (bytes == 0)
{
cout << "Connection closing...\n";
break;
}
else
{
cout << "recv error\n";
break;
}
}
shutdown(ClientSocket, SD_SEND);
closesocket(ClientSocket);
WSACleanup();
cout << "Server stopped.\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment