Created
February 26, 2026 16:11
-
-
Save Marie-zaj/a51309dde10d5ac55d225f1b056614b9 to your computer and use it in GitHub Desktop.
Practise5
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 <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; | |
| } |
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
| #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