Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Fred_FS

Pages: [1]
1
Network / [SFML 2]List of all open games
« on: January 06, 2013, 02:47:33 pm »
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.

2
Network / network-management with SFML 2
« on: August 31, 2011, 01:12:42 pm »
Hello,

I am pretty new to networking and have just read a few tutorials about it and read the SFML documentation and treid to programm a little network-manager.
It's nothing special. One user should be able to be the host and an other one should be allowed to join the "game".

I didn't want to use a seperate server-application, so the user can choose if he wants to host a game and "be" the server.
Therefore I have a network-manager class. It does all the server-stuff(selector and listener). This network-manager is run in a sperate thread, if the user hosts a game and the methode looks like this:
Code: [Select]

while( true )
{
if( selector_.Wait() )
{
if( selector_.IsReady( listener_ ) )
{
// listener is ready, so there is a pending connection
sf::TcpSocket* client = new sf::TcpSocket;
if( listener_.Accept( *client ) == sf::Socket::Done )
{
clients_.push_back( client );
selector_.Add( *client );
client->SetBlocking( false );
std::cout << "GAME_INFO: Client connected with IP " << client->GetRemoteAddress() << std::endl;

}
}
else
{
// Check whether the other objects are ready
for( std::list<sf::TcpSocket*>::iterator it = clients_.begin();
it != clients_.end(); ++it )
{
sf::TcpSocket client = **it;
if( selector_.IsReady( client ) )
{
sf::Packet packet;
sf::Socket::Status state = client.Receive( packet );
if( state == sf::Socket::Done )
{
handle_message( client, packet );
}
}
}
}
}
}

Basiclly it's taken from the documentation. But now I have a problem. A use may disconnect for any reason. In this case I am not able to remove the socket.
What I tried:
Code: [Select]

for( std::list<sf::TcpSocket*>::iterator it = clients_.begin();
it != clients_.end();)
{
sf::TcpSocket client = **it;
if( selector_.IsReady( client ) )
{
sf::Packet packet;
sf::Socket::Status state = client.Receive( packet );
if( state == sf::Socket::Done )
{
handle_message( client, packet );
++it;
continue;
}
else if( state == sf::Socket::Disconnected )
{
selector_.Remove( client );
std::cout << "Client disconnected with IP " << client.GetRemoteAddress();
it = clients_.erase( it );
}
}
}


But this is not working. If i put "++it" to the end of the loop, my socket won't receive any messages. Even if the socket is not removed, it will not catch the welcome-message which I send to the clients.

Has anyone already programmed a network-manager with SFML 2 and can give me some hint? Or would you do it in a completly other way?

Thanks for your help,
Fred

3
Network / [SFML 2.0] IPAddress::ToString is not working
« on: August 28, 2011, 05:59:05 pm »
Hello,

if I use the IPAddress::ToString function I allways get a "Debug Assertion Failed"-error.
The expression is: _CrtIsValidHeapPointer( pUserData )

My very simple code is:
Code: [Select]

int main( int argc, char* argv[] )
{
sf::IpAddress ip("192.168.1.1");
std::cout << ip.ToString();
return 0;
}


Do I use this function in a wrong way or is it maybe a bug in SFML 2.0?

Thanks for your help,
Fred

4
SFML wiki / Creae a simple image manager
« on: April 22, 2011, 07:44:19 pm »
Hello,
I decided to create a little tutorial on handling images, because often images and sprites are used together in the same object-class and so the image is often reloaded for all objects of the same type, what shouldn't be necessary.
You can read it here.

Pages: [1]