1
Network / Re: [SFML 2]List of all open games
« on: January 08, 2013, 07:22:23 pm »
I found a solution to my problem which is not exactly what I wanted, but the best I could get.
If anyone is interested in what I have done:
My server starts this UDP thread as in my last post.
If a client is looking for games, it will create an udp socket and send a broadcast message to all servers:
In the update-method of this game state my client is now waiting for answers from the servers, and if there is one, it will put it into a server list. This "waiting-procedure" will go on for about 10 seconds.
By now I've tested this method only with one server. But this server is found without crashing and so I am confident, that it will also work with more than one server .
If anyone is interested in what I have done:
My server starts this UDP thread as in my last post.
If a client is looking for games, it will create an udp socket and send a broadcast message to all servers:
if( udp_socket_ )
delete udp_socket_;
udp_time_ = 0.0f;
udp_socket_ = new sf::UdpSocket();
sf::Packet pack;
if( udp_socket_->send( pack, sf::IpAddress("255.255.255.255"), 12346) != sf::Socket::Done )
return;
udp_socket_->setBlocking( false );
delete udp_socket_;
udp_time_ = 0.0f;
udp_socket_ = new sf::UdpSocket();
sf::Packet pack;
if( udp_socket_->send( pack, sf::IpAddress("255.255.255.255"), 12346) != sf::Socket::Done )
return;
udp_socket_->setBlocking( false );
In the update-method of this game state my client is now waiting for answers from the servers, and if there is one, it will put it into a server list. This "waiting-procedure" will go on for about 10 seconds.
if( udp_socket_ )
{
sf::IpAddress sender;
unsigned short sender_port;
sf::Packet pack;
if( (udp_socket_->receive(pack, sender, sender_port)) == sf::Socket::Done )
games_list->addItem(sender.toString(), sender);
udp_time_ += game_env_.render_time;
if( udp_time_ >= 10.0 )
{
udp_time_ = 0.0f;
delete udp_socket_;
udp_socket_ = 0;
}
}
{
sf::IpAddress sender;
unsigned short sender_port;
sf::Packet pack;
if( (udp_socket_->receive(pack, sender, sender_port)) == sf::Socket::Done )
games_list->addItem(sender.toString(), sender);
udp_time_ += game_env_.render_time;
if( udp_time_ >= 10.0 )
{
udp_time_ = 0.0f;
delete udp_socket_;
udp_socket_ = 0;
}
}
By now I've tested this method only with one server. But this server is found without crashing and so I am confident, that it will also work with more than one server .