SFML и C++ Уроки \ Разработка игр › Форумы › SFML Network › Ряд вопросов › Ответ в теме: Ряд вопросов
mypmyp, в данном случае тебе нужно использовать sf::SocketSelector. Добавляешь в него сокеты, а затем проверяешь методом isReady, если возвращают true => в сокет была записана информация. Вот пример из документации:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
// Create a socket to listen to new connections sf::TcpListener listener; listener.listen(55001); // Create a list to store the future clients std::list<sf::TcpSocket*> clients; // Create a selector sf::SocketSelector selector; // Add the listener to the selector selector.add(listener); // Endless loop that waits for new connections while (running) { // Make the selector wait for data on any socket if (selector.wait()) { // Test the listener if (selector.isReady(listener)) { // The listener is ready: there is a pending connection sf::TcpSocket* client = new sf::TcpSocket; if (listener.accept(*client) == sf::Socket::Done) { // Add the new client to the clients list clients.push_back(client); // Add the new client to the selector so that we will // be notified when he sends something selector.add(*client); } else { // Error, we won't get a new connection, delete the socket delete client; } } else { // The listener socket is not ready, test all other sockets (the clients) for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it) { sf::TcpSocket& client = **it; if (selector.isReady(client)) { // The client has sent some data, we can receive it sf::Packet packet; if (client.receive(packet) == sf::Socket::Done) { ... } } } } } } |