Skip to content

Instantly share code, notes, and snippets.

@XoLinA
Created February 26, 2026 07:27
Show Gist options
  • Select an option

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

Select an option

Save XoLinA/c3260669db829ce8761d35290dbe3dca to your computer and use it in GitHub Desktop.
практика
#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()
{
setlocale(0, "");
system("title CLIENT 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;
addrinfo* result = NULL;
getaddrinfo("localhost", "27015", &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 input;
cout << "\nEnter integer (type exit to quit): ";
getline(cin, input);
send(ConnectSocket, input.c_str(),input.size(), 0 );
if (input == "exit")
break;
char buffer[512]{};
int bytes = recv( ConnectSocket, buffer,511,0);
if (bytes > 0)
{
buffer[bytes] = '\0';
cout << "Server response: " << buffer << endl;
}
else
break;
}
shutdown(ConnectSocket, SD_SEND);
closesocket(ConnectSocket);
WSACleanup();
cout << "Client stopped.\n";
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()
{
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);
closesocket(ListenSocket);
cout << "Client connected!\n";
while (true)
{
char buffer[512]{};
int bytes = recv(ClientSocket, buffer, 511, 0);
if (bytes > 0)
{
buffer[bytes] = '\0';
string text = buffer;
if (text == "exit")
break;
try
{
int number = stoi(text);
int resultNumber = number + 1;
string response = to_string(resultNumber);
send(ClientSocket,response.c_str(),response.size(),0);
}
catch (...)
{
string error = "Invalid number";
send(ClientSocket,error.c_str(), error.size(),0);
}
}
else
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