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

Author Topic: TcpSocket crashes server.  (Read 3058 times)

0 Members and 1 Guest are viewing this topic.

nuvw

  • Newbie
  • *
  • Posts: 5
    • View Profile
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.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
TcpSocket crashes server.
« Reply #1 on: January 01, 2011, 07:09:21 am »
You need to add a test in to see if sending to the client failed, from the looks of it.

nuvw

  • Newbie
  • *
  • Posts: 5
    • View Profile
TcpSocket crashes server.
« Reply #2 on: January 01, 2011, 03:22:06 pm »
I am not sure whether I understand what you try to say.

The problem is that the server dies right after the call to TcpSocket::Send. So any test that comes after that call is meaningless.

nuvw

  • Newbie
  • *
  • Posts: 5
    • View Profile
TcpSocket crashes server.
« Reply #3 on: January 01, 2011, 03:50:21 pm »
I looked further into this and it seems that the server process is getting a SIGPIPE signal, which causes it to terminate.

The problem has already been mentioned earlier:
http://www.sfml-dev.org/forum/viewtopic.php?t=945&start=0&postdays=0&postorder=asc&highlight=

I am not sure, but wouldn't this be something that could be handled by SFML?

 

anything