Hello everyone
I am having some issue with TcpSockets. As this is the first time that I look into network programming, it might as well be that I overlook something general about networks and sockets. However, here it is:
Imagine the following server and client interacting.
Server:
#include <iostream>
#include <SFML/Network.hpp>
int main()
{
sf::TcpListener listener;
listener.Listen( 55555 );
sf::TcpSocket clientSocket;
listener.Accept( clientSocket );
sf::Sleep( 1.0 );
sf::Packet packet;
packet << (float)42.0;
clientSocket.Send( packet ); // server crashes here
while( true )
{
std::cout << "I am a good server who likes to loop forever." << std::endl;
sf::Sleep( 1.0 );
}
}
Client:
#include <iostream>
#include <SFML/Network.hpp>
int main()
{
sf::TcpSocket socket;
socket.Connect( sf::IpAddress::LocalHost, 55555 );
std::cout << "I am an evil client who likes to quit without cleaning up!" << std::endl;
}
As already mentioned in the code, the server crashes as soon as it tries to send a packet to the client, which has terminated a long time ago.
I know that the above code for the client is far from good. However, this is not the issue here. What bothers me is that a bad client (or even an unstable one, which crashes frequently) can easily cause the server to crash as well. I do not see any possibility to use SFML's sockets in a safer way than that.
Note: Before I switched to version 2.0 I experienced the exact same problem in 1.6.