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

Author Topic: What causes an sf::Socket::Error?  (Read 4986 times)

0 Members and 1 Guest are viewing this topic.

Mr. Moon

  • Newbie
  • *
  • Posts: 15
    • View Profile
What causes an sf::Socket::Error?
« on: October 18, 2010, 06:15:41 am »
So I have some code for a 2-player game that looks like this for both the host and client player, using a non-blocking SocketUDP:

Code: [Select]

sf::Socket::Status sendStatus;
sf::Socket::Status rcvStatus;
if (socket.IsValid())
{
        // SEND position data to the other player

        sf::Packet outPacket;
        outPacket << myPlayer.getX() << myPlayer.getY();
        sf::IPAddress tip = theirIP;
        sendStatus = socket.Send(outPacket, tip, PORT);

        // RECEIVE position data from the other player

        float x = theirPlayer.getX();
        float y = theirPlayer.getY();
        sf::Packet inPacket;
        unsigned short port = PORT;
        tip = theirIP;
        rcvStatus = socket.Receive(inPacket, tip, port);

        if (rcvStatus == sf::Socket::Done)
        {
                if (!(inPacket >> x >> y))
                {
                        // Error...
                }
                else
                {
                        theirPlayer.SetPosition(x, y);
                }
        }
}
else
{
        // Error.  Quit game.
}


Both players are just sending their positions to one another.

Initially I had the framerate capped at 120 FPS, and this code ran every game loop (inefficient, I know).  It would work fine for awhile, but rcvStatus would occasionally be sf::Socket::Error rather than sf::Socket::Done, which is what it usually sits at.  The problem seemed to get worse the longer the session was.  The problem persisted at 60 FPS.

At a forced limit of 10 FPS I didn't notice any of these errors.

So my hypothesis is that I was just sending out too many packets.  Could that cause an sf::Socket::Error condition?  Is there any way to find out what triggered an sf::Socket::Error?

I'm using SFML 1.6 and Visual Studio 2010 if that matters.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
What causes an sf::Socket::Error?
« Reply #1 on: October 18, 2010, 08:14:30 am »
In your code, it's not the status which is an error, it's "inPacket >> x >> y" that fails. So what's true? Your code or your explanation? ;)

UDP is not a reliable protocol: sometimes, data may arrive corrupted. This might explain that you sometimes get an error.
Laurent Gomila - SFML developer

Mr. Moon

  • Newbie
  • *
  • Posts: 15
    • View Profile
What causes an sf::Socket::Error?
« Reply #2 on: October 18, 2010, 09:30:07 am »
Quote from: "Laurent"
In your code, it's not the status which is an error, it's "inPacket >> x >> y" that fails. So what's true? Your code or your explanation? ;)

UDP is not a reliable protocol: sometimes, data may arrive corrupted. This might explain that you sometimes get an error.


 The "if (!(inPacket >> x >> y))" code doesn't actually do any error handling; the code just ignores the bad packet.  The errors I'm referring to are because rcvStatus is sometimes set to sf::Socket::Error after I call Receive().  I know this because I'm printing rcvStatus as an sf::String later in my code.  So what I'm wondering is, what does it mean if Receive() returns with an error?  Does that just mean the packet was corrupt?  And why is my performance getting worse the longer I test my game at high framerates?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
What causes an sf::Socket::Error?
« Reply #3 on: October 18, 2010, 09:36:31 am »
I can't help you more, I don't know enough about the internals of the UDP protocol to tell you what happens exactly. The best I can do is to test and see if there's anything wrong in SFML; maybe you can provide a complete minimal code that reproduces your problem?
Laurent Gomila - SFML developer

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
What causes an sf::Socket::Error?
« Reply #4 on: October 19, 2010, 11:58:41 am »
and be carfeul not to send too much packets too ;)
Mindiell
----

Mr. Moon

  • Newbie
  • *
  • Posts: 15
    • View Profile
What causes an sf::Socket::Error?
« Reply #5 on: October 20, 2010, 08:39:25 am »
Quote from: "Mindiell"
and be carfeul not to send too much packets too ;)


I haven't done any network programming for a game before; how do I know if I'm sending out too many packets?  If I'm sending out small packets constantly at 30 FPS, is that too much?  60 FPS?

And Laurent, I appreciate the concern.  If the problem persists I'll try to write a minimal example that reproduces the problem.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
What causes an sf::Socket::Error?
« Reply #6 on: October 20, 2010, 08:42:38 am »
You'll know that you send too many packets when your app starts lagging more and more ;)
Laurent Gomila - SFML developer

 

anything