Hello,
currently I am working on a little network-game using SFML-network and I have a problem listing the open games.
In my current approach the server creates an own udp-thread in which it is waiting for broadcasting messages. The clients send such broadcasting messages to receive an answer from any server which is waiting for messages. This works for one server. But I want to list all open games, hence all servers which are waiting for clients. So I thought, that I need a while-loop to handle all the packets comming from the different servers.
But therefore I would have to use non-blocking udp-sockets, because a blocking one would end up waiting for packtes to receive, but none is comming in. So my code looks like:
sf::UdpSocket udp_socket;
sf::Packet pack;
if( udp_socket.send( pack, sf::IpAddress("255.255.255.255"), 12346) != sf::Socket::Done )
return;
udp_socket.setBlocking( false );
sf::IpAddress sender;
unsigned short sender_port;
while(udp_socket.receive(pack, sender, sender_port) == sf::Socket::Done )
{
std::cout << "received" << std::endl;
}
With this code I never receive any packets, because the status of the receive-method is always 1(sf::Socket::NotReady). If I use the blocking udp_socket, I receive one packet from my existing server, but after that the next receive-command is blocking again and it is not going on.
Do you have any idea, what would be a possible solution to list all servers with a udp-broadcast?
My idea would be that the server sends a second packet after the first packet(after a short time of waiting) and if the client receives this "poisoned" packet, it will leave the loop. But I wonder, if there is a better solution.
Thanks for your help,
Fred
EDIT: Sorry I forgot to mention, that I am using SFML 2, which I downloaded a few days ago from git repository.