/
yaseeeeechka
/
CenterControl
Обзор
Документация
Войти
/
yaseeeeechka
/
CenterControl
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
CenterControl_UDP/client.cpp
79 строк
2 KB
Yaroslava
Add UDP impl
11 дек 2025, 21:44
11 дек 2025, 21:44
b29a632
Код
Авторство
О чём код?
#define _WINSOCK_DEPRECATED_NO_WARNINGS #include <SFML/Graphics.hpp> #include <string> #include <iostream> #include <vector> #include <functional> #include <memory> #include <unordered_map> #include <algorithm> #include <cmath> #include "GameFieldConfig.h" #include "MsgProtocol.h" #include "client/RoomScreenList.h" #include "client/Session.h" #include "client/AuthScreen.h" #include "client/TopScoresScreen.h" #include "client/GameScreen.h" #include "client/RoomScreen.h" #include "client/WindowApp.h" // ==================== Client ========================================== class Client { public: Client(std::string ip, uint16_t port) : server_ip_(std::move(ip)), port_(port) { #ifdef _WIN32 WSADATA wsa{}; if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { throw std::runtime_error("WSAStartup failed"); } #endif // UDP-сокет sock_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock_ == INVALID_SOCKET) { throw std::runtime_error("socket() failed"); } addr_.sin_family = AF_INET; addr_.sin_port = htons(port_); addr_.sin_addr.s_addr = inet_addr(server_ip_.c_str()); } void Run() { // Для UDP connect() просто привязывает адрес/порт по умолчанию для send/recv if (connect(sock_, (sockaddr*)&addr_, sizeof(addr_)) == SOCKET_ERROR) { throw std::runtime_error("connect() (UDP) failed"); } std::cout << "Connected to server via UDP\n"; Session session(sock_); WindowApp app; app.SetSession(&session); app.Run(); #ifdef _WIN32 closesocket(sock_); WSACleanup(); #else // на Linux можно добавить close(sock_); #endif } private: std::string server_ip_; uint16_t port_; SOCKET sock_; sockaddr_in addr_{}; }; // ==================== main ============================================ int main() { try { Client client("127.0.0.1", 5000); client.Run(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; } }