Created
March 5, 2026 14:00
-
-
Save XoLinA/48790aa9f33f8fed1abb028b349b9778 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define WIN32_LEAN_AND_MEAN | |
| #include <iostream> | |
| #include <string> | |
| #include <thread> | |
| #include <ws2tcpip.h> | |
| using namespace std; | |
| #pragma comment(lib,"Ws2_32.lib") | |
| void receiveThread(SOCKET s) | |
| { | |
| while (true) | |
| { | |
| char buffer[512]{}; | |
| int bytes = recv(s, buffer, 511, 0); | |
| if (bytes <= 0) break; | |
| buffer[bytes] = '\0'; | |
| cout << "\nServer: " << buffer << endl; | |
| } | |
| } | |
| int main() | |
| { | |
| WSADATA wsa; | |
| WSAStartup(MAKEWORD(2, 2), &wsa); | |
| addrinfo hints{}; | |
| hints.ai_family = AF_INET; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| addrinfo* result; | |
| getaddrinfo("localhost", "27015", &hints, &result); | |
| SOCKET s = socket(result->ai_family,result->ai_socktype,result->ai_protocol); | |
| connect(s, result->ai_addr, (int)result->ai_addrlen); | |
| freeaddrinfo(result); | |
| cout << "Connected to server\n"; | |
| thread(receiveThread, s).detach(); | |
| while (true) | |
| { | |
| string cmd; | |
| cout << "\nEnter command: "; | |
| getline(cin, cmd); | |
| send(s, cmd.c_str(), cmd.size(), 0); | |
| if (cmd == "exit") break; | |
| } | |
| closesocket(s); | |
| WSACleanup(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define WIN32_LEAN_AND_MEAN | |
| #include <iostream> | |
| #include <string> | |
| #include <thread> | |
| #include <ctime> | |
| #include <ws2tcpip.h> | |
| using namespace std; | |
| #pragma comment(lib,"Ws2_32.lib") | |
| string getTime() | |
| { | |
| time_t now = time(0); | |
| tm t; | |
| localtime_s(&t, &now); | |
| char buf[32]; | |
| sprintf_s(buf, "%02d:%02d:%02d", t.tm_hour, t.tm_min, t.tm_sec); | |
| return buf; | |
| } | |
| string getDate() | |
| { | |
| time_t now = time(0); | |
| tm t; | |
| localtime_s(&t, &now); | |
| char buf[32]; | |
| sprintf_s(buf, "%02d.%02d.%d", t.tm_mday,t.tm_mon + 1, 1900 + t.tm_year); | |
| return buf; | |
| } | |
| void processRequest(SOCKET client, string request) | |
| { | |
| string answer; | |
| if (request == "time") | |
| answer = "Time: " + getTime(); | |
| else if (request == "date") | |
| answer = "Date: " + getDate(); | |
| else if (request.find("weather") == 0) | |
| { | |
| string city = request.substr(8); | |
| answer = "Weather in " + city + ": +18C sunny"; | |
| } | |
| else if (request == "eur") | |
| answer = "EUR rate: 42.30"; | |
| else if (request == "btc") | |
| answer = "BTC rate: 65000$"; | |
| else | |
| answer = "Unknown command"; | |
| send(client, answer.c_str(), answer.size(), 0); | |
| } | |
| int main() | |
| { | |
| WSADATA wsa; | |
| WSAStartup(MAKEWORD(2, 2), &wsa); | |
| addrinfo hints{}; | |
| hints.ai_family = AF_INET; | |
| hints.ai_socktype = SOCK_STREAM; | |
| hints.ai_protocol = IPPROTO_TCP; | |
| hints.ai_flags = AI_PASSIVE; | |
| addrinfo* result; | |
| 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 << "Server started...\n"; | |
| SOCKET client = accept(listenSocket, NULL, NULL); | |
| cout << "Client connected\n"; | |
| while (true) | |
| { | |
| char buffer[512]{}; | |
| int bytes = recv(client, buffer, 511, 0); | |
| if (bytes <= 0) break; | |
| buffer[bytes] = '\0'; | |
| string request = buffer; | |
| if (request == "exit") break; | |
| cout << "Request: " << request << endl; | |
| thread(processRequest, client, request).detach(); | |
| } | |
| closesocket(client); | |
| WSACleanup(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment