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 - nuvw

Pages: [1]
1
Network / TcpSocket crashes server.
« on: January 01, 2011, 03:25:59 am »
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:
Code: [Select]

#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:
Code: [Select]

#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.

Pages: [1]
anything